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" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 27 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | |
| 32 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 33 | using namespace sema; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 35 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 36 | bool InOverloadResolution, |
| 37 | StandardConversionSequence &SCS); |
| 38 | static OverloadingResult |
| 39 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 40 | UserDefinedConversionSequence& User, |
| 41 | OverloadCandidateSet& Conversions, |
| 42 | bool AllowExplicit); |
| 43 | |
| 44 | |
| 45 | static ImplicitConversionSequence::CompareKind |
| 46 | CompareStandardConversionSequences(Sema &S, |
| 47 | const StandardConversionSequence& SCS1, |
| 48 | const StandardConversionSequence& SCS2); |
| 49 | |
| 50 | static ImplicitConversionSequence::CompareKind |
| 51 | CompareQualificationConversions(Sema &S, |
| 52 | const StandardConversionSequence& SCS1, |
| 53 | const StandardConversionSequence& SCS2); |
| 54 | |
| 55 | static ImplicitConversionSequence::CompareKind |
| 56 | CompareDerivedToBaseConversions(Sema &S, |
| 57 | const StandardConversionSequence& SCS1, |
| 58 | const StandardConversionSequence& SCS2); |
| 59 | |
| 60 | |
| 61 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 62 | /// GetConversionCategory - Retrieve the implicit conversion |
| 63 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 65 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 66 | static const ImplicitConversionCategory |
| 67 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 68 | ICC_Identity, |
| 69 | ICC_Lvalue_Transformation, |
| 70 | ICC_Lvalue_Transformation, |
| 71 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 72 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 73 | ICC_Qualification_Adjustment, |
| 74 | ICC_Promotion, |
| 75 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 76 | ICC_Promotion, |
| 77 | ICC_Conversion, |
| 78 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 79 | ICC_Conversion, |
| 80 | ICC_Conversion, |
| 81 | ICC_Conversion, |
| 82 | ICC_Conversion, |
| 83 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 84 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 85 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 86 | ICC_Conversion, |
| 87 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 88 | ICC_Conversion |
| 89 | }; |
| 90 | return Category[(int)Kind]; |
| 91 | } |
| 92 | |
| 93 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 94 | /// corresponding to the given implicit conversion kind. |
| 95 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 96 | static const ImplicitConversionRank |
| 97 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 98 | ICR_Exact_Match, |
| 99 | ICR_Exact_Match, |
| 100 | ICR_Exact_Match, |
| 101 | ICR_Exact_Match, |
| 102 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 103 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 104 | ICR_Promotion, |
| 105 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 106 | ICR_Promotion, |
| 107 | ICR_Conversion, |
| 108 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 109 | ICR_Conversion, |
| 110 | ICR_Conversion, |
| 111 | ICR_Conversion, |
| 112 | ICR_Conversion, |
| 113 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 114 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 115 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 116 | ICR_Conversion, |
| 117 | ICR_Conversion, |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 118 | ICR_Complex_Real_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 119 | }; |
| 120 | return Rank[(int)Kind]; |
| 121 | } |
| 122 | |
| 123 | /// GetImplicitConversionName - Return the name of this kind of |
| 124 | /// implicit conversion. |
| 125 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 126 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 127 | "No conversion", |
| 128 | "Lvalue-to-rvalue", |
| 129 | "Array-to-pointer", |
| 130 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 131 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 132 | "Qualification", |
| 133 | "Integral promotion", |
| 134 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 135 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 136 | "Integral conversion", |
| 137 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 138 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 139 | "Floating-integral conversion", |
| 140 | "Pointer conversion", |
| 141 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 142 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 143 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 144 | "Derived-to-base conversion", |
| 145 | "Vector conversion", |
| 146 | "Vector splat", |
| 147 | "Complex-real conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | }; |
| 149 | return Name[Kind]; |
| 150 | } |
| 151 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 152 | /// StandardConversionSequence - Set the standard conversion |
| 153 | /// sequence to the identity conversion. |
| 154 | void StandardConversionSequence::setAsIdentityConversion() { |
| 155 | First = ICK_Identity; |
| 156 | Second = ICK_Identity; |
| 157 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 158 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 159 | ReferenceBinding = false; |
| 160 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 161 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 162 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 165 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 166 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 167 | /// implicit conversions. |
| 168 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 169 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 170 | if (GetConversionRank(First) > Rank) |
| 171 | Rank = GetConversionRank(First); |
| 172 | if (GetConversionRank(Second) > Rank) |
| 173 | Rank = GetConversionRank(Second); |
| 174 | if (GetConversionRank(Third) > Rank) |
| 175 | Rank = GetConversionRank(Third); |
| 176 | return Rank; |
| 177 | } |
| 178 | |
| 179 | /// isPointerConversionToBool - Determines whether this conversion is |
| 180 | /// 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] | 181 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 184 | // Note that FromType has not necessarily been transformed by the |
| 185 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 186 | // check for their presence as well as checking whether FromType is |
| 187 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 188 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 189 | (getFromType()->isPointerType() || |
| 190 | getFromType()->isObjCObjectPointerType() || |
| 191 | getFromType()->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 192 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 193 | return true; |
| 194 | |
| 195 | return false; |
| 196 | } |
| 197 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 198 | /// isPointerConversionToVoidPointer - Determines whether this |
| 199 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 200 | /// used as part of the ranking of standard conversion sequences (C++ |
| 201 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 203 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 205 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 206 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 207 | |
| 208 | // Note that FromType has not necessarily been transformed by the |
| 209 | // array-to-pointer implicit conversion, so check for its presence |
| 210 | // and redo the conversion to get a pointer. |
| 211 | if (First == ICK_Array_To_Pointer) |
| 212 | FromType = Context.getArrayDecayedType(FromType); |
| 213 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 214 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 215 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 216 | return ToPtrType->getPointeeType()->isVoidType(); |
| 217 | |
| 218 | return false; |
| 219 | } |
| 220 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | /// DebugPrint - Print this standard conversion sequence to standard |
| 222 | /// error. Useful for debugging overloading issues. |
| 223 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 224 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 225 | bool PrintedSomething = false; |
| 226 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 227 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | PrintedSomething = true; |
| 229 | } |
| 230 | |
| 231 | if (Second != ICK_Identity) { |
| 232 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 233 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 234 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 235 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 236 | |
| 237 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 238 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 239 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 240 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 241 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 242 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 243 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 244 | PrintedSomething = true; |
| 245 | } |
| 246 | |
| 247 | if (Third != ICK_Identity) { |
| 248 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 249 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 250 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 251 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 252 | PrintedSomething = true; |
| 253 | } |
| 254 | |
| 255 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 256 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 261 | /// error. Useful for debugging overloading issues. |
| 262 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 263 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 264 | if (Before.First || Before.Second || Before.Third) { |
| 265 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 266 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 267 | } |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 268 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 269 | if (After.First || After.Second || After.Third) { |
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 | After.DebugPrint(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 276 | /// error. Useful for debugging overloading issues. |
| 277 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 278 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 279 | switch (ConversionKind) { |
| 280 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 281 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 282 | Standard.DebugPrint(); |
| 283 | break; |
| 284 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 285 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 286 | UserDefined.DebugPrint(); |
| 287 | break; |
| 288 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 289 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 290 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 291 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 292 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 293 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 294 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 295 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 299 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 300 | } |
| 301 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 302 | void AmbiguousConversionSequence::construct() { |
| 303 | new (&conversions()) ConversionSet(); |
| 304 | } |
| 305 | |
| 306 | void AmbiguousConversionSequence::destruct() { |
| 307 | conversions().~ConversionSet(); |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 312 | FromTypePtr = O.FromTypePtr; |
| 313 | ToTypePtr = O.ToTypePtr; |
| 314 | new (&conversions()) ConversionSet(O.conversions()); |
| 315 | } |
| 316 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 317 | namespace { |
| 318 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 319 | // template parameter and template argument information. |
| 320 | struct DFIParamWithArguments { |
| 321 | TemplateParameter Param; |
| 322 | TemplateArgument FirstArg; |
| 323 | TemplateArgument SecondArg; |
| 324 | }; |
| 325 | } |
| 326 | |
| 327 | /// \brief Convert from Sema's representation of template deduction information |
| 328 | /// to the form used in overload-candidate information. |
| 329 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 330 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 331 | Sema::TemplateDeductionResult TDK, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 332 | TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 333 | OverloadCandidate::DeductionFailureInfo Result; |
| 334 | Result.Result = static_cast<unsigned>(TDK); |
| 335 | Result.Data = 0; |
| 336 | switch (TDK) { |
| 337 | case Sema::TDK_Success: |
| 338 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 339 | case Sema::TDK_TooManyArguments: |
| 340 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 344 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 345 | Result.Data = Info.Param.getOpaqueValue(); |
| 346 | break; |
| 347 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 348 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 349 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 350 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 351 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 352 | Saved->Param = Info.Param; |
| 353 | Saved->FirstArg = Info.FirstArg; |
| 354 | Saved->SecondArg = Info.SecondArg; |
| 355 | Result.Data = Saved; |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 360 | Result.Data = Info.take(); |
| 361 | break; |
| 362 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 363 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 364 | case Sema::TDK_FailedOverloadResolution: |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | return Result; |
| 369 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 371 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 372 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 373 | case Sema::TDK_Success: |
| 374 | case Sema::TDK_InstantiationDepth: |
| 375 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 376 | case Sema::TDK_TooManyArguments: |
| 377 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 378 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 379 | break; |
| 380 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 381 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 382 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 383 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 384 | Data = 0; |
| 385 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 386 | |
| 387 | case Sema::TDK_SubstitutionFailure: |
| 388 | // FIXME: Destroy the template arugment list? |
| 389 | Data = 0; |
| 390 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 392 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 393 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 394 | case Sema::TDK_FailedOverloadResolution: |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | TemplateParameter |
| 400 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 401 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 402 | case Sema::TDK_Success: |
| 403 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 404 | case Sema::TDK_TooManyArguments: |
| 405 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 406 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 407 | return TemplateParameter(); |
| 408 | |
| 409 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 410 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 411 | return TemplateParameter::getFromOpaqueValue(Data); |
| 412 | |
| 413 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 414 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 415 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
| 416 | |
| 417 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 418 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 419 | case Sema::TDK_FailedOverloadResolution: |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | return TemplateParameter(); |
| 424 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 425 | |
| 426 | TemplateArgumentList * |
| 427 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 428 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 429 | case Sema::TDK_Success: |
| 430 | case Sema::TDK_InstantiationDepth: |
| 431 | case Sema::TDK_TooManyArguments: |
| 432 | case Sema::TDK_TooFewArguments: |
| 433 | case Sema::TDK_Incomplete: |
| 434 | case Sema::TDK_InvalidExplicitArguments: |
| 435 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 436 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 437 | return 0; |
| 438 | |
| 439 | case Sema::TDK_SubstitutionFailure: |
| 440 | return static_cast<TemplateArgumentList*>(Data); |
| 441 | |
| 442 | // Unhandled |
| 443 | case Sema::TDK_NonDeducedMismatch: |
| 444 | case Sema::TDK_FailedOverloadResolution: |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 451 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 452 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 453 | case Sema::TDK_Success: |
| 454 | case Sema::TDK_InstantiationDepth: |
| 455 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 456 | case Sema::TDK_TooManyArguments: |
| 457 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 458 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 459 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 460 | return 0; |
| 461 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 462 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 463 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 464 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
| 465 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 466 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 467 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 468 | case Sema::TDK_FailedOverloadResolution: |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | const TemplateArgument * |
| 476 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 477 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 478 | case Sema::TDK_Success: |
| 479 | case Sema::TDK_InstantiationDepth: |
| 480 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 481 | case Sema::TDK_TooManyArguments: |
| 482 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 483 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 484 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 485 | return 0; |
| 486 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 487 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 488 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 489 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 490 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 491 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 492 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 493 | case Sema::TDK_FailedOverloadResolution: |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | void OverloadCandidateSet::clear() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 501 | inherited::clear(); |
| 502 | Functions.clear(); |
| 503 | } |
| 504 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 505 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 506 | // overload of the declarations in Old. This routine returns false if |
| 507 | // New and Old cannot be overloaded, e.g., if New has the same |
| 508 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 509 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 510 | // it does return false, MatchedDecl will point to the decl that New |
| 511 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 512 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 513 | // |
| 514 | // Example: Given the following input: |
| 515 | // |
| 516 | // void f(int, float); // #1 |
| 517 | // void f(int, int); // #2 |
| 518 | // int f(int, int); // #3 |
| 519 | // |
| 520 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 522 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 523 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 524 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 525 | // (since they have different signatures), so this routine returns |
| 526 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 527 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 528 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 529 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 530 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 531 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 533 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 534 | // |
| 535 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 536 | // into a class by a using declaration. The rules for whether to hide |
| 537 | // shadow declarations ignore some properties which otherwise figure |
| 538 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 539 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 540 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 541 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 542 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 543 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 544 | NamedDecl *OldD = *I; |
| 545 | |
| 546 | bool OldIsUsingDecl = false; |
| 547 | if (isa<UsingShadowDecl>(OldD)) { |
| 548 | OldIsUsingDecl = true; |
| 549 | |
| 550 | // We can always introduce two using declarations into the same |
| 551 | // context, even if they have identical signatures. |
| 552 | if (NewIsUsingDecl) continue; |
| 553 | |
| 554 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 555 | } |
| 556 | |
| 557 | // If either declaration was introduced by a using declaration, |
| 558 | // we'll need to use slightly different rules for matching. |
| 559 | // Essentially, these rules are the normal rules, except that |
| 560 | // function templates hide function templates with different |
| 561 | // return types or template parameter lists. |
| 562 | bool UseMemberUsingDeclRules = |
| 563 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 564 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 565 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 566 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 567 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 568 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 569 | continue; |
| 570 | } |
| 571 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 572 | Match = *I; |
| 573 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 574 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 575 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 576 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 577 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 578 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 579 | continue; |
| 580 | } |
| 581 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 582 | Match = *I; |
| 583 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 584 | } |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 585 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 586 | // We can overload with these, which can show up when doing |
| 587 | // redeclaration checks for UsingDecls. |
| 588 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 589 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 590 | // Optimistically assume that an unresolved using decl will |
| 591 | // overload; if it doesn't, we'll have to diagnose during |
| 592 | // template instantiation. |
| 593 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 594 | // (C++ 13p1): |
| 595 | // Only function declarations can be overloaded; object and type |
| 596 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 597 | Match = *I; |
| 598 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 599 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 600 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 601 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 602 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 603 | } |
| 604 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 605 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 606 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 607 | // If both of the functions are extern "C", then they are not |
| 608 | // overloads. |
| 609 | if (Old->isExternC() && New->isExternC()) |
| 610 | return false; |
| 611 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 612 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 613 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 614 | |
| 615 | // C++ [temp.fct]p2: |
| 616 | // A function template can be overloaded with other function templates |
| 617 | // and with normal (non-template) functions. |
| 618 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 619 | return true; |
| 620 | |
| 621 | // Is the function New an overload of the function Old? |
| 622 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 623 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 624 | |
| 625 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 626 | // determine whether they are overloads. If we find any mismatch |
| 627 | // in the signature, they are overloads. |
| 628 | |
| 629 | // If either of these functions is a K&R-style function (no |
| 630 | // prototype), then we consider them to have matching signatures. |
| 631 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 632 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 633 | return false; |
| 634 | |
| 635 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 636 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 637 | |
| 638 | // The signature of a function includes the types of its |
| 639 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 640 | // of the ellipsis; see C++ DR 357). |
| 641 | if (OldQType != NewQType && |
| 642 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 643 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 644 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 645 | return true; |
| 646 | |
| 647 | // C++ [temp.over.link]p4: |
| 648 | // The signature of a function template consists of its function |
| 649 | // signature, its return type and its template parameter list. The names |
| 650 | // of the template parameters are significant only for establishing the |
| 651 | // relationship between the template parameters and the rest of the |
| 652 | // signature. |
| 653 | // |
| 654 | // We check the return type and template parameter lists for function |
| 655 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 656 | // |
| 657 | // However, we don't consider either of these when deciding whether |
| 658 | // a member introduced by a shadow declaration is hidden. |
| 659 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 660 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 661 | OldTemplate->getTemplateParameters(), |
| 662 | false, TPL_TemplateMatch) || |
| 663 | OldType->getResultType() != NewType->getResultType())) |
| 664 | return true; |
| 665 | |
| 666 | // If the function is a class member, its signature includes the |
| 667 | // cv-qualifiers (if any) on the function itself. |
| 668 | // |
| 669 | // As part of this, also check whether one of the member functions |
| 670 | // is static, in which case they are not overloads (C++ |
| 671 | // 13.1p2). While not part of the definition of the signature, |
| 672 | // this check is important to determine whether these functions |
| 673 | // can be overloaded. |
| 674 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 675 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 676 | if (OldMethod && NewMethod && |
| 677 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 678 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 679 | return true; |
| 680 | |
| 681 | // The signatures match; this is not an overload. |
| 682 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 685 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 686 | /// from the given expression (Expr) to the given type (ToType). This |
| 687 | /// function returns an implicit conversion sequence that can be used |
| 688 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 689 | /// |
| 690 | /// void f(float f); |
| 691 | /// void g(int i) { f(i); } |
| 692 | /// |
| 693 | /// this routine would produce an implicit conversion sequence to |
| 694 | /// describe the initialization of f from i, which will be a standard |
| 695 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 696 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 697 | // |
| 698 | /// Note that this routine only determines how the conversion can be |
| 699 | /// performed; it does not actually perform the conversion. As such, |
| 700 | /// it will not produce any diagnostics if no conversion is available, |
| 701 | /// but will instead return an implicit conversion sequence of kind |
| 702 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 703 | /// |
| 704 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 705 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 706 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 707 | /// permitted. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 708 | static ImplicitConversionSequence |
| 709 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 710 | bool SuppressUserConversions, |
| 711 | bool AllowExplicit, |
| 712 | bool InOverloadResolution) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 713 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 714 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
| 715 | ICS.Standard)) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 716 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 717 | return ICS; |
| 718 | } |
| 719 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 720 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 721 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 722 | return ICS; |
| 723 | } |
| 724 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 725 | // C++ [over.ics.user]p4: |
| 726 | // A conversion of an expression of class type to the same class |
| 727 | // type is given Exact Match rank, and a conversion of an |
| 728 | // expression of class type to a base class of that type is |
| 729 | // given Conversion rank, in spite of the fact that a copy/move |
| 730 | // constructor (i.e., a user-defined conversion function) is |
| 731 | // called for those cases. |
| 732 | QualType FromType = From->getType(); |
| 733 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 734 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 735 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 736 | ICS.setStandard(); |
| 737 | ICS.Standard.setAsIdentityConversion(); |
| 738 | ICS.Standard.setFromType(FromType); |
| 739 | ICS.Standard.setAllToTypes(ToType); |
| 740 | |
| 741 | // We don't actually check at this point whether there is a valid |
| 742 | // copy/move constructor, since overloading just assumes that it |
| 743 | // exists. When we actually perform initialization, we'll find the |
| 744 | // appropriate constructor to copy the returned object, if needed. |
| 745 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 747 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 748 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 749 | ICS.Standard.Second = ICK_Derived_To_Base; |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 750 | |
| 751 | return ICS; |
| 752 | } |
| 753 | |
| 754 | if (SuppressUserConversions) { |
| 755 | // We're not in the case above, so there is no conversion that |
| 756 | // we can perform. |
| 757 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 758 | return ICS; |
| 759 | } |
| 760 | |
| 761 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 762 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 763 | OverloadingResult UserDefResult |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 764 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 765 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 766 | |
| 767 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 768 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 769 | // C++ [over.ics.user]p4: |
| 770 | // A conversion of an expression of class type to the same class |
| 771 | // type is given Exact Match rank, and a conversion of an |
| 772 | // expression of class type to a base class of that type is |
| 773 | // given Conversion rank, in spite of the fact that a copy |
| 774 | // constructor (i.e., a user-defined conversion function) is |
| 775 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 777 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | QualType FromCanon |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 779 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 780 | QualType ToCanon |
| 781 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 782 | if (Constructor->isCopyConstructor() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 783 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 784 | // Turn this into a "standard" conversion sequence, so that it |
| 785 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 786 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 787 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 788 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 789 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 790 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 791 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 792 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 793 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 794 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 795 | |
| 796 | // C++ [over.best.ics]p4: |
| 797 | // However, when considering the argument of a user-defined |
| 798 | // conversion function that is a candidate by 13.3.1.3 when |
| 799 | // invoked for the copying of the temporary in the second step |
| 800 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 801 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 802 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 803 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 804 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 805 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 806 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 807 | ICS.setAmbiguous(); |
| 808 | ICS.Ambiguous.setFromType(From->getType()); |
| 809 | ICS.Ambiguous.setToType(ToType); |
| 810 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 811 | Cand != Conversions.end(); ++Cand) |
| 812 | if (Cand->Viable) |
| 813 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 814 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 815 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 816 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 817 | |
| 818 | return ICS; |
| 819 | } |
| 820 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 821 | bool Sema::TryImplicitConversion(InitializationSequence &Sequence, |
| 822 | const InitializedEntity &Entity, |
| 823 | Expr *Initializer, |
| 824 | bool SuppressUserConversions, |
| 825 | bool AllowExplicitConversions, |
| 826 | bool InOverloadResolution) { |
| 827 | ImplicitConversionSequence ICS |
| 828 | = clang::TryImplicitConversion(*this, Initializer, Entity.getType(), |
| 829 | SuppressUserConversions, |
| 830 | AllowExplicitConversions, |
| 831 | InOverloadResolution); |
| 832 | if (ICS.isBad()) return true; |
| 833 | |
| 834 | // Perform the actual conversion. |
| 835 | Sequence.AddConversionSequenceStep(ICS, Entity.getType()); |
| 836 | return false; |
| 837 | } |
| 838 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 839 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 840 | /// expression From to the type ToType. Returns true if there was an |
| 841 | /// error, false otherwise. The expression From is replaced with the |
| 842 | /// converted expression. Flavor is the kind of conversion we're |
| 843 | /// performing, used in the error message. If @p AllowExplicit, |
| 844 | /// explicit user-defined conversions are permitted. |
| 845 | bool |
| 846 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 847 | AssignmentAction Action, bool AllowExplicit) { |
| 848 | ImplicitConversionSequence ICS; |
| 849 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 850 | } |
| 851 | |
| 852 | bool |
| 853 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 854 | AssignmentAction Action, bool AllowExplicit, |
| 855 | ImplicitConversionSequence& ICS) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 856 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 857 | /*SuppressUserConversions=*/false, |
| 858 | AllowExplicit, |
| 859 | /*InOverloadResolution=*/false); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 860 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 861 | } |
| 862 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 863 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 864 | /// conversion that strips "noreturn" off the nested function type. |
| 865 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 866 | QualType ToType, QualType &ResultTy) { |
| 867 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 868 | return false; |
| 869 | |
| 870 | // Strip the noreturn off the type we're converting from; noreturn can |
| 871 | // safely be removed. |
| 872 | FromType = Context.getNoReturnType(FromType, false); |
| 873 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 874 | return false; |
| 875 | |
| 876 | ResultTy = FromType; |
| 877 | return true; |
| 878 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 879 | |
| 880 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 881 | /// vector conversion. |
| 882 | /// |
| 883 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 884 | /// conversion. |
| 885 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 886 | QualType ToType, ImplicitConversionKind &ICK) { |
| 887 | // We need at least one of these types to be a vector type to have a vector |
| 888 | // conversion. |
| 889 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 890 | return false; |
| 891 | |
| 892 | // Identical types require no conversions. |
| 893 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 894 | return false; |
| 895 | |
| 896 | // There are no conversions between extended vector types, only identity. |
| 897 | if (ToType->isExtVectorType()) { |
| 898 | // There are no conversions between extended vector types other than the |
| 899 | // identity conversion. |
| 900 | if (FromType->isExtVectorType()) |
| 901 | return false; |
| 902 | |
| 903 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 904 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 905 | ICK = ICK_Vector_Splat; |
| 906 | return true; |
| 907 | } |
| 908 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 909 | |
| 910 | // We can perform the conversion between vector types in the following cases: |
| 911 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 912 | // 2)lax vector conversions are permitted and the vector types are of the |
| 913 | // same size |
| 914 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 915 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 916 | (Context.getLangOptions().LaxVectorConversions && |
| 917 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 918 | ICK = ICK_Vector_Conversion; |
| 919 | return true; |
| 920 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 921 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 923 | return false; |
| 924 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 925 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 926 | /// IsStandardConversion - Determines whether there is a standard |
| 927 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 928 | /// expression From to the type ToType. Standard conversion sequences |
| 929 | /// only consider non-class types; for conversions that involve class |
| 930 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 931 | /// contain the standard conversion sequence required to perform this |
| 932 | /// conversion and this routine will return true. Otherwise, this |
| 933 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 934 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 935 | bool InOverloadResolution, |
| 936 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 937 | QualType FromType = From->getType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 939 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 940 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 941 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 942 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 943 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 944 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 945 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 946 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 948 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 949 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 950 | return false; |
| 951 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 955 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 956 | // array-to-pointer conversion, or function-to-pointer conversion |
| 957 | // (C++ 4p1). |
| 958 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 959 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 960 | DeclAccessPair AccessPair; |
| 961 | if (FunctionDecl *Fn |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 962 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 963 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 964 | // We were able to resolve the address of the overloaded function, |
| 965 | // so we can convert to the type of that function. |
| 966 | FromType = Fn->getType(); |
| 967 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 968 | if (!Method->isStatic()) { |
| 969 | Type *ClassType |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 970 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 971 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
| 975 | // If the "from" expression takes the address of the overloaded |
| 976 | // function, update the type of the resulting expression accordingly. |
| 977 | if (FromType->getAs<FunctionType>()) |
| 978 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(From->IgnoreParens())) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 979 | if (UnOp->getOpcode() == UO_AddrOf) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 980 | FromType = S.Context.getPointerType(FromType); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 981 | |
| 982 | // Check that we've computed the proper type after overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 983 | assert(S.Context.hasSameType(FromType, |
| 984 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 985 | } else { |
| 986 | return false; |
| 987 | } |
| 988 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 990 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 991 | // converted to an rvalue. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 992 | Expr::isLvalueResult argIsLvalue = From->isLvalue(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 994 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 995 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 996 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 997 | |
| 998 | // If T is a non-class type, the type of the rvalue is the |
| 999 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1000 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1001 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1002 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1003 | } else if (FromType->isArrayType()) { |
| 1004 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1005 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1006 | |
| 1007 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1008 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1009 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1010 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1011 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1012 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1013 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1014 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1015 | |
| 1016 | // For the purpose of ranking in overload resolution |
| 1017 | // (13.3.3.1.1), this conversion is considered an |
| 1018 | // array-to-pointer conversion followed by a qualification |
| 1019 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1020 | SCS.Second = ICK_Identity; |
| 1021 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1022 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1023 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1024 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1025 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 1026 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1027 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1028 | |
| 1029 | // An lvalue of function type T can be converted to an rvalue of |
| 1030 | // type "pointer to T." The result is a pointer to the |
| 1031 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1032 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1033 | } else { |
| 1034 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1035 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1036 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1037 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1038 | |
| 1039 | // The second conversion can be an integral promotion, floating |
| 1040 | // point promotion, integral conversion, floating point conversion, |
| 1041 | // floating-integral conversion, pointer conversion, |
| 1042 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1043 | // For overloading in C, this can also be a "compatible-type" |
| 1044 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1045 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1046 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1047 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1048 | // The unqualified versions of the types are the same: there's no |
| 1049 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1050 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1051 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1053 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1054 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1055 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1056 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1057 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1058 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1059 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1060 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1061 | SCS.Second = ICK_Complex_Promotion; |
| 1062 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1063 | } else if (FromType->isIntegralOrEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1064 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1065 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1066 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1067 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1068 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 1069 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1070 | SCS.Second = ICK_Complex_Conversion; |
| 1071 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1072 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 1073 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 1074 | // Complex-real conversions (C99 6.3.1.7) |
| 1075 | SCS.Second = ICK_Complex_Real; |
| 1076 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1077 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1078 | // Floating point conversions (C++ 4.8). |
| 1079 | SCS.Second = ICK_Floating_Conversion; |
| 1080 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1081 | } else if ((FromType->isRealFloatingType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1082 | ToType->isIntegralType(S.Context) && !ToType->isBooleanType()) || |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1083 | (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1084 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1085 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1086 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1087 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1088 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1089 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1090 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1091 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1092 | SCS.IncompatibleObjC = IncompatibleObjC; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1093 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
| 1094 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1095 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1096 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1097 | } else if (ToType->isBooleanType() && |
| 1098 | (FromType->isArithmeticType() || |
| 1099 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 1100 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1101 | FromType->isBlockPointerType() || |
| 1102 | FromType->isMemberPointerType() || |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1103 | FromType->isNullPtrType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1104 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1105 | SCS.Second = ICK_Boolean_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1106 | FromType = S.Context.BoolTy; |
| 1107 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1108 | SCS.Second = SecondICK; |
| 1109 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1110 | } else if (!S.getLangOptions().CPlusPlus && |
| 1111 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1112 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1113 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1114 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1115 | } else if (IsNoReturnConversion(S.Context, FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1116 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1117 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1118 | } else { |
| 1119 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1120 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1121 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1122 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1124 | QualType CanonFrom; |
| 1125 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1126 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1127 | if (S.IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1128 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1129 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1130 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1131 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1132 | } else { |
| 1133 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1134 | SCS.Third = ICK_Identity; |
| 1135 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1137 | // [...] Any difference in top-level cv-qualification is |
| 1138 | // subsumed by the initialization itself and does not constitute |
| 1139 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1140 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1141 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1142 | if (CanonFrom.getLocalUnqualifiedType() |
| 1143 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1144 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1145 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1146 | FromType = ToType; |
| 1147 | CanonFrom = CanonTo; |
| 1148 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1149 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1150 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1151 | |
| 1152 | // If we have not converted the argument type to the parameter type, |
| 1153 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1154 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1155 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1156 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1157 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1161 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1162 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1163 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1165 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1166 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1167 | if (!To) { |
| 1168 | return false; |
| 1169 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1170 | |
| 1171 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1172 | // unsigned short int can be converted to an rvalue of type int if |
| 1173 | // int can represent all the values of the source type; otherwise, |
| 1174 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1175 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1176 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1177 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1178 | if (// We can promote any signed, promotable integer type to an int |
| 1179 | (FromType->isSignedIntegerType() || |
| 1180 | // We can promote any unsigned integer type whose size is |
| 1181 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1183 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1184 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1187 | return To->getKind() == BuiltinType::UInt; |
| 1188 | } |
| 1189 | |
| 1190 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 1191 | // can be converted to an rvalue of the first of the following types |
| 1192 | // that can represent all the values of its underlying type: int, |
| 1193 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1194 | |
| 1195 | // We pre-calculate the promotion type for enum types. |
| 1196 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 1197 | if (ToType->isIntegerType()) |
| 1198 | return Context.hasSameUnqualifiedType(ToType, |
| 1199 | FromEnumType->getDecl()->getPromotionType()); |
| 1200 | |
| 1201 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1202 | // Determine whether the type we're converting from is signed or |
| 1203 | // unsigned. |
| 1204 | bool FromIsSigned; |
| 1205 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1206 | |
| 1207 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1208 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1209 | |
| 1210 | // The types we'll try to promote to, in the appropriate |
| 1211 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | QualType PromoteTypes[6] = { |
| 1213 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1214 | Context.LongTy, Context.UnsignedLongTy , |
| 1215 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1216 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1217 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1218 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1219 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1221 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1222 | // We found the type that we can promote to. If this is the |
| 1223 | // type we wanted, we have a promotion. Otherwise, no |
| 1224 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1225 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1231 | // rvalue of type int if int can represent all the values of the |
| 1232 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1233 | // unsigned int can represent all the values of the bit-field. If |
| 1234 | // the bit-field is larger yet, no integral promotion applies to |
| 1235 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1236 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1237 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1238 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1239 | using llvm::APSInt; |
| 1240 | if (From) |
| 1241 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1242 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1243 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1244 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1245 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1246 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1248 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1249 | if (BitWidth < ToSize || |
| 1250 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1251 | return To->getKind() == BuiltinType::Int; |
| 1252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1254 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1255 | // that fits into an unsigned int? |
| 1256 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1257 | return To->getKind() == BuiltinType::UInt; |
| 1258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1260 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1261 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1264 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1265 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1266 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1267 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1268 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1269 | |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
| 1273 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1274 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1275 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1277 | /// An rvalue of type float can be converted to an rvalue of type |
| 1278 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1279 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1280 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1281 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1282 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1283 | return true; |
| 1284 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1285 | // C99 6.3.1.5p1: |
| 1286 | // When a float is promoted to double or long double, or a |
| 1287 | // double is promoted to long double [...]. |
| 1288 | if (!getLangOptions().CPlusPlus && |
| 1289 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1290 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1291 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1292 | return true; |
| 1293 | } |
| 1294 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| 1297 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1298 | /// \brief Determine if a conversion is a complex promotion. |
| 1299 | /// |
| 1300 | /// A complex promotion is defined as a complex -> complex conversion |
| 1301 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1302 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1303 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1304 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1305 | if (!FromComplex) |
| 1306 | return false; |
| 1307 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1308 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1309 | if (!ToComplex) |
| 1310 | return false; |
| 1311 | |
| 1312 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1313 | ToComplex->getElementType()) || |
| 1314 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1315 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1318 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1319 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1320 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1321 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1322 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | static QualType |
| 1324 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1325 | QualType ToPointee, QualType ToType, |
| 1326 | ASTContext &Context) { |
| 1327 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 1328 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1329 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1332 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1333 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1334 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1335 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1338 | // already. |
| 1339 | return Context.getPointerType(ToPointee); |
| 1340 | } |
| 1341 | |
| 1342 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1343 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1344 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1345 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1348 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1349 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1350 | /// not have the right set of qualifiers. |
| 1351 | static QualType |
| 1352 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1353 | QualType ToType, |
| 1354 | ASTContext &Context) { |
| 1355 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1356 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1357 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1358 | |
| 1359 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1360 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1361 | return ToType; |
| 1362 | |
| 1363 | // Just build a canonical type that has the right qualifiers. |
| 1364 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1365 | } |
| 1366 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1368 | bool InOverloadResolution, |
| 1369 | ASTContext &Context) { |
| 1370 | // Handle value-dependent integral null pointer constants correctly. |
| 1371 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1372 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1373 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1374 | return !InOverloadResolution; |
| 1375 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1376 | return Expr->isNullPointerConstant(Context, |
| 1377 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1378 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1379 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1381 | /// IsPointerConversion - Determines whether the conversion of the |
| 1382 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1383 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1384 | /// 4.10). If so, returns true and places the converted type (that |
| 1385 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1386 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1387 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1388 | /// This routine also supports conversions to and from block pointers |
| 1389 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1390 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1391 | /// appropriate overloading rules for Objective-C, we may want to |
| 1392 | /// split the Objective-C checks into a different routine; however, |
| 1393 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1394 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1395 | /// set if the conversion is an allowed Objective-C conversion that |
| 1396 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1397 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1398 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1399 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1401 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1402 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1403 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1404 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1406 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1407 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1408 | ConvertedType = ToType; |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1412 | // Blocks: Block pointers can be converted to void*. |
| 1413 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1414 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1415 | ConvertedType = ToType; |
| 1416 | return true; |
| 1417 | } |
| 1418 | // Blocks: A null pointer constant can be converted to a block |
| 1419 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1421 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1422 | ConvertedType = ToType; |
| 1423 | return true; |
| 1424 | } |
| 1425 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1426 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1427 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1429 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1430 | ConvertedType = ToType; |
| 1431 | return true; |
| 1432 | } |
| 1433 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1434 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1435 | if (!ToTypePtr) |
| 1436 | return false; |
| 1437 | |
| 1438 | // 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] | 1439 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1440 | ConvertedType = ToType; |
| 1441 | return true; |
| 1442 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1443 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1444 | // Beyond this point, both types need to be pointers |
| 1445 | // , including objective-c pointers. |
| 1446 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1447 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1448 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1449 | ToType, Context); |
| 1450 | return true; |
| 1451 | |
| 1452 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1453 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1454 | if (!FromTypePtr) |
| 1455 | return false; |
| 1456 | |
| 1457 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 4e938f57b | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 1459 | // If the unqualified pointee types are the same, this can't be a |
| 1460 | // pointer conversion, so don't do all of the work below. |
| 1461 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1462 | return false; |
| 1463 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1464 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1465 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1466 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1467 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1468 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1470 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1471 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1472 | return true; |
| 1473 | } |
| 1474 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1475 | // When we're overloading in C, we allow a special kind of pointer |
| 1476 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1478 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1480 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1482 | return true; |
| 1483 | } |
| 1484 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1485 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1487 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1488 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1489 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1490 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1491 | // necessitates this conversion is ill-formed. The result of the |
| 1492 | // conversion is a pointer to the base class sub-object of the |
| 1493 | // derived class object. The null pointer value is converted to |
| 1494 | // the null pointer value of the destination type. |
| 1495 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1496 | // Note that we do not check for ambiguity or inaccessibility |
| 1497 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1498 | if (getLangOptions().CPlusPlus && |
| 1499 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1500 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1501 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1502 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1504 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1505 | ToType, Context); |
| 1506 | return true; |
| 1507 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1508 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | /// isObjCPointerConversion - Determines whether this is an |
| 1513 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1514 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1516 | QualType& ConvertedType, |
| 1517 | bool &IncompatibleObjC) { |
| 1518 | if (!getLangOptions().ObjC1) |
| 1519 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1520 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1521 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1522 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1524 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1525 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1526 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1527 | // 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] | 1528 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1529 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1530 | ConvertedType = ToType; |
| 1531 | return true; |
| 1532 | } |
| 1533 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1535 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1537 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1538 | ConvertedType = ToType; |
| 1539 | return true; |
| 1540 | } |
| 1541 | // Objective C++: We're able to convert from a pointer to an |
| 1542 | // interface to a pointer to a different interface. |
| 1543 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1544 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1545 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1546 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1547 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1548 | FromObjCPtr->getPointeeType())) |
| 1549 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1550 | ConvertedType = ToType; |
| 1551 | return true; |
| 1552 | } |
| 1553 | |
| 1554 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1555 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1556 | // interfaces, which is permitted. However, we're going to |
| 1557 | // complain about it. |
| 1558 | IncompatibleObjC = true; |
| 1559 | ConvertedType = FromType; |
| 1560 | return true; |
| 1561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1563 | // 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] | 1564 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1565 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1566 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1567 | else if (const BlockPointerType *ToBlockPtr = |
| 1568 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1569 | // 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] | 1570 | // to a block pointer type. |
| 1571 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1572 | ConvertedType = ToType; |
| 1573 | return true; |
| 1574 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1575 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1576 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1577 | else if (FromType->getAs<BlockPointerType>() && |
| 1578 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1579 | // 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] | 1580 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1581 | ConvertedType = ToType; |
| 1582 | return true; |
| 1583 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1584 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1585 | return false; |
| 1586 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1587 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1588 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1589 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1590 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1591 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1592 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1593 | return false; |
| 1594 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1595 | // If we have pointers to pointers, recursively check whether this |
| 1596 | // is an Objective-C conversion. |
| 1597 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1598 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1599 | IncompatibleObjC)) { |
| 1600 | // We always complain about this conversion. |
| 1601 | IncompatibleObjC = true; |
| 1602 | ConvertedType = ToType; |
| 1603 | return true; |
| 1604 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1605 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1606 | // as in I* to id. |
| 1607 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1608 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1609 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1610 | IncompatibleObjC)) { |
| 1611 | ConvertedType = ToType; |
| 1612 | return true; |
| 1613 | } |
| 1614 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1615 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1616 | // differences in the argument and result types are in Objective-C |
| 1617 | // pointer conversions. If so, we permit the conversion (but |
| 1618 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1620 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1621 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1622 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1623 | if (FromFunctionType && ToFunctionType) { |
| 1624 | // If the function types are exactly the same, this isn't an |
| 1625 | // Objective-C pointer conversion. |
| 1626 | if (Context.getCanonicalType(FromPointeeType) |
| 1627 | == Context.getCanonicalType(ToPointeeType)) |
| 1628 | return false; |
| 1629 | |
| 1630 | // Perform the quick checks that will tell us whether these |
| 1631 | // function types are obviously different. |
| 1632 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1633 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1634 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1635 | return false; |
| 1636 | |
| 1637 | bool HasObjCConversion = false; |
| 1638 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1639 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1640 | // Okay, the types match exactly. Nothing to do. |
| 1641 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1642 | ToFunctionType->getResultType(), |
| 1643 | ConvertedType, IncompatibleObjC)) { |
| 1644 | // Okay, we have an Objective-C pointer conversion. |
| 1645 | HasObjCConversion = true; |
| 1646 | } else { |
| 1647 | // Function types are too different. Abort. |
| 1648 | return false; |
| 1649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1651 | // Check argument types. |
| 1652 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1653 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1654 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1655 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1656 | if (Context.getCanonicalType(FromArgType) |
| 1657 | == Context.getCanonicalType(ToArgType)) { |
| 1658 | // Okay, the types match exactly. Nothing to do. |
| 1659 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1660 | ConvertedType, IncompatibleObjC)) { |
| 1661 | // Okay, we have an Objective-C pointer conversion. |
| 1662 | HasObjCConversion = true; |
| 1663 | } else { |
| 1664 | // Argument types are too different. Abort. |
| 1665 | return false; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | if (HasObjCConversion) { |
| 1670 | // We had an Objective-C conversion. Allow this pointer |
| 1671 | // conversion, but complain about it. |
| 1672 | ConvertedType = ToType; |
| 1673 | IncompatibleObjC = true; |
| 1674 | return true; |
| 1675 | } |
| 1676 | } |
| 1677 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1678 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1679 | } |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1680 | |
| 1681 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1682 | /// for equlity of their argument types. Caller has already checked that |
| 1683 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1684 | /// pointer types which only differ in their protocol qualifiers are equal. |
| 1685 | bool Sema::FunctionArgTypesAreEqual(FunctionProtoType* OldType, |
| 1686 | FunctionProtoType* NewType){ |
| 1687 | if (!getLangOptions().ObjC1) |
| 1688 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1689 | NewType->arg_type_begin()); |
| 1690 | |
| 1691 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1692 | N = NewType->arg_type_begin(), |
| 1693 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1694 | QualType ToType = (*O); |
| 1695 | QualType FromType = (*N); |
| 1696 | if (ToType != FromType) { |
| 1697 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1698 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1699 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1700 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1701 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1702 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1703 | continue; |
| 1704 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1705 | else if (const ObjCObjectPointerType *PTTo = |
| 1706 | ToType->getAs<ObjCObjectPointerType>()) { |
| 1707 | if (const ObjCObjectPointerType *PTFr = |
| 1708 | FromType->getAs<ObjCObjectPointerType>()) |
| 1709 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1710 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1711 | } |
| 1712 | return false; |
| 1713 | } |
| 1714 | } |
| 1715 | return true; |
| 1716 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1718 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1719 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1720 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1721 | /// conversions for which IsPointerConversion has already returned |
| 1722 | /// true. It returns true and produces a diagnostic if there was an |
| 1723 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1724 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1725 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1726 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1727 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1728 | QualType FromType = From->getType(); |
| 1729 | |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1730 | if (CXXBoolLiteralExpr* LitBool |
| 1731 | = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens())) |
| 1732 | if (LitBool->getValue() == false) |
| 1733 | Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false) |
| 1734 | << ToType; |
| 1735 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1736 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1737 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1738 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1739 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1741 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1742 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1743 | // We must have a derived-to-base conversion. Check an |
| 1744 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1745 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1746 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1747 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1748 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1749 | return true; |
| 1750 | |
| 1751 | // The conversion was successful. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1752 | Kind = CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1756 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1758 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1759 | // Objective-C++ conversions are always okay. |
| 1760 | // FIXME: We should have a different class of conversions for the |
| 1761 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1762 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1763 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1764 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1765 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1766 | return false; |
| 1767 | } |
| 1768 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1769 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1770 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1771 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1772 | /// If so, returns true and places the converted type (that might differ from |
| 1773 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1774 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1775 | QualType ToType, |
| 1776 | bool InOverloadResolution, |
| 1777 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1778 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1779 | if (!ToTypePtr) |
| 1780 | return false; |
| 1781 | |
| 1782 | // 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] | 1783 | if (From->isNullPointerConstant(Context, |
| 1784 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1785 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1786 | ConvertedType = ToType; |
| 1787 | return true; |
| 1788 | } |
| 1789 | |
| 1790 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1791 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1792 | if (!FromTypePtr) |
| 1793 | return false; |
| 1794 | |
| 1795 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1796 | // where D is derived from B (C++ 4.11p2). |
| 1797 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1798 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1799 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1800 | |
| 1801 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1802 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1803 | ToClass.getTypePtr()); |
| 1804 | return true; |
| 1805 | } |
| 1806 | |
| 1807 | return false; |
| 1808 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1809 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1810 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1811 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1812 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1813 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1814 | /// true and produces a diagnostic if there was an error, or returns false |
| 1815 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1817 | CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1818 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1819 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1820 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1821 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1822 | if (!FromPtrType) { |
| 1823 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1824 | assert(From->isNullPointerConstant(Context, |
| 1825 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1826 | "Expr must be null pointer constant!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1827 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1828 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1829 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1830 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1831 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1832 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1833 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1834 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1835 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1836 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1837 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1838 | // FIXME: What about dependent types? |
| 1839 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1840 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1841 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1842 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1843 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1844 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1845 | assert(DerivationOkay && |
| 1846 | "Should not have been called if derivation isn't OK."); |
| 1847 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1848 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1849 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1850 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1851 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1852 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1853 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1854 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1855 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1857 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1858 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1859 | << FromClass << ToClass << QualType(VBase, 0) |
| 1860 | << From->getSourceRange(); |
| 1861 | return true; |
| 1862 | } |
| 1863 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1864 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1865 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1866 | Paths.front(), |
| 1867 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1868 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1869 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1870 | BuildBasePathArray(Paths, BasePath); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1871 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1872 | return false; |
| 1873 | } |
| 1874 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1875 | /// IsQualificationConversion - Determines whether the conversion from |
| 1876 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1877 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | bool |
| 1879 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1880 | FromType = Context.getCanonicalType(FromType); |
| 1881 | ToType = Context.getCanonicalType(ToType); |
| 1882 | |
| 1883 | // If FromType and ToType are the same type, this is not a |
| 1884 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1885 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1886 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1888 | // (C++ 4.4p4): |
| 1889 | // A conversion can add cv-qualifiers at levels other than the first |
| 1890 | // in multi-level pointers, subject to the following rules: [...] |
| 1891 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1892 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1893 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1894 | // Within each iteration of the loop, we check the qualifiers to |
| 1895 | // determine if this still looks like a qualification |
| 1896 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1897 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1898 | // until there are no more pointers or pointers-to-members left to |
| 1899 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1900 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1901 | |
| 1902 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1903 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1904 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1905 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1907 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1908 | // every cv for 0 < k < j. |
| 1909 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1910 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1911 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1913 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1914 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1916 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1917 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1918 | |
| 1919 | // We are left with FromType and ToType being the pointee types |
| 1920 | // after unwrapping the original FromType and ToType the same number |
| 1921 | // of types. If we unwrapped any pointers, and if FromType and |
| 1922 | // ToType have the same unqualified type (since we checked |
| 1923 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1924 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1927 | /// Determines whether there is a user-defined conversion sequence |
| 1928 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1929 | /// ToType. If such a conversion exists, User will contain the |
| 1930 | /// user-defined conversion sequence that performs such a conversion |
| 1931 | /// and this routine will return true. Otherwise, this routine returns |
| 1932 | /// false and User is unspecified. |
| 1933 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1934 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1935 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1936 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1937 | static OverloadingResult |
| 1938 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1939 | UserDefinedConversionSequence& User, |
| 1940 | OverloadCandidateSet& CandidateSet, |
| 1941 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1942 | // Whether we will only visit constructors. |
| 1943 | bool ConstructorsOnly = false; |
| 1944 | |
| 1945 | // If the type we are conversion to is a class type, enumerate its |
| 1946 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1947 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1948 | // C++ [over.match.ctor]p1: |
| 1949 | // When objects of class type are direct-initialized (8.5), or |
| 1950 | // copy-initialized from an expression of the same or a |
| 1951 | // derived class type (8.5), overload resolution selects the |
| 1952 | // constructor. [...] For copy-initialization, the candidate |
| 1953 | // functions are all the converting constructors (12.3.1) of |
| 1954 | // that class. The argument list is the expression-list within |
| 1955 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1956 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1957 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1958 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1959 | ConstructorsOnly = true; |
| 1960 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1961 | if (S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag())) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1962 | // We're not going to find any constructors. |
| 1963 | } else if (CXXRecordDecl *ToRecordDecl |
| 1964 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1965 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1966 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1967 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1968 | NamedDecl *D = *Con; |
| 1969 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1970 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1971 | // Find the constructor (which may be a template). |
| 1972 | CXXConstructorDecl *Constructor = 0; |
| 1973 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1974 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1975 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1977 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1978 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1979 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1980 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1981 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1982 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1983 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1984 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 1985 | /*ExplicitArgs*/ 0, |
| 1986 | &From, 1, CandidateSet, |
| 1987 | /*SuppressUserConversions=*/ |
| 1988 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1989 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1990 | // Allow one user-defined conversion when user specifies a |
| 1991 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1992 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 1993 | &From, 1, CandidateSet, |
| 1994 | /*SuppressUserConversions=*/ |
| 1995 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1996 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1997 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2001 | // Enumerate conversion functions, if we're allowed to. |
| 2002 | if (ConstructorsOnly) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2003 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2004 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2005 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2007 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2009 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2010 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2011 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2012 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2013 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2014 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2015 | DeclAccessPair FoundDecl = I.getPair(); |
| 2016 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2017 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2018 | if (isa<UsingShadowDecl>(D)) |
| 2019 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2020 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2021 | CXXConversionDecl *Conv; |
| 2022 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2023 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2024 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2025 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2026 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2027 | |
| 2028 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2029 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2030 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2031 | ActingContext, From, ToType, |
| 2032 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2033 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2034 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2035 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2039 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2040 | |
| 2041 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2042 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best)) { |
| 2043 | case OR_Success: |
| 2044 | // Record the standard conversion we used and the conversion function. |
| 2045 | if (CXXConstructorDecl *Constructor |
| 2046 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 2047 | // C++ [over.ics.user]p1: |
| 2048 | // If the user-defined conversion is specified by a |
| 2049 | // constructor (12.3.1), the initial standard conversion |
| 2050 | // sequence converts the source type to the type required by |
| 2051 | // the argument of the constructor. |
| 2052 | // |
| 2053 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2054 | if (Best->Conversions[0].isEllipsis()) |
| 2055 | User.EllipsisConversion = true; |
| 2056 | else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2057 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2058 | User.EllipsisConversion = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2059 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2060 | User.ConversionFunction = Constructor; |
| 2061 | User.After.setAsIdentityConversion(); |
| 2062 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2063 | User.After.setAllToTypes(ToType); |
| 2064 | return OR_Success; |
| 2065 | } else if (CXXConversionDecl *Conversion |
| 2066 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 2067 | // C++ [over.ics.user]p1: |
| 2068 | // |
| 2069 | // [...] If the user-defined conversion is specified by a |
| 2070 | // conversion function (12.3.2), the initial standard |
| 2071 | // conversion sequence converts the source type to the |
| 2072 | // implicit object parameter of the conversion function. |
| 2073 | User.Before = Best->Conversions[0].Standard; |
| 2074 | User.ConversionFunction = Conversion; |
| 2075 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2076 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2077 | // C++ [over.ics.user]p2: |
| 2078 | // The second standard conversion sequence converts the |
| 2079 | // result of the user-defined conversion to the target type |
| 2080 | // for the sequence. Since an implicit conversion sequence |
| 2081 | // is an initialization, the special rules for |
| 2082 | // initialization by user-defined conversion apply when |
| 2083 | // selecting the best user-defined conversion for a |
| 2084 | // user-defined conversion sequence (see 13.3.3 and |
| 2085 | // 13.3.3.1). |
| 2086 | User.After = Best->FinalConversion; |
| 2087 | return OR_Success; |
| 2088 | } else { |
| 2089 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2090 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2093 | case OR_No_Viable_Function: |
| 2094 | return OR_No_Viable_Function; |
| 2095 | case OR_Deleted: |
| 2096 | // No conversion here! We're done. |
| 2097 | return OR_Deleted; |
| 2098 | |
| 2099 | case OR_Ambiguous: |
| 2100 | return OR_Ambiguous; |
| 2101 | } |
| 2102 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2103 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2104 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2105 | |
| 2106 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2107 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2108 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2109 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2110 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2111 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2112 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2113 | if (OvResult == OR_Ambiguous) |
| 2114 | Diag(From->getSourceRange().getBegin(), |
| 2115 | diag::err_typecheck_ambiguous_condition) |
| 2116 | << From->getType() << ToType << From->getSourceRange(); |
| 2117 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2118 | Diag(From->getSourceRange().getBegin(), |
| 2119 | diag::err_typecheck_nonviable_condition) |
| 2120 | << From->getType() << ToType << From->getSourceRange(); |
| 2121 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2122 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2123 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2124 | return true; |
| 2125 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2127 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2128 | /// conversion sequences to determine whether one is better than the |
| 2129 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2130 | static ImplicitConversionSequence::CompareKind |
| 2131 | CompareImplicitConversionSequences(Sema &S, |
| 2132 | const ImplicitConversionSequence& ICS1, |
| 2133 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2134 | { |
| 2135 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2136 | // conversion sequences (as defined in 13.3.3.1) |
| 2137 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2138 | // conversion sequence than a user-defined conversion sequence or |
| 2139 | // an ellipsis conversion sequence, and |
| 2140 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2141 | // conversion sequence than an ellipsis conversion sequence |
| 2142 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2144 | // C++0x [over.best.ics]p10: |
| 2145 | // For the purpose of ranking implicit conversion sequences as |
| 2146 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2147 | // treated as a user-defined sequence that is indistinguishable |
| 2148 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2149 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2150 | return ImplicitConversionSequence::Better; |
| 2151 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2152 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2153 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2154 | // The following checks require both conversion sequences to be of |
| 2155 | // the same kind. |
| 2156 | if (ICS1.getKind() != ICS2.getKind()) |
| 2157 | return ImplicitConversionSequence::Indistinguishable; |
| 2158 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2159 | // Two implicit conversion sequences of the same form are |
| 2160 | // indistinguishable conversion sequences unless one of the |
| 2161 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2162 | if (ICS1.isStandard()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2163 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2164 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2165 | // User-defined conversion sequence U1 is a better conversion |
| 2166 | // sequence than another user-defined conversion sequence U2 if |
| 2167 | // they contain the same user-defined conversion function or |
| 2168 | // constructor and if the second standard conversion sequence of |
| 2169 | // U1 is better than the second standard conversion sequence of |
| 2170 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2172 | ICS2.UserDefined.ConversionFunction) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2173 | return CompareStandardConversionSequences(S, |
| 2174 | ICS1.UserDefined.After, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2175 | ICS2.UserDefined.After); |
| 2176 | } |
| 2177 | |
| 2178 | return ImplicitConversionSequence::Indistinguishable; |
| 2179 | } |
| 2180 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2181 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2182 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2183 | Qualifiers Quals; |
| 2184 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
| 2185 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
| 2186 | } |
| 2187 | |
| 2188 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2189 | } |
| 2190 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2191 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2192 | // determine if one is a proper subset of the other. |
| 2193 | static ImplicitConversionSequence::CompareKind |
| 2194 | compareStandardConversionSubsets(ASTContext &Context, |
| 2195 | const StandardConversionSequence& SCS1, |
| 2196 | const StandardConversionSequence& SCS2) { |
| 2197 | ImplicitConversionSequence::CompareKind Result |
| 2198 | = ImplicitConversionSequence::Indistinguishable; |
| 2199 | |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2200 | // the identity conversion sequence is considered to be a subsequence of |
| 2201 | // any non-identity conversion sequence |
| 2202 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2203 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2204 | return ImplicitConversionSequence::Better; |
| 2205 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2206 | return ImplicitConversionSequence::Worse; |
| 2207 | } |
| 2208 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2209 | if (SCS1.Second != SCS2.Second) { |
| 2210 | if (SCS1.Second == ICK_Identity) |
| 2211 | Result = ImplicitConversionSequence::Better; |
| 2212 | else if (SCS2.Second == ICK_Identity) |
| 2213 | Result = ImplicitConversionSequence::Worse; |
| 2214 | else |
| 2215 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2216 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2217 | return ImplicitConversionSequence::Indistinguishable; |
| 2218 | |
| 2219 | if (SCS1.Third == SCS2.Third) { |
| 2220 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2221 | : ImplicitConversionSequence::Indistinguishable; |
| 2222 | } |
| 2223 | |
| 2224 | if (SCS1.Third == ICK_Identity) |
| 2225 | return Result == ImplicitConversionSequence::Worse |
| 2226 | ? ImplicitConversionSequence::Indistinguishable |
| 2227 | : ImplicitConversionSequence::Better; |
| 2228 | |
| 2229 | if (SCS2.Third == ICK_Identity) |
| 2230 | return Result == ImplicitConversionSequence::Better |
| 2231 | ? ImplicitConversionSequence::Indistinguishable |
| 2232 | : ImplicitConversionSequence::Worse; |
| 2233 | |
| 2234 | return ImplicitConversionSequence::Indistinguishable; |
| 2235 | } |
| 2236 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2237 | /// CompareStandardConversionSequences - Compare two standard |
| 2238 | /// conversion sequences to determine whether one is better than the |
| 2239 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2240 | static ImplicitConversionSequence::CompareKind |
| 2241 | CompareStandardConversionSequences(Sema &S, |
| 2242 | const StandardConversionSequence& SCS1, |
| 2243 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2244 | { |
| 2245 | // Standard conversion sequence S1 is a better conversion sequence |
| 2246 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2247 | |
| 2248 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2249 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2250 | // excluding any Lvalue Transformation; the identity conversion |
| 2251 | // sequence is considered to be a subsequence of any |
| 2252 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2253 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2254 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2255 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2256 | |
| 2257 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2258 | // defined below), or, if not that, |
| 2259 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2260 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2261 | if (Rank1 < Rank2) |
| 2262 | return ImplicitConversionSequence::Better; |
| 2263 | else if (Rank2 < Rank1) |
| 2264 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2266 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2267 | // are indistinguishable unless one of the following rules |
| 2268 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2270 | // A conversion that is not a conversion of a pointer, or |
| 2271 | // pointer to member, to bool is better than another conversion |
| 2272 | // that is such a conversion. |
| 2273 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2274 | return SCS2.isPointerConversionToBool() |
| 2275 | ? ImplicitConversionSequence::Better |
| 2276 | : ImplicitConversionSequence::Worse; |
| 2277 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2278 | // C++ [over.ics.rank]p4b2: |
| 2279 | // |
| 2280 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2281 | // conversion of B* to A* is better than conversion of B* to |
| 2282 | // void*, and conversion of A* to void* is better than conversion |
| 2283 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2285 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2287 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2288 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2289 | // Exactly one of the conversion sequences is a conversion to |
| 2290 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2291 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2292 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2293 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2294 | // Neither conversion sequence converts to a void pointer; compare |
| 2295 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2296 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2297 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2298 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2299 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2300 | // Both conversion sequences are conversions to void |
| 2301 | // pointers. Compare the source types to determine if there's an |
| 2302 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2303 | QualType FromType1 = SCS1.getFromType(); |
| 2304 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2305 | |
| 2306 | // Adjust the types we're converting from via the array-to-pointer |
| 2307 | // conversion, if we need to. |
| 2308 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2309 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2310 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2311 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2313 | QualType FromPointee1 |
| 2314 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2315 | QualType FromPointee2 |
| 2316 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2317 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2318 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2319 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2320 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2321 | return ImplicitConversionSequence::Worse; |
| 2322 | |
| 2323 | // Objective-C++: If one interface is more specific than the |
| 2324 | // other, it is the better one. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2325 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2326 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2327 | if (FromIface1 && FromIface1) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2328 | if (S.Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2329 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2330 | else if (S.Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2331 | return ImplicitConversionSequence::Worse; |
| 2332 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2333 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2334 | |
| 2335 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2336 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2338 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2339 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2341 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2342 | // C++0x [over.ics.rank]p3b4: |
| 2343 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2344 | // implicit object parameter of a non-static member function declared |
| 2345 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 2346 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2347 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 2348 | // or if the member function in this case has a ref qualifier. |
| 2349 | // (Of course, we don't have ref qualifiers yet.) |
| 2350 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 2351 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 2352 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2353 | |
| 2354 | // C++ [over.ics.rank]p3b4: |
| 2355 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2356 | // which the references refer are the same type except for |
| 2357 | // top-level cv-qualifiers, and the type to which the reference |
| 2358 | // initialized by S2 refers is more cv-qualified than the type |
| 2359 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2360 | QualType T1 = SCS1.getToType(2); |
| 2361 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2362 | T1 = S.Context.getCanonicalType(T1); |
| 2363 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2364 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2365 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2366 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2367 | if (UnqualT1 == UnqualT2) { |
| 2368 | // If the type is an array type, promote the element qualifiers to the type |
| 2369 | // for comparison. |
| 2370 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2371 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2372 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2373 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2374 | if (T2.isMoreQualifiedThan(T1)) |
| 2375 | return ImplicitConversionSequence::Better; |
| 2376 | else if (T1.isMoreQualifiedThan(T2)) |
| 2377 | return ImplicitConversionSequence::Worse; |
| 2378 | } |
| 2379 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2380 | |
| 2381 | return ImplicitConversionSequence::Indistinguishable; |
| 2382 | } |
| 2383 | |
| 2384 | /// CompareQualificationConversions - Compares two standard conversion |
| 2385 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2387 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2388 | CompareQualificationConversions(Sema &S, |
| 2389 | const StandardConversionSequence& SCS1, |
| 2390 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2391 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2392 | // -- S1 and S2 differ only in their qualification conversion and |
| 2393 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2394 | // cv-qualification signature of type T1 is a proper subset of |
| 2395 | // the cv-qualification signature of type T2, and S1 is not the |
| 2396 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2397 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2398 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2399 | return ImplicitConversionSequence::Indistinguishable; |
| 2400 | |
| 2401 | // FIXME: the example in the standard doesn't use a qualification |
| 2402 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2403 | QualType T1 = SCS1.getToType(2); |
| 2404 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2405 | T1 = S.Context.getCanonicalType(T1); |
| 2406 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2407 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2408 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2409 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2410 | |
| 2411 | // If the types are the same, we won't learn anything by unwrapped |
| 2412 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2413 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2414 | return ImplicitConversionSequence::Indistinguishable; |
| 2415 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2416 | // If the type is an array type, promote the element qualifiers to the type |
| 2417 | // for comparison. |
| 2418 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2419 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2420 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2421 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2422 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2423 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2424 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2425 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2426 | // Within each iteration of the loop, we check the qualifiers to |
| 2427 | // determine if this still looks like a qualification |
| 2428 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2429 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2430 | // until there are no more pointers or pointers-to-members left |
| 2431 | // to unwrap. This essentially mimics what |
| 2432 | // IsQualificationConversion does, but here we're checking for a |
| 2433 | // strict subset of qualifiers. |
| 2434 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2435 | // The qualifiers are the same, so this doesn't tell us anything |
| 2436 | // about how the sequences rank. |
| 2437 | ; |
| 2438 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2439 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2440 | if (Result == ImplicitConversionSequence::Worse) |
| 2441 | // Neither has qualifiers that are a subset of the other's |
| 2442 | // qualifiers. |
| 2443 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2445 | Result = ImplicitConversionSequence::Better; |
| 2446 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2447 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2448 | if (Result == ImplicitConversionSequence::Better) |
| 2449 | // Neither has qualifiers that are a subset of the other's |
| 2450 | // qualifiers. |
| 2451 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2453 | Result = ImplicitConversionSequence::Worse; |
| 2454 | } else { |
| 2455 | // Qualifiers are disjoint. |
| 2456 | return ImplicitConversionSequence::Indistinguishable; |
| 2457 | } |
| 2458 | |
| 2459 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2460 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2461 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2464 | // Check that the winning standard conversion sequence isn't using |
| 2465 | // the deprecated string literal array to pointer conversion. |
| 2466 | switch (Result) { |
| 2467 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2468 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2469 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2470 | break; |
| 2471 | |
| 2472 | case ImplicitConversionSequence::Indistinguishable: |
| 2473 | break; |
| 2474 | |
| 2475 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2476 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2477 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2478 | break; |
| 2479 | } |
| 2480 | |
| 2481 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2484 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2485 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2486 | /// various kinds of derived-to-base conversions (C++ |
| 2487 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2488 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2489 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2490 | CompareDerivedToBaseConversions(Sema &S, |
| 2491 | const StandardConversionSequence& SCS1, |
| 2492 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2493 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2494 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2495 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2496 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2497 | |
| 2498 | // Adjust the types we're converting from via the array-to-pointer |
| 2499 | // conversion, if we need to. |
| 2500 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2501 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2502 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2503 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2504 | |
| 2505 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2506 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 2507 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 2508 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 2509 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2511 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2512 | // |
| 2513 | // If class B is derived directly or indirectly from class A and |
| 2514 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2515 | // |
| 2516 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2517 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2518 | |
| 2519 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2520 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2521 | SCS2.Second == ICK_Pointer_Conversion && |
| 2522 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2523 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2524 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2526 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2528 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2529 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2530 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2531 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2532 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2533 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2534 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2535 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
| 2536 | const ObjCObjectType* ToIface1 = ToPointee1->getAs<ObjCObjectType>(); |
| 2537 | const ObjCObjectType* ToIface2 = ToPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2538 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2539 | // -- 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] | 2540 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2541 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2542 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2543 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2544 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2545 | |
| 2546 | if (ToIface1 && ToIface2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2547 | if (S.Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2548 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2549 | else if (S.Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2550 | return ImplicitConversionSequence::Worse; |
| 2551 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2552 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2553 | |
| 2554 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2555 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2556 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2557 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2558 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2559 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2561 | if (FromIface1 && FromIface2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2562 | if (S.Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2563 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2564 | else if (S.Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2565 | return ImplicitConversionSequence::Worse; |
| 2566 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2567 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2570 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2571 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2572 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2573 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2574 | const MemberPointerType * FromMemPointer1 = |
| 2575 | FromType1->getAs<MemberPointerType>(); |
| 2576 | const MemberPointerType * ToMemPointer1 = |
| 2577 | ToType1->getAs<MemberPointerType>(); |
| 2578 | const MemberPointerType * FromMemPointer2 = |
| 2579 | FromType2->getAs<MemberPointerType>(); |
| 2580 | const MemberPointerType * ToMemPointer2 = |
| 2581 | ToType2->getAs<MemberPointerType>(); |
| 2582 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2583 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2584 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2585 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2586 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2587 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2588 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2589 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2590 | // 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] | 2591 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2592 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2593 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2594 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2595 | return ImplicitConversionSequence::Better; |
| 2596 | } |
| 2597 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2598 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2599 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2600 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2601 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2602 | return ImplicitConversionSequence::Worse; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2606 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2607 | // -- 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] | 2608 | // -- binding of an expression of type C to a reference of type |
| 2609 | // B& is better than binding an expression of type C to a |
| 2610 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2611 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2612 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2613 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2614 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2615 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2616 | return ImplicitConversionSequence::Worse; |
| 2617 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2618 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2619 | // -- 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] | 2620 | // -- binding of an expression of type B to a reference of type |
| 2621 | // A& is better than binding an expression of type C to a |
| 2622 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2623 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2624 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2625 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2626 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2627 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2628 | return ImplicitConversionSequence::Worse; |
| 2629 | } |
| 2630 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2631 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2632 | return ImplicitConversionSequence::Indistinguishable; |
| 2633 | } |
| 2634 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2635 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2636 | /// determine whether they are reference-related, |
| 2637 | /// reference-compatible, reference-compatible with added |
| 2638 | /// qualification, or incompatible, for use in C++ initialization by |
| 2639 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2640 | /// type, and the first type (T1) is the pointee type of the reference |
| 2641 | /// type being initialized. |
| 2642 | Sema::ReferenceCompareResult |
| 2643 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2644 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2645 | bool &DerivedToBase, |
| 2646 | bool &ObjCConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2647 | assert(!OrigT1->isReferenceType() && |
| 2648 | "T1 must be the pointee type of the reference type"); |
| 2649 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2650 | |
| 2651 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2652 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2653 | Qualifiers T1Quals, T2Quals; |
| 2654 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2655 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2656 | |
| 2657 | // C++ [dcl.init.ref]p4: |
| 2658 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2659 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2660 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2661 | DerivedToBase = false; |
| 2662 | ObjCConversion = false; |
| 2663 | if (UnqualT1 == UnqualT2) { |
| 2664 | // Nothing to do. |
| 2665 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2666 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2667 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2668 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 2669 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 2670 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 2671 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2672 | else |
| 2673 | return Ref_Incompatible; |
| 2674 | |
| 2675 | // At this point, we know that T1 and T2 are reference-related (at |
| 2676 | // least). |
| 2677 | |
| 2678 | // If the type is an array type, promote the element qualifiers to the type |
| 2679 | // for comparison. |
| 2680 | if (isa<ArrayType>(T1) && T1Quals) |
| 2681 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2682 | if (isa<ArrayType>(T2) && T2Quals) |
| 2683 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2684 | |
| 2685 | // C++ [dcl.init.ref]p4: |
| 2686 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2687 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2688 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2689 | // overload resolution, cases for which cv1 is greater |
| 2690 | // cv-qualification than cv2 are identified as |
| 2691 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2692 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2693 | return Ref_Compatible; |
| 2694 | else if (T1.isMoreQualifiedThan(T2)) |
| 2695 | return Ref_Compatible_With_Added_Qualification; |
| 2696 | else |
| 2697 | return Ref_Related; |
| 2698 | } |
| 2699 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2700 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2701 | /// with DeclType. Return true if something definite is found. |
| 2702 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2703 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 2704 | QualType DeclType, SourceLocation DeclLoc, |
| 2705 | Expr *Init, QualType T2, bool AllowRvalues, |
| 2706 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2707 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 2708 | CXXRecordDecl *T2RecordDecl |
| 2709 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2710 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2711 | QualType ToType |
| 2712 | = AllowRvalues? DeclType->getAs<ReferenceType>()->getPointeeType() |
| 2713 | : DeclType; |
| 2714 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2715 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2716 | const UnresolvedSetImpl *Conversions |
| 2717 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2718 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2719 | E = Conversions->end(); I != E; ++I) { |
| 2720 | NamedDecl *D = *I; |
| 2721 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2722 | if (isa<UsingShadowDecl>(D)) |
| 2723 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2724 | |
| 2725 | FunctionTemplateDecl *ConvTemplate |
| 2726 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2727 | CXXConversionDecl *Conv; |
| 2728 | if (ConvTemplate) |
| 2729 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2730 | else |
| 2731 | Conv = cast<CXXConversionDecl>(D); |
| 2732 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2733 | // If this is an explicit conversion, and we're not allowed to consider |
| 2734 | // explicit conversions, skip it. |
| 2735 | if (!AllowExplicit && Conv->isExplicit()) |
| 2736 | continue; |
| 2737 | |
| 2738 | if (AllowRvalues) { |
| 2739 | bool DerivedToBase = false; |
| 2740 | bool ObjCConversion = false; |
| 2741 | if (!ConvTemplate && |
| 2742 | S.CompareReferenceRelationship(DeclLoc, |
| 2743 | Conv->getConversionType().getNonReferenceType().getUnqualifiedType(), |
| 2744 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| 2745 | DerivedToBase, ObjCConversion) |
| 2746 | == Sema::Ref_Incompatible) |
| 2747 | continue; |
| 2748 | } else { |
| 2749 | // If the conversion function doesn't return a reference type, |
| 2750 | // it can't be considered for this conversion. An rvalue reference |
| 2751 | // is only acceptable if its referencee is a function type. |
| 2752 | |
| 2753 | const ReferenceType *RefType = |
| 2754 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 2755 | if (!RefType || |
| 2756 | (!RefType->isLValueReferenceType() && |
| 2757 | !RefType->getPointeeType()->isFunctionType())) |
| 2758 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2759 | } |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2760 | |
| 2761 | if (ConvTemplate) |
| 2762 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2763 | Init, ToType, CandidateSet); |
| 2764 | else |
| 2765 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2766 | ToType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2770 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2771 | case OR_Success: |
| 2772 | // C++ [over.ics.ref]p1: |
| 2773 | // |
| 2774 | // [...] If the parameter binds directly to the result of |
| 2775 | // applying a conversion function to the argument |
| 2776 | // expression, the implicit conversion sequence is a |
| 2777 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2778 | // second standard conversion sequence either an identity |
| 2779 | // conversion or, if the conversion function returns an |
| 2780 | // entity of a type that is a derived class of the parameter |
| 2781 | // type, a derived-to-base Conversion. |
| 2782 | if (!Best->FinalConversion.DirectBinding) |
| 2783 | return false; |
| 2784 | |
| 2785 | ICS.setUserDefined(); |
| 2786 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2787 | ICS.UserDefined.After = Best->FinalConversion; |
| 2788 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2789 | ICS.UserDefined.EllipsisConversion = false; |
| 2790 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2791 | ICS.UserDefined.After.DirectBinding && |
| 2792 | "Expected a direct reference binding!"); |
| 2793 | return true; |
| 2794 | |
| 2795 | case OR_Ambiguous: |
| 2796 | ICS.setAmbiguous(); |
| 2797 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2798 | Cand != CandidateSet.end(); ++Cand) |
| 2799 | if (Cand->Viable) |
| 2800 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2801 | return true; |
| 2802 | |
| 2803 | case OR_No_Viable_Function: |
| 2804 | case OR_Deleted: |
| 2805 | // There was no suitable conversion, or we found a deleted |
| 2806 | // conversion; continue with other checks. |
| 2807 | return false; |
| 2808 | } |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 2809 | |
| 2810 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2813 | /// \brief Compute an implicit conversion sequence for reference |
| 2814 | /// initialization. |
| 2815 | static ImplicitConversionSequence |
| 2816 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2817 | SourceLocation DeclLoc, |
| 2818 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2819 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2820 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2821 | |
| 2822 | // Most paths end in a failed conversion. |
| 2823 | ImplicitConversionSequence ICS; |
| 2824 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2825 | |
| 2826 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2827 | QualType T2 = Init->getType(); |
| 2828 | |
| 2829 | // If the initializer is the address of an overloaded function, try |
| 2830 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2831 | // type of the resulting function. |
| 2832 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2833 | DeclAccessPair Found; |
| 2834 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2835 | false, Found)) |
| 2836 | T2 = Fn->getType(); |
| 2837 | } |
| 2838 | |
| 2839 | // Compute some basic properties of the types and the initializer. |
| 2840 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2841 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2842 | bool ObjCConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2843 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2844 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2845 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| 2846 | ObjCConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2848 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2849 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2850 | // A reference to type "cv1 T1" is initialized by an expression |
| 2851 | // of type "cv2 T2" as follows: |
| 2852 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2853 | // -- If reference is an lvalue reference and the initializer expression |
| 2854 | // The next bullet point (T1 is a function) is pretty much equivalent to this |
| 2855 | // one, so it's handled here. |
| 2856 | if (!isRValRef || T1->isFunctionType()) { |
| 2857 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2858 | // reference-compatible with "cv2 T2," or |
| 2859 | // |
| 2860 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2861 | if (InitCategory.isLValue() && |
| 2862 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2863 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2864 | // When a parameter of reference type binds directly (8.5.3) |
| 2865 | // to an argument expression, the implicit conversion sequence |
| 2866 | // is the identity conversion, unless the argument expression |
| 2867 | // has a type that is a derived class of the parameter type, |
| 2868 | // in which case the implicit conversion sequence is a |
| 2869 | // derived-to-base Conversion (13.3.3.1). |
| 2870 | ICS.setStandard(); |
| 2871 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2872 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 2873 | : ObjCConversion? ICK_Compatible_Conversion |
| 2874 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2875 | ICS.Standard.Third = ICK_Identity; |
| 2876 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2877 | ICS.Standard.setToType(0, T2); |
| 2878 | ICS.Standard.setToType(1, T1); |
| 2879 | ICS.Standard.setToType(2, T1); |
| 2880 | ICS.Standard.ReferenceBinding = true; |
| 2881 | ICS.Standard.DirectBinding = true; |
| 2882 | ICS.Standard.RRefBinding = isRValRef; |
| 2883 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2884 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2885 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2886 | // derived-to-base conversions is suppressed when we're |
| 2887 | // computing the implicit conversion sequence (C++ |
| 2888 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2889 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2890 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2891 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2892 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2893 | // not reference-related to T2, and can be implicitly |
| 2894 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2895 | // is reference-compatible with "cv3 T3" 92) (this |
| 2896 | // conversion is selected by enumerating the applicable |
| 2897 | // conversion functions (13.3.1.6) and choosing the best |
| 2898 | // one through overload resolution (13.3)), |
| 2899 | if (!SuppressUserConversions && T2->isRecordType() && |
| 2900 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2901 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2902 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 2903 | Init, T2, /*AllowRvalues=*/false, |
| 2904 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2905 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2909 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 2910 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2911 | // shall be an rvalue reference and the initializer expression shall be |
| 2912 | // an rvalue or have a function type. |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2913 | // |
| 2914 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2915 | // point, which is that, due to p2 (which short-circuits reference |
| 2916 | // binding by only attempting a simple conversion for non-direct |
| 2917 | // bindings) and p3's strange wording, we allow a const volatile |
| 2918 | // reference to bind to an rvalue. Hence the check for the presence |
| 2919 | // of "const" rather than checking for "const" being the only |
| 2920 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2921 | // This is also the point where rvalue references and lvalue inits no longer |
| 2922 | // go together. |
| 2923 | if ((!isRValRef && !T1.isConstQualified()) || |
| 2924 | (isRValRef && InitCategory.isLValue())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2925 | return ICS; |
| 2926 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2927 | // -- If T1 is a function type, then |
| 2928 | // -- if T2 is the same type as T1, the reference is bound to the |
| 2929 | // initializer expression lvalue; |
| 2930 | // -- if T2 is a class type and the initializer expression can be |
| 2931 | // implicitly converted to an lvalue of type T1 [...], the |
| 2932 | // reference is bound to the function lvalue that is the result |
| 2933 | // of the conversion; |
| 2934 | // This is the same as for the lvalue case above, so it was handled there. |
| 2935 | // -- otherwise, the program is ill-formed. |
| 2936 | // This is the one difference to the lvalue case. |
| 2937 | if (T1->isFunctionType()) |
| 2938 | return ICS; |
| 2939 | |
| 2940 | // -- Otherwise, if T2 is a class type and |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2941 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2942 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2943 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2944 | // -- T1 is not reference-related to T2 and the initializer |
| 2945 | // expression can be implicitly converted to an rvalue |
| 2946 | // of type "cv3 T3" (this conversion is selected by |
| 2947 | // enumerating the applicable conversion functions |
| 2948 | // (13.3.1.6) and choosing the best one through overload |
| 2949 | // resolution (13.3)), |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2950 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2951 | // then the reference is bound to the initializer |
| 2952 | // expression rvalue in the first case and to the object |
| 2953 | // that is the result of the conversion in the second case |
| 2954 | // (or, in either case, to the appropriate base class |
| 2955 | // subobject of the object). |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2956 | if (T2->isRecordType()) { |
| 2957 | // First case: "cv1 T1" is reference-compatible with "cv2 T2". This is a |
| 2958 | // direct binding in C++0x but not in C++03. |
| 2959 | if (InitCategory.isRValue() && |
| 2960 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2961 | ICS.setStandard(); |
| 2962 | ICS.Standard.First = ICK_Identity; |
| 2963 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 2964 | : ObjCConversion? ICK_Compatible_Conversion |
| 2965 | : ICK_Identity; |
| 2966 | ICS.Standard.Third = ICK_Identity; |
| 2967 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2968 | ICS.Standard.setToType(0, T2); |
| 2969 | ICS.Standard.setToType(1, T1); |
| 2970 | ICS.Standard.setToType(2, T1); |
| 2971 | ICS.Standard.ReferenceBinding = true; |
| 2972 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
| 2973 | ICS.Standard.RRefBinding = isRValRef; |
| 2974 | ICS.Standard.CopyConstructor = 0; |
| 2975 | return ICS; |
| 2976 | } |
| 2977 | |
| 2978 | // Second case: not reference-related. |
| 2979 | if (RefRelationship == Sema::Ref_Incompatible && |
| 2980 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2981 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 2982 | Init, T2, /*AllowRvalues=*/true, |
| 2983 | AllowExplicit)) |
| 2984 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2985 | } |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2986 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2987 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2988 | // initialized from the initializer expression using the |
| 2989 | // rules for a non-reference copy initialization (8.5). The |
| 2990 | // reference is then bound to the temporary. If T1 is |
| 2991 | // reference-related to T2, cv1 must be the same |
| 2992 | // cv-qualification as, or greater cv-qualification than, |
| 2993 | // cv2; otherwise, the program is ill-formed. |
| 2994 | if (RefRelationship == Sema::Ref_Related) { |
| 2995 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2996 | // we would be reference-compatible or reference-compatible with |
| 2997 | // added qualification. But that wasn't the case, so the reference |
| 2998 | // initialization fails. |
| 2999 | return ICS; |
| 3000 | } |
| 3001 | |
| 3002 | // If at least one of the types is a class type, the types are not |
| 3003 | // related, and we aren't allowed any user conversions, the |
| 3004 | // reference binding fails. This case is important for breaking |
| 3005 | // recursion, since TryImplicitConversion below will attempt to |
| 3006 | // create a temporary through the use of a copy constructor. |
| 3007 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3008 | (T1->isRecordType() || T2->isRecordType())) |
| 3009 | return ICS; |
| 3010 | |
| 3011 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3012 | // When a parameter of reference type is not bound directly to |
| 3013 | // an argument expression, the conversion sequence is the one |
| 3014 | // required to convert the argument expression to the |
| 3015 | // underlying type of the reference according to |
| 3016 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3017 | // to copy-initializing a temporary of the underlying type with |
| 3018 | // the argument expression. Any difference in top-level |
| 3019 | // cv-qualification is subsumed by the initialization itself |
| 3020 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3021 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3022 | /*AllowExplicit=*/false, |
| 3023 | /*InOverloadResolution=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3024 | |
| 3025 | // Of course, that's still a reference binding. |
| 3026 | if (ICS.isStandard()) { |
| 3027 | ICS.Standard.ReferenceBinding = true; |
| 3028 | ICS.Standard.RRefBinding = isRValRef; |
| 3029 | } else if (ICS.isUserDefined()) { |
| 3030 | ICS.UserDefined.After.ReferenceBinding = true; |
| 3031 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 3032 | } |
| 3033 | return ICS; |
| 3034 | } |
| 3035 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3036 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3037 | /// ToType from the expression From. Return the implicit conversion |
| 3038 | /// sequence required to pass this argument, which may be a bad |
| 3039 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3040 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3041 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3042 | static ImplicitConversionSequence |
| 3043 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | b7f9e6a | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 3044 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3045 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3046 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3047 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3048 | /*FIXME:*/From->getLocStart(), |
| 3049 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3050 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3051 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3052 | return TryImplicitConversion(S, From, ToType, |
| 3053 | SuppressUserConversions, |
| 3054 | /*AllowExplicit=*/false, |
| 3055 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3058 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3059 | /// parameter of the given member function (@c Method) from the |
| 3060 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3061 | static ImplicitConversionSequence |
| 3062 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
| 3063 | CXXMethodDecl *Method, |
| 3064 | CXXRecordDecl *ActingContext) { |
| 3065 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3066 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3067 | // const volatile object. |
| 3068 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3069 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3070 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3071 | |
| 3072 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3073 | // to exit early. |
| 3074 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3075 | |
| 3076 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3077 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3078 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3079 | FromType = PT->getPointeeType(); |
| 3080 | |
| 3081 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3082 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3083 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3084 | // where X is the class of which the function is a member |
| 3085 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 3086 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3088 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3089 | // reference binding here, that allows class rvalues to bind to |
| 3090 | // non-constant references. |
| 3091 | |
| 3092 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 3093 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3094 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3095 | if (ImplicitParamType.getCVRQualifiers() |
| 3096 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3097 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3098 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3099 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3100 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3101 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Check that we have either the same type or a derived type. It |
| 3104 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3105 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3106 | ImplicitConversionKind SecondKind; |
| 3107 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3108 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3109 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3110 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3111 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3112 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3113 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3114 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3115 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3116 | |
| 3117 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3118 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3119 | ICS.Standard.setAsIdentityConversion(); |
| 3120 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3121 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3122 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3123 | ICS.Standard.ReferenceBinding = true; |
| 3124 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 3125 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3126 | return ICS; |
| 3127 | } |
| 3128 | |
| 3129 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3130 | /// the implicit object parameter for the given Method with the given |
| 3131 | /// expression. |
| 3132 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3133 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 3134 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3135 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3136 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3137 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3138 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3139 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3141 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3142 | FromRecordType = PT->getPointeeType(); |
| 3143 | DestType = Method->getThisType(Context); |
| 3144 | } else { |
| 3145 | FromRecordType = From->getType(); |
| 3146 | DestType = ImplicitParamRecordType; |
| 3147 | } |
| 3148 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3149 | // Note that we always use the true parent context when performing |
| 3150 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | ImplicitConversionSequence ICS |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3152 | = TryObjectArgumentInitialization(*this, From->getType(), Method, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3153 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3154 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3155 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3156 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3157 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3159 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3160 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3162 | if (!Context.hasSameType(From->getType(), DestType)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3163 | ImpCastExprToType(From, DestType, CK_NoOp, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3164 | From->getType()->isPointerType() ? VK_RValue : VK_LValue); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3165 | return false; |
| 3166 | } |
| 3167 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3168 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3169 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3170 | static ImplicitConversionSequence |
| 3171 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3172 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3173 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3174 | // FIXME: Are these flags correct? |
| 3175 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3177 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3181 | /// of the expression From to bool (C++0x [conv]p3). |
| 3182 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3183 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3184 | if (!ICS.isBad()) |
| 3185 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3186 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3187 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3188 | return Diag(From->getSourceRange().getBegin(), |
| 3189 | diag::err_typecheck_bool_condition) |
| 3190 | << From->getType() << From->getSourceRange(); |
| 3191 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3192 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3193 | |
| 3194 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3195 | /// expression From to 'id'. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3196 | static ImplicitConversionSequence |
| 3197 | TryContextuallyConvertToObjCId(Sema &S, Expr *From) { |
| 3198 | QualType Ty = S.Context.getObjCIdType(); |
| 3199 | return TryImplicitConversion(S, From, Ty, |
| 3200 | // FIXME: Are these flags correct? |
| 3201 | /*SuppressUserConversions=*/false, |
| 3202 | /*AllowExplicit=*/true, |
| 3203 | /*InOverloadResolution=*/false); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3204 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3205 | |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3206 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3207 | /// of the expression From to 'id'. |
| 3208 | bool Sema::PerformContextuallyConvertToObjCId(Expr *&From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3209 | QualType Ty = Context.getObjCIdType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3210 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3211 | if (!ICS.isBad()) |
| 3212 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| 3213 | return true; |
| 3214 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3215 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3216 | /// \brief Attempt to convert the given expression to an integral or |
| 3217 | /// enumeration type. |
| 3218 | /// |
| 3219 | /// This routine will attempt to convert an expression of class type to an |
| 3220 | /// integral or enumeration type, if that class type only has a single |
| 3221 | /// conversion to an integral or enumeration type. |
| 3222 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3223 | /// \param Loc The source location of the construct that requires the |
| 3224 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3225 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3226 | /// \param FromE The expression we're converting from. |
| 3227 | /// |
| 3228 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3229 | /// have integral or enumeration type. |
| 3230 | /// |
| 3231 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3232 | /// incomplete class type. |
| 3233 | /// |
| 3234 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3235 | /// explicit conversion function (because no implicit conversion functions |
| 3236 | /// were available). This is a recovery mode. |
| 3237 | /// |
| 3238 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3239 | /// showing which conversion was picked. |
| 3240 | /// |
| 3241 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3242 | /// conversion function that could convert to integral or enumeration type. |
| 3243 | /// |
| 3244 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
| 3245 | /// usable conversion function. |
| 3246 | /// |
| 3247 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3248 | /// function, which may be an extension in this case. |
| 3249 | /// |
| 3250 | /// \returns The expression, converted to an integral or enumeration type if |
| 3251 | /// successful. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3252 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3253 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3254 | const PartialDiagnostic &NotIntDiag, |
| 3255 | const PartialDiagnostic &IncompleteDiag, |
| 3256 | const PartialDiagnostic &ExplicitConvDiag, |
| 3257 | const PartialDiagnostic &ExplicitConvNote, |
| 3258 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3259 | const PartialDiagnostic &AmbigNote, |
| 3260 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3261 | // We can't perform any more checking for type-dependent expressions. |
| 3262 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3263 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3264 | |
| 3265 | // If the expression already has integral or enumeration type, we're golden. |
| 3266 | QualType T = From->getType(); |
| 3267 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3268 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3269 | |
| 3270 | // FIXME: Check for missing '()' if T is a function type? |
| 3271 | |
| 3272 | // If we don't have a class type in C++, there's no way we can get an |
| 3273 | // expression of integral or enumeration type. |
| 3274 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3275 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3276 | Diag(Loc, NotIntDiag) |
| 3277 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3278 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | // We must have a complete class type. |
| 3282 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3283 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3284 | |
| 3285 | // Look for a conversion to an integral or enumeration type. |
| 3286 | UnresolvedSet<4> ViableConversions; |
| 3287 | UnresolvedSet<4> ExplicitConversions; |
| 3288 | const UnresolvedSetImpl *Conversions |
| 3289 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
| 3290 | |
| 3291 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3292 | E = Conversions->end(); |
| 3293 | I != E; |
| 3294 | ++I) { |
| 3295 | if (CXXConversionDecl *Conversion |
| 3296 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3297 | if (Conversion->getConversionType().getNonReferenceType() |
| 3298 | ->isIntegralOrEnumerationType()) { |
| 3299 | if (Conversion->isExplicit()) |
| 3300 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3301 | else |
| 3302 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | switch (ViableConversions.size()) { |
| 3307 | case 0: |
| 3308 | if (ExplicitConversions.size() == 1) { |
| 3309 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3310 | CXXConversionDecl *Conversion |
| 3311 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3312 | |
| 3313 | // The user probably meant to invoke the given explicit |
| 3314 | // conversion; use it. |
| 3315 | QualType ConvTy |
| 3316 | = Conversion->getConversionType().getNonReferenceType(); |
| 3317 | std::string TypeStr; |
| 3318 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
| 3319 | |
| 3320 | Diag(Loc, ExplicitConvDiag) |
| 3321 | << T << ConvTy |
| 3322 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3323 | "static_cast<" + TypeStr + ">(") |
| 3324 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3325 | ")"); |
| 3326 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3327 | << ConvTy->isEnumeralType() << ConvTy; |
| 3328 | |
| 3329 | // If we aren't in a SFINAE context, build a call to the |
| 3330 | // explicit conversion function. |
| 3331 | if (isSFINAEContext()) |
| 3332 | return ExprError(); |
| 3333 | |
| 3334 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3335 | From = BuildCXXMemberCallExpr(From, Found, Conversion); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | // We'll complain below about a non-integral condition type. |
| 3339 | break; |
| 3340 | |
| 3341 | case 1: { |
| 3342 | // Apply this conversion. |
| 3343 | DeclAccessPair Found = ViableConversions[0]; |
| 3344 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3345 | |
| 3346 | CXXConversionDecl *Conversion |
| 3347 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3348 | QualType ConvTy |
| 3349 | = Conversion->getConversionType().getNonReferenceType(); |
| 3350 | if (ConvDiag.getDiagID()) { |
| 3351 | if (isSFINAEContext()) |
| 3352 | return ExprError(); |
| 3353 | |
| 3354 | Diag(Loc, ConvDiag) |
| 3355 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3356 | } |
| 3357 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3358 | From = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3359 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3360 | break; |
| 3361 | } |
| 3362 | |
| 3363 | default: |
| 3364 | Diag(Loc, AmbigDiag) |
| 3365 | << T << From->getSourceRange(); |
| 3366 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3367 | CXXConversionDecl *Conv |
| 3368 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3369 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3370 | Diag(Conv->getLocation(), AmbigNote) |
| 3371 | << ConvTy->isEnumeralType() << ConvTy; |
| 3372 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3373 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3376 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3377 | Diag(Loc, NotIntDiag) |
| 3378 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3379 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3380 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3383 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3384 | /// candidate functions, using the given function call arguments. If |
| 3385 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3386 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3387 | /// |
| 3388 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3389 | /// based on an incomplete set of function arguments. This feature is used by |
| 3390 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | void |
| 3392 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3393 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3394 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3395 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3396 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3397 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3398 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3399 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3400 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3402 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3404 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3405 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3406 | // If we get here, it's because we're calling a member function |
| 3407 | // that is named without a member access expression (e.g., |
| 3408 | // "this->f") that was either written explicitly or created |
| 3409 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3410 | // function, e.g., X::f(). We use an empty type for the implied |
| 3411 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3412 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3413 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3414 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3415 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3416 | return; |
| 3417 | } |
| 3418 | // We treat a constructor like a non-member function, since its object |
| 3419 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3422 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3423 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3424 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3425 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3426 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3427 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3428 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3429 | // C++ [class.copy]p3: |
| 3430 | // A member function template is never instantiated to perform the copy |
| 3431 | // of a class object to an object of its class type. |
| 3432 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 3433 | if (NumArgs == 1 && |
| 3434 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3435 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3436 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3437 | return; |
| 3438 | } |
| 3439 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3440 | // Add this candidate |
| 3441 | CandidateSet.push_back(OverloadCandidate()); |
| 3442 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3443 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3444 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3445 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3446 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3447 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3448 | |
| 3449 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3450 | |
| 3451 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3452 | // parameters is viable only if it has an ellipsis in its parameter |
| 3453 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3454 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 3455 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3456 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3457 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3458 | return; |
| 3459 | } |
| 3460 | |
| 3461 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3462 | // is viable only if the (m+1)st parameter has a default argument |
| 3463 | // (8.3.6). For the purposes of overload resolution, the |
| 3464 | // parameter list is truncated on the right, so that there are |
| 3465 | // exactly m parameters. |
| 3466 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3467 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3468 | // Not enough arguments. |
| 3469 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3470 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3471 | return; |
| 3472 | } |
| 3473 | |
| 3474 | // Determine the implicit conversion sequences for each of the |
| 3475 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3476 | Candidate.Conversions.resize(NumArgs); |
| 3477 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3478 | if (ArgIdx < NumArgsInProto) { |
| 3479 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3480 | // exist for each argument an implicit conversion sequence |
| 3481 | // (13.3.3.1) that converts that argument to the corresponding |
| 3482 | // parameter of F. |
| 3483 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3484 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3485 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3486 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3487 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3488 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3489 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3490 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3491 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3492 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3493 | } else { |
| 3494 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3495 | // argument for which there is no corresponding parameter is |
| 3496 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3497 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3502 | /// \brief Add all of the function declarations in the given function set to |
| 3503 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3504 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3505 | Expr **Args, unsigned NumArgs, |
| 3506 | OverloadCandidateSet& CandidateSet, |
| 3507 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3508 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3509 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3510 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3511 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3512 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3513 | cast<CXXMethodDecl>(FD)->getParent(), |
| 3514 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3515 | CandidateSet, SuppressUserConversions); |
| 3516 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3517 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3518 | SuppressUserConversions); |
| 3519 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3520 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3521 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3522 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3523 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3524 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3525 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3526 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3527 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3528 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3529 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3530 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3531 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3532 | Args, NumArgs, CandidateSet, |
| 3533 | SuppressUserConversions); |
| 3534 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3535 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3538 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3539 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3540 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3541 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3542 | Expr **Args, unsigned NumArgs, |
| 3543 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3544 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3545 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3546 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3547 | |
| 3548 | if (isa<UsingShadowDecl>(Decl)) |
| 3549 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 3550 | |
| 3551 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3552 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3553 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3554 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3555 | /*ExplicitArgs*/ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3556 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3557 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3558 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3559 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3560 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3561 | ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3562 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3566 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3567 | /// of candidate functions, using the given function call arguments |
| 3568 | /// and the object argument (@c Object). For example, in a call |
| 3569 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 3570 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 3571 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3572 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3574 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3575 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 3576 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3577 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3578 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3580 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3581 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3582 | assert(!isa<CXXConstructorDecl>(Method) && |
| 3583 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3585 | if (!CandidateSet.isNewCandidate(Method)) |
| 3586 | return; |
| 3587 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3588 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3589 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3590 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3591 | // Add this candidate |
| 3592 | CandidateSet.push_back(OverloadCandidate()); |
| 3593 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3594 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3595 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3596 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3597 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3598 | |
| 3599 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3600 | |
| 3601 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3602 | // parameters is viable only if it has an ellipsis in its parameter |
| 3603 | // list (8.3.5). |
| 3604 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3605 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3606 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3607 | return; |
| 3608 | } |
| 3609 | |
| 3610 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3611 | // is viable only if the (m+1)st parameter has a default argument |
| 3612 | // (8.3.6). For the purposes of overload resolution, the |
| 3613 | // parameter list is truncated on the right, so that there are |
| 3614 | // exactly m parameters. |
| 3615 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 3616 | if (NumArgs < MinRequiredArgs) { |
| 3617 | // Not enough arguments. |
| 3618 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3619 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3620 | return; |
| 3621 | } |
| 3622 | |
| 3623 | Candidate.Viable = true; |
| 3624 | Candidate.Conversions.resize(NumArgs + 1); |
| 3625 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3626 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3627 | // The implicit object argument is ignored. |
| 3628 | Candidate.IgnoreObjectArgument = true; |
| 3629 | else { |
| 3630 | // Determine the implicit conversion sequence for the object |
| 3631 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3632 | Candidate.Conversions[0] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3633 | = TryObjectArgumentInitialization(*this, ObjectType, Method, |
| 3634 | ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3635 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3636 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3637 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3638 | return; |
| 3639 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | // Determine the implicit conversion sequences for each of the |
| 3643 | // arguments. |
| 3644 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3645 | if (ArgIdx < NumArgsInProto) { |
| 3646 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3647 | // exist for each argument an implicit conversion sequence |
| 3648 | // (13.3.3.1) that converts that argument to the corresponding |
| 3649 | // parameter of F. |
| 3650 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3652 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3653 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3654 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3655 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3656 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3657 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3658 | break; |
| 3659 | } |
| 3660 | } else { |
| 3661 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3662 | // argument for which there is no corresponding parameter is |
| 3663 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3664 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3669 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 3670 | /// set, using template argument deduction to produce an appropriate member |
| 3671 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3673 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3674 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3675 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3676 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3677 | QualType ObjectType, |
| 3678 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3679 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3680 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3681 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3682 | return; |
| 3683 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3684 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3686 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | // 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] | 3688 | // candidate functions in the usual way.113) A given name can refer to one |
| 3689 | // or more function templates and also to a set of overloaded non-template |
| 3690 | // functions. In such a case, the candidate functions generated from each |
| 3691 | // function template are combined with the set of non-template candidate |
| 3692 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3693 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3694 | FunctionDecl *Specialization = 0; |
| 3695 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3696 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3697 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3698 | CandidateSet.push_back(OverloadCandidate()); |
| 3699 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3700 | Candidate.FoundDecl = FoundDecl; |
| 3701 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 3702 | Candidate.Viable = false; |
| 3703 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3704 | Candidate.IsSurrogate = false; |
| 3705 | Candidate.IgnoreObjectArgument = false; |
| 3706 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3707 | Info); |
| 3708 | return; |
| 3709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3711 | // Add the function template specialization produced by template argument |
| 3712 | // deduction as a candidate. |
| 3713 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3715 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3716 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3717 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3718 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3721 | /// \brief Add a C++ function template specialization as a candidate |
| 3722 | /// in the candidate set, using template argument deduction to produce |
| 3723 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3724 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3725 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3726 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3727 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3728 | Expr **Args, unsigned NumArgs, |
| 3729 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3730 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3731 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3732 | return; |
| 3733 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3734 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3736 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3737 | // 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] | 3738 | // candidate functions in the usual way.113) A given name can refer to one |
| 3739 | // or more function templates and also to a set of overloaded non-template |
| 3740 | // functions. In such a case, the candidate functions generated from each |
| 3741 | // function template are combined with the set of non-template candidate |
| 3742 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3743 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3744 | FunctionDecl *Specialization = 0; |
| 3745 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3746 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3747 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3748 | CandidateSet.push_back(OverloadCandidate()); |
| 3749 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3750 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3751 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3752 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3753 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3754 | Candidate.IsSurrogate = false; |
| 3755 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3756 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3757 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3758 | return; |
| 3759 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3761 | // Add the function template specialization produced by template argument |
| 3762 | // deduction as a candidate. |
| 3763 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3764 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3765 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3767 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3768 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3770 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3771 | /// 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] | 3772 | /// (which may or may not be the same type as the type that the |
| 3773 | /// conversion function produces). |
| 3774 | void |
| 3775 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3776 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3777 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3778 | Expr *From, QualType ToType, |
| 3779 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3780 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3781 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3782 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3783 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3784 | return; |
| 3785 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3786 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3787 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3788 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3789 | // Add this candidate |
| 3790 | CandidateSet.push_back(OverloadCandidate()); |
| 3791 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3792 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3793 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3794 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3795 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3796 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3797 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3798 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3799 | Candidate.Viable = true; |
| 3800 | Candidate.Conversions.resize(1); |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 3802 | // C++ [over.match.funcs]p4: |
| 3803 | // For conversion functions, the function is considered to be a member of |
| 3804 | // the class of the implicit implied object argument for the purpose of |
| 3805 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3806 | // |
| 3807 | // Determine the implicit conversion sequence for the implicit |
| 3808 | // object parameter. |
| 3809 | QualType ImplicitParamType = From->getType(); |
| 3810 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 3811 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 3812 | CXXRecordDecl *ConversionContext |
| 3813 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
| 3814 | |
| 3815 | Candidate.Conversions[0] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3816 | = TryObjectArgumentInitialization(*this, From->getType(), Conversion, |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3817 | ConversionContext); |
| 3818 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3819 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3820 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3821 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3822 | return; |
| 3823 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3824 | |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3825 | // We won't go through a user-define type conversion function to convert a |
| 3826 | // derived to base as such conversions are given Conversion Rank. They only |
| 3827 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3828 | QualType FromCanon |
| 3829 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3830 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3831 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3832 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3833 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3834 | return; |
| 3835 | } |
| 3836 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3837 | // To determine what the conversion from the result of calling the |
| 3838 | // conversion function to the type we're eventually trying to |
| 3839 | // convert to (ToType), we need to synthesize a call to the |
| 3840 | // conversion function and attempt copy initialization from it. This |
| 3841 | // makes sure that we get the right semantics with respect to |
| 3842 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3843 | // call on the stack and we don't need its arguments to be |
| 3844 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3846 | From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3847 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 3848 | Context.getPointerType(Conversion->getType()), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3849 | CK_FunctionToPointerDecay, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3850 | &ConversionRef, VK_RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | |
| 3852 | // 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] | 3853 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3854 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3856 | Conversion->getConversionType().getNonLValueExprType(Context), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3857 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3859 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3860 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3861 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3863 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3864 | case ImplicitConversionSequence::StandardConversion: |
| 3865 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3866 | |
| 3867 | // C++ [over.ics.user]p3: |
| 3868 | // If the user-defined conversion is specified by a specialization of a |
| 3869 | // conversion function template, the second standard conversion sequence |
| 3870 | // shall have exact match rank. |
| 3871 | if (Conversion->getPrimaryTemplate() && |
| 3872 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3873 | Candidate.Viable = false; |
| 3874 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3875 | } |
| 3876 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3877 | break; |
| 3878 | |
| 3879 | case ImplicitConversionSequence::BadConversion: |
| 3880 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3881 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3882 | break; |
| 3883 | |
| 3884 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3886 | "Can only end up with a standard conversion sequence or failure"); |
| 3887 | } |
| 3888 | } |
| 3889 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3890 | /// \brief Adds a conversion function template specialization |
| 3891 | /// candidate to the overload set, using template argument deduction |
| 3892 | /// to deduce the template arguments of the conversion function |
| 3893 | /// template from the type that we are converting to (C++ |
| 3894 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3896 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3897 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3898 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3899 | Expr *From, QualType ToType, |
| 3900 | OverloadCandidateSet &CandidateSet) { |
| 3901 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3902 | "Only conversion function templates permitted here"); |
| 3903 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3904 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3905 | return; |
| 3906 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3907 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3908 | CXXConversionDecl *Specialization = 0; |
| 3909 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3911 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3912 | CandidateSet.push_back(OverloadCandidate()); |
| 3913 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3914 | Candidate.FoundDecl = FoundDecl; |
| 3915 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3916 | Candidate.Viable = false; |
| 3917 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3918 | Candidate.IsSurrogate = false; |
| 3919 | Candidate.IgnoreObjectArgument = false; |
| 3920 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3921 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3922 | return; |
| 3923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3925 | // Add the conversion function template specialization produced by |
| 3926 | // template argument deduction as a candidate. |
| 3927 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3928 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3929 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3932 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3933 | /// converts the given @c Object to a function pointer via the |
| 3934 | /// conversion function @c Conversion, and then attempts to call it |
| 3935 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3936 | /// the type of function that we'll eventually be calling. |
| 3937 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3938 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3939 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3940 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3941 | QualType ObjectType, |
| 3942 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3943 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3944 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3945 | return; |
| 3946 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3947 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3948 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3950 | CandidateSet.push_back(OverloadCandidate()); |
| 3951 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3952 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3953 | Candidate.Function = 0; |
| 3954 | Candidate.Surrogate = Conversion; |
| 3955 | Candidate.Viable = true; |
| 3956 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3957 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3958 | Candidate.Conversions.resize(NumArgs + 1); |
| 3959 | |
| 3960 | // Determine the implicit conversion sequence for the implicit |
| 3961 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | ImplicitConversionSequence ObjectInit |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3963 | = TryObjectArgumentInitialization(*this, ObjectType, Conversion, |
| 3964 | ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3965 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3966 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3967 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3968 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3969 | return; |
| 3970 | } |
| 3971 | |
| 3972 | // The first conversion is actually a user-defined conversion whose |
| 3973 | // first conversion is ObjectInit's standard conversion (which is |
| 3974 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3975 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3976 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3977 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3978 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3980 | = Candidate.Conversions[0].UserDefined.Before; |
| 3981 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3982 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3984 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3985 | |
| 3986 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3987 | // parameters is viable only if it has an ellipsis in its parameter |
| 3988 | // list (8.3.5). |
| 3989 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3990 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3991 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3992 | return; |
| 3993 | } |
| 3994 | |
| 3995 | // Function types don't have any default arguments, so just check if |
| 3996 | // we have enough arguments. |
| 3997 | if (NumArgs < NumArgsInProto) { |
| 3998 | // Not enough arguments. |
| 3999 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4000 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4001 | return; |
| 4002 | } |
| 4003 | |
| 4004 | // Determine the implicit conversion sequences for each of the |
| 4005 | // arguments. |
| 4006 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4007 | if (ArgIdx < NumArgsInProto) { |
| 4008 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4009 | // exist for each argument an implicit conversion sequence |
| 4010 | // (13.3.3.1) that converts that argument to the corresponding |
| 4011 | // parameter of F. |
| 4012 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4013 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4014 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4015 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4016 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4017 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4018 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4019 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4020 | break; |
| 4021 | } |
| 4022 | } else { |
| 4023 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4024 | // argument for which there is no corresponding parameter is |
| 4025 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4026 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4027 | } |
| 4028 | } |
| 4029 | } |
| 4030 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4031 | /// \brief Add overload candidates for overloaded operators that are |
| 4032 | /// member functions. |
| 4033 | /// |
| 4034 | /// Add the overloaded operator candidates that are member functions |
| 4035 | /// for the operator Op that was used in an operator expression such |
| 4036 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4037 | /// CandidateSet will store the added overload candidates. (C++ |
| 4038 | /// [over.match.oper]). |
| 4039 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4040 | SourceLocation OpLoc, |
| 4041 | Expr **Args, unsigned NumArgs, |
| 4042 | OverloadCandidateSet& CandidateSet, |
| 4043 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4044 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4045 | |
| 4046 | // C++ [over.match.oper]p3: |
| 4047 | // For a unary operator @ with an operand of a type whose |
| 4048 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4049 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4050 | // a right operand of a type whose cv-unqualified version is T2, |
| 4051 | // three sets of candidate functions, designated member |
| 4052 | // candidates, non-member candidates and built-in candidates, are |
| 4053 | // constructed as follows: |
| 4054 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4055 | |
| 4056 | // -- If T1 is a class type, the set of member candidates is the |
| 4057 | // result of the qualified lookup of T1::operator@ |
| 4058 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4059 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4060 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4061 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4062 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4063 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4065 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4066 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4067 | Operators.suppressDiagnostics(); |
| 4068 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4070 | OperEnd = Operators.end(); |
| 4071 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4072 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4073 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4074 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4075 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4076 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4079 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4080 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4081 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4082 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4083 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4084 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4085 | /// (at the beginning of the argument list) that will be contextually |
| 4086 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4088 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4089 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4090 | bool IsAssignmentOperator, |
| 4091 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4092 | // Overload resolution is always an unevaluated context. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4093 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4095 | // Add this candidate |
| 4096 | CandidateSet.push_back(OverloadCandidate()); |
| 4097 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4098 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4099 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4100 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4101 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4102 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4103 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4104 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4105 | |
| 4106 | // Determine the implicit conversion sequences for each of the |
| 4107 | // arguments. |
| 4108 | Candidate.Viable = true; |
| 4109 | Candidate.Conversions.resize(NumArgs); |
| 4110 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4111 | // C++ [over.match.oper]p4: |
| 4112 | // For the built-in assignment operators, conversions of the |
| 4113 | // left operand are restricted as follows: |
| 4114 | // -- no temporaries are introduced to hold the left operand, and |
| 4115 | // -- no user-defined conversions are applied to the left |
| 4116 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4118 | // |
| 4119 | // We block these conversions by turning off user-defined |
| 4120 | // conversions, since that is the only way that initialization of |
| 4121 | // a reference to a non-class type can occur from something that |
| 4122 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4123 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4125 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4126 | Candidate.Conversions[ArgIdx] |
| 4127 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4128 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4130 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4131 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4132 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4133 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4134 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4135 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4136 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4137 | break; |
| 4138 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4143 | /// candidate operator functions for built-in operators (C++ |
| 4144 | /// [over.built]). The types are separated into pointer types and |
| 4145 | /// enumeration types. |
| 4146 | class BuiltinCandidateTypeSet { |
| 4147 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4148 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4149 | |
| 4150 | /// PointerTypes - The set of pointer types that will be used in the |
| 4151 | /// built-in candidates. |
| 4152 | TypeSet PointerTypes; |
| 4153 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4154 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4155 | /// used in the built-in candidates. |
| 4156 | TypeSet MemberPointerTypes; |
| 4157 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4158 | /// EnumerationTypes - The set of enumeration types that will be |
| 4159 | /// used in the built-in candidates. |
| 4160 | TypeSet EnumerationTypes; |
| 4161 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4162 | /// \brief The set of vector types that will be used in the built-in |
| 4163 | /// candidates. |
| 4164 | TypeSet VectorTypes; |
| 4165 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4166 | /// Sema - The semantic analysis instance where we are building the |
| 4167 | /// candidate type set. |
| 4168 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4169 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4170 | /// Context - The AST context in which we will build the type sets. |
| 4171 | ASTContext &Context; |
| 4172 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4173 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4174 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4175 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4176 | |
| 4177 | public: |
| 4178 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4179 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4180 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4182 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4184 | void AddTypesConvertedFrom(QualType Ty, |
| 4185 | SourceLocation Loc, |
| 4186 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4187 | bool AllowExplicitConversions, |
| 4188 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4189 | |
| 4190 | /// pointer_begin - First pointer type found; |
| 4191 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4192 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4193 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4194 | iterator pointer_end() { return PointerTypes.end(); } |
| 4195 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4196 | /// member_pointer_begin - First member pointer type found; |
| 4197 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4198 | |
| 4199 | /// member_pointer_end - Past the last member pointer type found; |
| 4200 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4201 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4202 | /// enumeration_begin - First enumeration type found; |
| 4203 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4204 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4205 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4206 | iterator enumeration_end() { return EnumerationTypes.end(); } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4207 | |
| 4208 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4209 | iterator vector_end() { return VectorTypes.end(); } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4210 | }; |
| 4211 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4212 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4213 | /// the set of pointer types along with any more-qualified variants of |
| 4214 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4215 | /// will add "int const *", "int const volatile *", "int const |
| 4216 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4217 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4218 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4219 | /// |
| 4220 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4221 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4222 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4223 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4225 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4226 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4227 | return false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4228 | |
| 4229 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4230 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4231 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4232 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4233 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4234 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4235 | buildObjCPtr = true; |
| 4236 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4237 | else |
| 4238 | assert(false && "type was not a pointer type!"); |
| 4239 | } |
| 4240 | else |
| 4241 | PointeeTy = PointerTy->getPointeeType(); |
| 4242 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4243 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4244 | // (the qualifier would sink to the element type), and for another, the |
| 4245 | // only overload situation where it matters is subscript or pointer +- int, |
| 4246 | // and those shouldn't have qualifier variants anyway. |
| 4247 | if (PointeeTy->isArrayType()) |
| 4248 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4249 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4250 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4251 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4252 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4253 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 4254 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4255 | // Iterate through all strict supersets of BaseCVR. |
| 4256 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4257 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4258 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4259 | // in the types. |
| 4260 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4261 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4262 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4263 | if (!buildObjCPtr) |
| 4264 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 4265 | else |
| 4266 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | return true; |
| 4270 | } |
| 4271 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4272 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4273 | /// to the set of pointer types along with any more-qualified variants of |
| 4274 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4275 | /// will add "int const *", "int const volatile *", "int const |
| 4276 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4277 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4278 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4279 | /// |
| 4280 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4281 | bool |
| 4282 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4283 | QualType Ty) { |
| 4284 | // Insert this type. |
| 4285 | if (!MemberPointerTypes.insert(Ty)) |
| 4286 | return false; |
| 4287 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4288 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4289 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4290 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4291 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4292 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4293 | // (the qualifier would sink to the element type), and for another, the |
| 4294 | // only overload situation where it matters is subscript or pointer +- int, |
| 4295 | // and those shouldn't have qualifier variants anyway. |
| 4296 | if (PointeeTy->isArrayType()) |
| 4297 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4298 | const Type *ClassTy = PointerTy->getClass(); |
| 4299 | |
| 4300 | // Iterate through all strict supersets of the pointee type's CVR |
| 4301 | // qualifiers. |
| 4302 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4303 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4304 | if ((CVR | BaseCVR) != CVR) continue; |
| 4305 | |
| 4306 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 4307 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | return true; |
| 4311 | } |
| 4312 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4313 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4314 | /// 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] | 4315 | /// primarily interested in pointer types and enumeration types. We also |
| 4316 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4317 | /// AllowUserConversions is true if we should look at the conversion |
| 4318 | /// functions of a class type, and AllowExplicitConversions if we |
| 4319 | /// should also include the explicit conversion functions of a class |
| 4320 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4321 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4322 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4323 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4324 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4325 | bool AllowExplicitConversions, |
| 4326 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4327 | // Only deal with canonical types. |
| 4328 | Ty = Context.getCanonicalType(Ty); |
| 4329 | |
| 4330 | // Look through reference types; they aren't part of the type of an |
| 4331 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4332 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4333 | Ty = RefTy->getPointeeType(); |
| 4334 | |
| 4335 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4336 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4337 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 4338 | // If we're dealing with an array type, decay to the pointer. |
| 4339 | if (Ty->isArrayType()) |
| 4340 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4341 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 4342 | PointerTypes.insert(Ty); |
| 4343 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4344 | // Insert our type, and its more-qualified variants, into the set |
| 4345 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4346 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4347 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4348 | } else if (Ty->isMemberPointerType()) { |
| 4349 | // Member pointers are far easier, since the pointee can't be converted. |
| 4350 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4351 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4352 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4353 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4354 | } else if (Ty->isVectorType()) { |
| 4355 | VectorTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4356 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4357 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4358 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4359 | // No conversion functions in incomplete types. |
| 4360 | return; |
| 4361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4363 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4364 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 4365 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4366 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4367 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4368 | NamedDecl *D = I.getDecl(); |
| 4369 | if (isa<UsingShadowDecl>(D)) |
| 4370 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4371 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4373 | // about which builtin types we can convert to. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4374 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4375 | continue; |
| 4376 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4377 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4378 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4379 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4380 | VisibleQuals); |
| 4381 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4382 | } |
| 4383 | } |
| 4384 | } |
| 4385 | } |
| 4386 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4387 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4388 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4389 | /// given type to the candidate set. |
| 4390 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4391 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4392 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4393 | unsigned NumArgs, |
| 4394 | OverloadCandidateSet &CandidateSet) { |
| 4395 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4396 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4397 | // T& operator=(T&, T) |
| 4398 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4399 | ParamTypes[1] = T; |
| 4400 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4401 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4402 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4403 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4404 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4405 | ParamTypes[0] |
| 4406 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4407 | ParamTypes[1] = T; |
| 4408 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4412 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4413 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4414 | /// 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] | 4415 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4416 | Qualifiers VRQuals; |
| 4417 | const RecordType *TyRec; |
| 4418 | if (const MemberPointerType *RHSMPType = |
| 4419 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4420 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4421 | else |
| 4422 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4423 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4424 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4425 | VRQuals.addVolatile(); |
| 4426 | VRQuals.addRestrict(); |
| 4427 | return VRQuals; |
| 4428 | } |
| 4429 | |
| 4430 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4431 | if (!ClassDecl->hasDefinition()) |
| 4432 | return VRQuals; |
| 4433 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4434 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4435 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4436 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4437 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4438 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4439 | NamedDecl *D = I.getDecl(); |
| 4440 | if (isa<UsingShadowDecl>(D)) |
| 4441 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4442 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4443 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4444 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4445 | CanTy = ResTypeRef->getPointeeType(); |
| 4446 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4447 | // as see them. |
| 4448 | bool done = false; |
| 4449 | while (!done) { |
| 4450 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4451 | CanTy = ResTypePtr->getPointeeType(); |
| 4452 | else if (const MemberPointerType *ResTypeMPtr = |
| 4453 | CanTy->getAs<MemberPointerType>()) |
| 4454 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4455 | else |
| 4456 | done = true; |
| 4457 | if (CanTy.isVolatileQualified()) |
| 4458 | VRQuals.addVolatile(); |
| 4459 | if (CanTy.isRestrictQualified()) |
| 4460 | VRQuals.addRestrict(); |
| 4461 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4462 | return VRQuals; |
| 4463 | } |
| 4464 | } |
| 4465 | } |
| 4466 | return VRQuals; |
| 4467 | } |
| 4468 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4469 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 4470 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 4471 | /// on the operator @p Op and the arguments given. For example, if the |
| 4472 | /// operator is a binary '+', this routine might add "int |
| 4473 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4474 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4476 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4477 | Expr **Args, unsigned NumArgs, |
| 4478 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4479 | // The set of "promoted arithmetic types", which are the arithmetic |
| 4480 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 4481 | // that the first few of these types are the promoted integral |
| 4482 | // types; these types need to be first. |
| 4483 | // FIXME: What about complex? |
| 4484 | const unsigned FirstIntegralType = 0; |
| 4485 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4486 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4487 | LastPromotedIntegralType = 13; |
| 4488 | const unsigned FirstPromotedArithmeticType = 7, |
| 4489 | LastPromotedArithmeticType = 16; |
| 4490 | const unsigned NumArithmeticTypes = 16; |
| 4491 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 4493 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4494 | Context.SignedCharTy, Context.ShortTy, |
| 4495 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 4496 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 4497 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 4498 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 4499 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 4500 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 4501 | "Invalid first promoted integral type"); |
| 4502 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 4503 | == Context.UnsignedLongLongTy && |
| 4504 | "Invalid last promoted integral type"); |
| 4505 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 4506 | "Invalid first promoted arithmetic type"); |
| 4507 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 4508 | == Context.LongDoubleTy && |
| 4509 | "Invalid last promoted arithmetic type"); |
| 4510 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4511 | // Find all of the types that the arguments can convert to, but only |
| 4512 | // if the operator we're looking at has built-in operator candidates |
| 4513 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4514 | Qualifiers VisibleTypeConversionsQuals; |
| 4515 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4516 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4517 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 4518 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4519 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4520 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4521 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 4522 | OpLoc, |
| 4523 | true, |
| 4524 | (Op == OO_Exclaim || |
| 4525 | Op == OO_AmpAmp || |
| 4526 | Op == OO_PipePipe), |
| 4527 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4528 | |
| 4529 | bool isComparison = false; |
| 4530 | switch (Op) { |
| 4531 | case OO_None: |
| 4532 | case NUM_OVERLOADED_OPERATORS: |
| 4533 | assert(false && "Expected an overloaded operator"); |
| 4534 | break; |
| 4535 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4536 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4538 | goto UnaryStar; |
| 4539 | else |
| 4540 | goto BinaryStar; |
| 4541 | break; |
| 4542 | |
| 4543 | case OO_Plus: // '+' is either unary or binary |
| 4544 | if (NumArgs == 1) |
| 4545 | goto UnaryPlus; |
| 4546 | else |
| 4547 | goto BinaryPlus; |
| 4548 | break; |
| 4549 | |
| 4550 | case OO_Minus: // '-' is either unary or binary |
| 4551 | if (NumArgs == 1) |
| 4552 | goto UnaryMinus; |
| 4553 | else |
| 4554 | goto BinaryMinus; |
| 4555 | break; |
| 4556 | |
| 4557 | case OO_Amp: // '&' is either unary or binary |
| 4558 | if (NumArgs == 1) |
| 4559 | goto UnaryAmp; |
| 4560 | else |
| 4561 | goto BinaryAmp; |
| 4562 | |
| 4563 | case OO_PlusPlus: |
| 4564 | case OO_MinusMinus: |
| 4565 | // C++ [over.built]p3: |
| 4566 | // |
| 4567 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 4568 | // is either volatile or empty, there exist candidate operator |
| 4569 | // functions of the form |
| 4570 | // |
| 4571 | // VQ T& operator++(VQ T&); |
| 4572 | // T operator++(VQ T&, int); |
| 4573 | // |
| 4574 | // C++ [over.built]p4: |
| 4575 | // |
| 4576 | // For every pair (T, VQ), where T is an arithmetic type other |
| 4577 | // than bool, and VQ is either volatile or empty, there exist |
| 4578 | // candidate operator functions of the form |
| 4579 | // |
| 4580 | // VQ T& operator--(VQ T&); |
| 4581 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4582 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4583 | Arith < NumArithmeticTypes; ++Arith) { |
| 4584 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4585 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4586 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4587 | |
| 4588 | // Non-volatile version. |
| 4589 | if (NumArgs == 1) |
| 4590 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4591 | else |
| 4592 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4593 | // heuristic to reduce number of builtin candidates in the set. |
| 4594 | // Add volatile version only if there are conversions to a volatile type. |
| 4595 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4596 | // Volatile version |
| 4597 | ParamTypes[0] |
| 4598 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 4599 | if (NumArgs == 1) |
| 4600 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4601 | else |
| 4602 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 4603 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | // C++ [over.built]p5: |
| 4607 | // |
| 4608 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4609 | // cv-unqualified object type, and VQ is either volatile or |
| 4610 | // empty, there exist candidate operator functions of the form |
| 4611 | // |
| 4612 | // T*VQ& operator++(T*VQ&); |
| 4613 | // T*VQ& operator--(T*VQ&); |
| 4614 | // T* operator++(T*VQ&, int); |
| 4615 | // T* operator--(T*VQ&, int); |
| 4616 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4617 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4618 | // Skip pointer types that aren't pointers to object types. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4619 | if (!(*Ptr)->getPointeeType()->isIncompleteOrObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4620 | continue; |
| 4621 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | QualType ParamTypes[2] = { |
| 4623 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4624 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4626 | // Without volatile |
| 4627 | if (NumArgs == 1) |
| 4628 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4629 | else |
| 4630 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4631 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4632 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4633 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4634 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4635 | ParamTypes[0] |
| 4636 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4637 | if (NumArgs == 1) |
| 4638 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4639 | else |
| 4640 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4641 | } |
| 4642 | } |
| 4643 | break; |
| 4644 | |
| 4645 | UnaryStar: |
| 4646 | // C++ [over.built]p6: |
| 4647 | // For every cv-qualified or cv-unqualified object type T, there |
| 4648 | // exist candidate operator functions of the form |
| 4649 | // |
| 4650 | // T& operator*(T*); |
| 4651 | // |
| 4652 | // C++ [over.built]p7: |
| 4653 | // For every function type T, there exist candidate operator |
| 4654 | // functions of the form |
| 4655 | // T& operator*(T*); |
| 4656 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4657 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4658 | QualType ParamTy = *Ptr; |
Argyrios Kyrtzidis | 42d0f2a | 2010-08-23 07:12:16 +0000 | [diff] [blame] | 4659 | QualType PointeeTy = ParamTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4661 | &ParamTy, Args, 1, CandidateSet); |
| 4662 | } |
| 4663 | break; |
| 4664 | |
| 4665 | UnaryPlus: |
| 4666 | // C++ [over.built]p8: |
| 4667 | // For every type T, there exist candidate operator functions of |
| 4668 | // the form |
| 4669 | // |
| 4670 | // T* operator+(T*); |
| 4671 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4672 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4673 | QualType ParamTy = *Ptr; |
| 4674 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 4675 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4676 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4677 | // Fall through |
| 4678 | |
| 4679 | UnaryMinus: |
| 4680 | // C++ [over.built]p9: |
| 4681 | // For every promoted arithmetic type T, there exist candidate |
| 4682 | // operator functions of the form |
| 4683 | // |
| 4684 | // T operator+(T); |
| 4685 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4686 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4687 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 4688 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 4689 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 4690 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4691 | |
| 4692 | // Extension: We also add these operators for vector types. |
| 4693 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4694 | VecEnd = CandidateTypes.vector_end(); |
| 4695 | Vec != VecEnd; ++Vec) { |
| 4696 | QualType VecTy = *Vec; |
| 4697 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4698 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4699 | break; |
| 4700 | |
| 4701 | case OO_Tilde: |
| 4702 | // C++ [over.built]p10: |
| 4703 | // For every promoted integral type T, there exist candidate |
| 4704 | // operator functions of the form |
| 4705 | // |
| 4706 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4708 | Int < LastPromotedIntegralType; ++Int) { |
| 4709 | QualType IntTy = ArithmeticTypes[Int]; |
| 4710 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 4711 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4712 | |
| 4713 | // Extension: We also add this operator for vector types. |
| 4714 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4715 | VecEnd = CandidateTypes.vector_end(); |
| 4716 | Vec != VecEnd; ++Vec) { |
| 4717 | QualType VecTy = *Vec; |
| 4718 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4719 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4720 | break; |
| 4721 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4722 | case OO_New: |
| 4723 | case OO_Delete: |
| 4724 | case OO_Array_New: |
| 4725 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4726 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4727 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4728 | break; |
| 4729 | |
| 4730 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4731 | UnaryAmp: |
| 4732 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4733 | // C++ [over.match.oper]p3: |
| 4734 | // -- For the operator ',', the unary operator '&', or the |
| 4735 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4736 | break; |
| 4737 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4738 | case OO_EqualEqual: |
| 4739 | case OO_ExclaimEqual: |
| 4740 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | // For every pointer to member type T, there exist candidate operator |
| 4742 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4743 | // |
| 4744 | // bool operator==(T,T); |
| 4745 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4746 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4747 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4748 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4749 | MemPtr != MemPtrEnd; |
| 4750 | ++MemPtr) { |
| 4751 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4752 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4753 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4755 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4757 | case OO_Less: |
| 4758 | case OO_Greater: |
| 4759 | case OO_LessEqual: |
| 4760 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4761 | // C++ [over.built]p15: |
| 4762 | // |
| 4763 | // For every pointer or enumeration type T, there exist |
| 4764 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4765 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4766 | // bool operator<(T, T); |
| 4767 | // bool operator>(T, T); |
| 4768 | // bool operator<=(T, T); |
| 4769 | // bool operator>=(T, T); |
| 4770 | // bool operator==(T, T); |
| 4771 | // bool operator!=(T, T); |
| 4772 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4773 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4774 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4775 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4776 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4777 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4778 | = CandidateTypes.enumeration_begin(); |
| 4779 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4780 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4781 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4782 | } |
| 4783 | |
| 4784 | // Fall through. |
| 4785 | isComparison = true; |
| 4786 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4787 | BinaryPlus: |
| 4788 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4789 | if (!isComparison) { |
| 4790 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4791 | |
| 4792 | // C++ [over.built]p13: |
| 4793 | // |
| 4794 | // For every cv-qualified or cv-unqualified object type T |
| 4795 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4797 | // T* operator+(T*, ptrdiff_t); |
| 4798 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4799 | // T* operator-(T*, ptrdiff_t); |
| 4800 | // T* operator+(ptrdiff_t, T*); |
| 4801 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4802 | // |
| 4803 | // C++ [over.built]p14: |
| 4804 | // |
| 4805 | // For every T, where T is a pointer to object type, there |
| 4806 | // exist candidate operator functions of the form |
| 4807 | // |
| 4808 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4810 | = CandidateTypes.pointer_begin(); |
| 4811 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4812 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4813 | |
| 4814 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4815 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4816 | |
| 4817 | if (Op == OO_Plus) { |
| 4818 | // T* operator+(ptrdiff_t, T*); |
| 4819 | ParamTypes[0] = ParamTypes[1]; |
| 4820 | ParamTypes[1] = *Ptr; |
| 4821 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4822 | } else { |
| 4823 | // ptrdiff_t operator-(T, T); |
| 4824 | ParamTypes[1] = *Ptr; |
| 4825 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4826 | Args, 2, CandidateSet); |
| 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | // Fall through |
| 4831 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4832 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4833 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4834 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4835 | // C++ [over.built]p12: |
| 4836 | // |
| 4837 | // For every pair of promoted arithmetic types L and R, there |
| 4838 | // exist candidate operator functions of the form |
| 4839 | // |
| 4840 | // LR operator*(L, R); |
| 4841 | // LR operator/(L, R); |
| 4842 | // LR operator+(L, R); |
| 4843 | // LR operator-(L, R); |
| 4844 | // bool operator<(L, R); |
| 4845 | // bool operator>(L, R); |
| 4846 | // bool operator<=(L, R); |
| 4847 | // bool operator>=(L, R); |
| 4848 | // bool operator==(L, R); |
| 4849 | // bool operator!=(L, R); |
| 4850 | // |
| 4851 | // where LR is the result of the usual arithmetic conversions |
| 4852 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4853 | // |
| 4854 | // C++ [over.built]p24: |
| 4855 | // |
| 4856 | // For every pair of promoted arithmetic types L and R, there exist |
| 4857 | // candidate operator functions of the form |
| 4858 | // |
| 4859 | // LR operator?(bool, L, R); |
| 4860 | // |
| 4861 | // where LR is the result of the usual arithmetic conversions |
| 4862 | // between types L and R. |
| 4863 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4865 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4866 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4867 | Right < LastPromotedArithmeticType; ++Right) { |
| 4868 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4869 | QualType Result |
| 4870 | = isComparison |
| 4871 | ? Context.BoolTy |
| 4872 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4873 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4874 | } |
| 4875 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4876 | |
| 4877 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 4878 | // conditional operator for vector types. |
| 4879 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4880 | Vec1End = CandidateTypes.vector_end(); |
| 4881 | Vec1 != Vec1End; ++Vec1) |
| 4882 | for (BuiltinCandidateTypeSet::iterator |
| 4883 | Vec2 = CandidateTypes.vector_begin(), |
| 4884 | Vec2End = CandidateTypes.vector_end(); |
| 4885 | Vec2 != Vec2End; ++Vec2) { |
| 4886 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 4887 | QualType Result; |
| 4888 | if (isComparison) |
| 4889 | Result = Context.BoolTy; |
| 4890 | else { |
| 4891 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 4892 | Result = *Vec1; |
| 4893 | else |
| 4894 | Result = *Vec2; |
| 4895 | } |
| 4896 | |
| 4897 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4898 | } |
| 4899 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4900 | break; |
| 4901 | |
| 4902 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4903 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4904 | case OO_Caret: |
| 4905 | case OO_Pipe: |
| 4906 | case OO_LessLess: |
| 4907 | case OO_GreaterGreater: |
| 4908 | // C++ [over.built]p17: |
| 4909 | // |
| 4910 | // For every pair of promoted integral types L and R, there |
| 4911 | // exist candidate operator functions of the form |
| 4912 | // |
| 4913 | // LR operator%(L, R); |
| 4914 | // LR operator&(L, R); |
| 4915 | // LR operator^(L, R); |
| 4916 | // LR operator|(L, R); |
| 4917 | // L operator<<(L, R); |
| 4918 | // L operator>>(L, R); |
| 4919 | // |
| 4920 | // where LR is the result of the usual arithmetic conversions |
| 4921 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4923 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4925 | Right < LastPromotedIntegralType; ++Right) { |
| 4926 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4927 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4928 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4929 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4930 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4931 | } |
| 4932 | } |
| 4933 | break; |
| 4934 | |
| 4935 | case OO_Equal: |
| 4936 | // C++ [over.built]p20: |
| 4937 | // |
| 4938 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4939 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4940 | // empty, there exist candidate operator functions of the form |
| 4941 | // |
| 4942 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4943 | for (BuiltinCandidateTypeSet::iterator |
| 4944 | Enum = CandidateTypes.enumeration_begin(), |
| 4945 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4946 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4948 | CandidateSet); |
| 4949 | for (BuiltinCandidateTypeSet::iterator |
| 4950 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4951 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4952 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4954 | CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4955 | |
| 4956 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4957 | |
| 4958 | case OO_PlusEqual: |
| 4959 | case OO_MinusEqual: |
| 4960 | // C++ [over.built]p19: |
| 4961 | // |
| 4962 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4963 | // volatile or empty, there exist candidate operator functions |
| 4964 | // of the form |
| 4965 | // |
| 4966 | // T*VQ& operator=(T*VQ&, T*); |
| 4967 | // |
| 4968 | // C++ [over.built]p21: |
| 4969 | // |
| 4970 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4971 | // cv-unqualified object type and VQ is either volatile or |
| 4972 | // empty, there exist candidate operator functions of the form |
| 4973 | // |
| 4974 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4975 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4976 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4977 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4978 | QualType ParamTypes[2]; |
| 4979 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4980 | |
| 4981 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4982 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4983 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4984 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4985 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4986 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4987 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4988 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4989 | ParamTypes[0] |
| 4990 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4991 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4992 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4993 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4994 | } |
| 4995 | // Fall through. |
| 4996 | |
| 4997 | case OO_StarEqual: |
| 4998 | case OO_SlashEqual: |
| 4999 | // C++ [over.built]p18: |
| 5000 | // |
| 5001 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5002 | // VQ is either volatile or empty, and R is a promoted |
| 5003 | // arithmetic type, there exist candidate operator functions of |
| 5004 | // the form |
| 5005 | // |
| 5006 | // VQ L& operator=(VQ L&, R); |
| 5007 | // VQ L& operator*=(VQ L&, R); |
| 5008 | // VQ L& operator/=(VQ L&, R); |
| 5009 | // VQ L& operator+=(VQ L&, R); |
| 5010 | // VQ L& operator-=(VQ L&, R); |
| 5011 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5012 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5013 | Right < LastPromotedArithmeticType; ++Right) { |
| 5014 | QualType ParamTypes[2]; |
| 5015 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 5016 | |
| 5017 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5018 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5019 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5020 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5021 | |
| 5022 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 5023 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5024 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 5025 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 5026 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5027 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 5028 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5029 | } |
| 5030 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5031 | |
| 5032 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 5033 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 5034 | Vec1End = CandidateTypes.vector_end(); |
| 5035 | Vec1 != Vec1End; ++Vec1) |
| 5036 | for (BuiltinCandidateTypeSet::iterator |
| 5037 | Vec2 = CandidateTypes.vector_begin(), |
| 5038 | Vec2End = CandidateTypes.vector_end(); |
| 5039 | Vec2 != Vec2End; ++Vec2) { |
| 5040 | QualType ParamTypes[2]; |
| 5041 | ParamTypes[1] = *Vec2; |
| 5042 | // Add this built-in operator as a candidate (VQ is empty). |
| 5043 | ParamTypes[0] = Context.getLValueReferenceType(*Vec1); |
| 5044 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5045 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 5046 | |
| 5047 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5048 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5049 | ParamTypes[0] = Context.getVolatileType(*Vec1); |
| 5050 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 5051 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5052 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 5053 | } |
| 5054 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5055 | break; |
| 5056 | |
| 5057 | case OO_PercentEqual: |
| 5058 | case OO_LessLessEqual: |
| 5059 | case OO_GreaterGreaterEqual: |
| 5060 | case OO_AmpEqual: |
| 5061 | case OO_CaretEqual: |
| 5062 | case OO_PipeEqual: |
| 5063 | // C++ [over.built]p22: |
| 5064 | // |
| 5065 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 5066 | // is either volatile or empty, and R is a promoted integral |
| 5067 | // type, there exist candidate operator functions of the form |
| 5068 | // |
| 5069 | // VQ L& operator%=(VQ L&, R); |
| 5070 | // VQ L& operator<<=(VQ L&, R); |
| 5071 | // VQ L& operator>>=(VQ L&, R); |
| 5072 | // VQ L& operator&=(VQ L&, R); |
| 5073 | // VQ L& operator^=(VQ L&, R); |
| 5074 | // VQ L& operator|=(VQ L&, R); |
| 5075 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5076 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5077 | Right < LastPromotedIntegralType; ++Right) { |
| 5078 | QualType ParamTypes[2]; |
| 5079 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 5080 | |
| 5081 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5082 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5083 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 5084 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5085 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5086 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 5087 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 5088 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 5089 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 5090 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5091 | } |
| 5092 | } |
| 5093 | break; |
| 5094 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5095 | case OO_Exclaim: { |
| 5096 | // C++ [over.operator]p23: |
| 5097 | // |
| 5098 | // There also exist candidate operator functions of the form |
| 5099 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5100 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5101 | // bool operator&&(bool, bool); [BELOW] |
| 5102 | // bool operator||(bool, bool); [BELOW] |
| 5103 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5104 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 5105 | /*IsAssignmentOperator=*/false, |
| 5106 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5107 | break; |
| 5108 | } |
| 5109 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5110 | case OO_AmpAmp: |
| 5111 | case OO_PipePipe: { |
| 5112 | // C++ [over.operator]p23: |
| 5113 | // |
| 5114 | // There also exist candidate operator functions of the form |
| 5115 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5116 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5117 | // bool operator&&(bool, bool); |
| 5118 | // bool operator||(bool, bool); |
| 5119 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5120 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 5121 | /*IsAssignmentOperator=*/false, |
| 5122 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5123 | break; |
| 5124 | } |
| 5125 | |
| 5126 | case OO_Subscript: |
| 5127 | // C++ [over.built]p13: |
| 5128 | // |
| 5129 | // For every cv-qualified or cv-unqualified object type T there |
| 5130 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5131 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5132 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5133 | // T& operator[](T*, ptrdiff_t); |
| 5134 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5135 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5136 | // T& operator[](ptrdiff_t, T*); |
| 5137 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 5138 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5139 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Argyrios Kyrtzidis | 42d0f2a | 2010-08-23 07:12:16 +0000 | [diff] [blame] | 5140 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5141 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5142 | |
| 5143 | // T& operator[](T*, ptrdiff_t) |
| 5144 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5145 | |
| 5146 | // T& operator[](ptrdiff_t, T*); |
| 5147 | ParamTypes[0] = ParamTypes[1]; |
| 5148 | ParamTypes[1] = *Ptr; |
| 5149 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5150 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5151 | break; |
| 5152 | |
| 5153 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5154 | // C++ [over.built]p11: |
| 5155 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5156 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5157 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5158 | // there exist candidate operator functions of the form |
| 5159 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5160 | // where CV12 is the union of CV1 and CV2. |
| 5161 | { |
| 5162 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5163 | CandidateTypes.pointer_begin(); |
| 5164 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5165 | QualType C1Ty = (*Ptr); |
| 5166 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5167 | QualifierCollector Q1; |
Argyrios Kyrtzidis | 42d0f2a | 2010-08-23 07:12:16 +0000 | [diff] [blame] | 5168 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 5169 | if (!isa<RecordType>(C1)) |
| 5170 | continue; |
| 5171 | // heuristic to reduce number of builtin candidates in the set. |
| 5172 | // Add volatile/restrict version only if there are conversions to a |
| 5173 | // volatile/restrict type. |
| 5174 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5175 | continue; |
| 5176 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5177 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5178 | for (BuiltinCandidateTypeSet::iterator |
| 5179 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 5180 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 5181 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5182 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5183 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 5184 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5185 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 5186 | break; |
| 5187 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5188 | // build CV12 T& |
| 5189 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5190 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5191 | T.isVolatileQualified()) |
| 5192 | continue; |
| 5193 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5194 | T.isRestrictQualified()) |
| 5195 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5196 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5197 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 5198 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5199 | } |
| 5200 | } |
| 5201 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5202 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5203 | |
| 5204 | case OO_Conditional: |
| 5205 | // Note that we don't consider the first argument, since it has been |
| 5206 | // contextually converted to bool long ago. The candidates below are |
| 5207 | // therefore added as binary. |
| 5208 | // |
| 5209 | // C++ [over.built]p24: |
| 5210 | // For every type T, where T is a pointer or pointer-to-member type, |
| 5211 | // there exist candidate operator functions of the form |
| 5212 | // |
| 5213 | // T operator?(bool, T, T); |
| 5214 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5215 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 5216 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 5217 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5218 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5219 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5220 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5221 | CandidateTypes.member_pointer_begin(), |
| 5222 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 5223 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5224 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5225 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5226 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5227 | } |
| 5228 | } |
| 5229 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5230 | /// \brief Add function candidates found via argument-dependent lookup |
| 5231 | /// to the set of overloading candidates. |
| 5232 | /// |
| 5233 | /// This routine performs argument-dependent name lookup based on the |
| 5234 | /// given function name (which may also be an operator name) and adds |
| 5235 | /// all of the overload candidates found by ADL to the overload |
| 5236 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5237 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5238 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5239 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5240 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5241 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5242 | OverloadCandidateSet& CandidateSet, |
| 5243 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5244 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5245 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 5246 | // FIXME: This approach for uniquing ADL results (and removing |
| 5247 | // redundant candidates from the set) relies on pointer-equality, |
| 5248 | // which means we need to key off the canonical decl. However, |
| 5249 | // always going back to the canonical decl might not get us the |
| 5250 | // right set of default arguments. What default arguments are |
| 5251 | // we supposed to consider on ADL candidates, anyway? |
| 5252 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5253 | // FIXME: Pass in the explicit template arguments? |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5254 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5255 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5256 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5257 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5258 | CandEnd = CandidateSet.end(); |
| 5259 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5260 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5261 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5262 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5263 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5264 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5265 | |
| 5266 | // For each of the ADL candidates we found, add it to the overload |
| 5267 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5268 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5269 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5270 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5271 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5272 | continue; |
| 5273 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5274 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5275 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5276 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5277 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5278 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 5279 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5280 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5283 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 5284 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5285 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5286 | isBetterOverloadCandidate(Sema &S, |
| 5287 | const OverloadCandidate& Cand1, |
| 5288 | const OverloadCandidate& Cand2, |
| 5289 | SourceLocation Loc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5290 | // Define viable functions to be better candidates than non-viable |
| 5291 | // functions. |
| 5292 | if (!Cand2.Viable) |
| 5293 | return Cand1.Viable; |
| 5294 | else if (!Cand1.Viable) |
| 5295 | return false; |
| 5296 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5297 | // C++ [over.match.best]p1: |
| 5298 | // |
| 5299 | // -- if F is a static member function, ICS1(F) is defined such |
| 5300 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 5301 | // any function G, and, symmetrically, ICS1(G) is neither |
| 5302 | // better nor worse than ICS1(F). |
| 5303 | unsigned StartArg = 0; |
| 5304 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 5305 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5307 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5308 | // A viable function F1 is defined to be a better function than another |
| 5309 | // 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] | 5310 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5311 | unsigned NumArgs = Cand1.Conversions.size(); |
| 5312 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 5313 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5314 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5315 | switch (CompareImplicitConversionSequences(S, |
| 5316 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5317 | Cand2.Conversions[ArgIdx])) { |
| 5318 | case ImplicitConversionSequence::Better: |
| 5319 | // Cand1 has a better conversion sequence. |
| 5320 | HasBetterConversion = true; |
| 5321 | break; |
| 5322 | |
| 5323 | case ImplicitConversionSequence::Worse: |
| 5324 | // Cand1 can't be better than Cand2. |
| 5325 | return false; |
| 5326 | |
| 5327 | case ImplicitConversionSequence::Indistinguishable: |
| 5328 | // Do nothing. |
| 5329 | break; |
| 5330 | } |
| 5331 | } |
| 5332 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5333 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5334 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5335 | if (HasBetterConversion) |
| 5336 | return true; |
| 5337 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5338 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5339 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 5340 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5341 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 5342 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | |
| 5344 | // -- F1 and F2 are function template specializations, and the function |
| 5345 | // template for F1 is more specialized than the template for F2 |
| 5346 | // 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] | 5347 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 5348 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 5349 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5350 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5351 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 5352 | Cand2.Function->getPrimaryTemplate(), |
| 5353 | Loc, |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 5354 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 5355 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5356 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5357 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5358 | // -- the context is an initialization by user-defined conversion |
| 5359 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 5360 | // from the return type of F1 to the destination type (i.e., |
| 5361 | // the type of the entity being initialized) is a better |
| 5362 | // conversion sequence than the standard conversion sequence |
| 5363 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5364 | if (Cand1.Function && Cand2.Function && |
| 5365 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5366 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5367 | switch (CompareStandardConversionSequences(S, |
| 5368 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5369 | Cand2.FinalConversion)) { |
| 5370 | case ImplicitConversionSequence::Better: |
| 5371 | // Cand1 has a better conversion sequence. |
| 5372 | return true; |
| 5373 | |
| 5374 | case ImplicitConversionSequence::Worse: |
| 5375 | // Cand1 can't be better than Cand2. |
| 5376 | return false; |
| 5377 | |
| 5378 | case ImplicitConversionSequence::Indistinguishable: |
| 5379 | // Do nothing |
| 5380 | break; |
| 5381 | } |
| 5382 | } |
| 5383 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5384 | return false; |
| 5385 | } |
| 5386 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5387 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5388 | /// within an overload candidate set. |
| 5389 | /// |
| 5390 | /// \param CandidateSet the set of candidate functions. |
| 5391 | /// |
| 5392 | /// \param Loc the location of the function name (or operator symbol) for |
| 5393 | /// which overload resolution occurs. |
| 5394 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5395 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5396 | /// function, Best points to the candidate function found. |
| 5397 | /// |
| 5398 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5399 | OverloadingResult |
| 5400 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
| 5401 | iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5402 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5403 | Best = end(); |
| 5404 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 5405 | if (Cand->Viable) |
| 5406 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5407 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5411 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5412 | return OR_No_Viable_Function; |
| 5413 | |
| 5414 | // Make sure that this function is better than every other viable |
| 5415 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5416 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5417 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5418 | Cand != Best && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5419 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc)) { |
| 5420 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5421 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5422 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5425 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5426 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5427 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5428 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5429 | return OR_Deleted; |
| 5430 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5431 | // C++ [basic.def.odr]p2: |
| 5432 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5433 | // when referred to from a potentially-evaluated expression. [Note: this |
| 5434 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5435 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 5436 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 5437 | if (Best->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5438 | S.MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5439 | return OR_Success; |
| 5440 | } |
| 5441 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5442 | namespace { |
| 5443 | |
| 5444 | enum OverloadCandidateKind { |
| 5445 | oc_function, |
| 5446 | oc_method, |
| 5447 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5448 | oc_function_template, |
| 5449 | oc_method_template, |
| 5450 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5451 | oc_implicit_default_constructor, |
| 5452 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5453 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5454 | }; |
| 5455 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5456 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 5457 | FunctionDecl *Fn, |
| 5458 | std::string &Description) { |
| 5459 | bool isTemplate = false; |
| 5460 | |
| 5461 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 5462 | isTemplate = true; |
| 5463 | Description = S.getTemplateArgumentBindingsText( |
| 5464 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 5465 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5466 | |
| 5467 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5468 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5469 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5470 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5471 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 5472 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5476 | // This actually gets spelled 'candidate function' for now, but |
| 5477 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5478 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5479 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5480 | |
| 5481 | assert(Meth->isCopyAssignment() |
| 5482 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5483 | return oc_implicit_copy_assignment; |
| 5484 | } |
| 5485 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5486 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | } // end anonymous namespace |
| 5490 | |
| 5491 | // Notes the location of an overload candidate. |
| 5492 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5493 | std::string FnDesc; |
| 5494 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 5495 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 5496 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5499 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 5500 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 5501 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5502 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 5503 | Sema &S, |
| 5504 | SourceLocation CaretLoc, |
| 5505 | const PartialDiagnostic &PDiag) const { |
| 5506 | S.Diag(CaretLoc, PDiag) |
| 5507 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5508 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5509 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 5510 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5511 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5514 | namespace { |
| 5515 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5516 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 5517 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 5518 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5519 | assert(Cand->Function && "for now, candidate must be a function"); |
| 5520 | FunctionDecl *Fn = Cand->Function; |
| 5521 | |
| 5522 | // There's a conversion slot for the object argument if this is a |
| 5523 | // non-constructor method. Note that 'I' corresponds the |
| 5524 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5525 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5526 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5527 | if (I == 0) |
| 5528 | isObjectArgument = true; |
| 5529 | else |
| 5530 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5533 | std::string FnDesc; |
| 5534 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 5535 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5536 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 5537 | QualType FromTy = Conv.Bad.getFromType(); |
| 5538 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5539 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5540 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5541 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5542 | Expr *E = FromExpr->IgnoreParens(); |
| 5543 | if (isa<UnaryOperator>(E)) |
| 5544 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5545 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5546 | |
| 5547 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 5548 | << (unsigned) FnKind << FnDesc |
| 5549 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5550 | << ToTy << Name << I+1; |
| 5551 | return; |
| 5552 | } |
| 5553 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5554 | // Do some hand-waving analysis to see if the non-viability is due |
| 5555 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5556 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 5557 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 5558 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 5559 | CToTy = RT->getPointeeType(); |
| 5560 | else { |
| 5561 | // TODO: detect and diagnose the full richness of const mismatches. |
| 5562 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 5563 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 5564 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 5565 | } |
| 5566 | |
| 5567 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 5568 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 5569 | // It is dumb that we have to do this here. |
| 5570 | while (isa<ArrayType>(CFromTy)) |
| 5571 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5572 | while (isa<ArrayType>(CToTy)) |
| 5573 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5574 | |
| 5575 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 5576 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 5577 | |
| 5578 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 5579 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 5580 | << (unsigned) FnKind << FnDesc |
| 5581 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5582 | << FromTy |
| 5583 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 5584 | << (unsigned) isObjectArgument << I+1; |
| 5585 | return; |
| 5586 | } |
| 5587 | |
| 5588 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5589 | assert(CVR && "unexpected qualifiers mismatch"); |
| 5590 | |
| 5591 | if (isObjectArgument) { |
| 5592 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 5593 | << (unsigned) FnKind << FnDesc |
| 5594 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5595 | << FromTy << (CVR - 1); |
| 5596 | } else { |
| 5597 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 5598 | << (unsigned) FnKind << FnDesc |
| 5599 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5600 | << FromTy << (CVR - 1) << I+1; |
| 5601 | } |
| 5602 | return; |
| 5603 | } |
| 5604 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5605 | // Diagnose references or pointers to incomplete types differently, |
| 5606 | // since it's far from impossible that the incompleteness triggered |
| 5607 | // the failure. |
| 5608 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 5609 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 5610 | TempFromTy = PTy->getPointeeType(); |
| 5611 | if (TempFromTy->isIncompleteType()) { |
| 5612 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 5613 | << (unsigned) FnKind << FnDesc |
| 5614 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5615 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 5616 | return; |
| 5617 | } |
| 5618 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5619 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5620 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5621 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 5622 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 5623 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5624 | FromPtrTy->getPointeeType()) && |
| 5625 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 5626 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
| 5627 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
| 5628 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5629 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5630 | } |
| 5631 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 5632 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 5633 | if (const ObjCObjectPointerType *ToPtrTy |
| 5634 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 5635 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 5636 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 5637 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5638 | FromPtrTy->getPointeeType()) && |
| 5639 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5640 | BaseToDerivedConversion = 2; |
| 5641 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 5642 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 5643 | !FromTy->isIncompleteType() && |
| 5644 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 5645 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 5646 | BaseToDerivedConversion = 3; |
| 5647 | } |
| 5648 | |
| 5649 | if (BaseToDerivedConversion) { |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5650 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5651 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5652 | << (unsigned) FnKind << FnDesc |
| 5653 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5654 | << (BaseToDerivedConversion - 1) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5655 | << FromTy << ToTy << I+1; |
| 5656 | return; |
| 5657 | } |
| 5658 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5659 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5660 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 5661 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5662 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 5663 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5664 | } |
| 5665 | |
| 5666 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 5667 | unsigned NumFormalArgs) { |
| 5668 | // TODO: treat calls to a missing default constructor as a special case |
| 5669 | |
| 5670 | FunctionDecl *Fn = Cand->Function; |
| 5671 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 5672 | |
| 5673 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 5674 | |
| 5675 | // at least / at most / exactly |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5676 | // FIXME: variadic templates "at most" should account for parameter packs |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5677 | unsigned mode, modeCount; |
| 5678 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5679 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 5680 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5681 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5682 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 5683 | mode = 0; // "at least" |
| 5684 | else |
| 5685 | mode = 2; // "exactly" |
| 5686 | modeCount = MinParams; |
| 5687 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5688 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 5689 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5690 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5691 | if (MinParams != FnTy->getNumArgs()) |
| 5692 | mode = 1; // "at most" |
| 5693 | else |
| 5694 | mode = 2; // "exactly" |
| 5695 | modeCount = FnTy->getNumArgs(); |
| 5696 | } |
| 5697 | |
| 5698 | std::string Description; |
| 5699 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 5700 | |
| 5701 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5702 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 5703 | << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5706 | /// Diagnose a failed template-argument deduction. |
| 5707 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 5708 | Expr **Args, unsigned NumArgs) { |
| 5709 | FunctionDecl *Fn = Cand->Function; // pattern |
| 5710 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5711 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5712 | NamedDecl *ParamD; |
| 5713 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 5714 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 5715 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5716 | switch (Cand->DeductionFailure.Result) { |
| 5717 | case Sema::TDK_Success: |
| 5718 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 5719 | |
| 5720 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5721 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 5722 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 5723 | << ParamD->getDeclName(); |
| 5724 | return; |
| 5725 | } |
| 5726 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 5727 | case Sema::TDK_Underqualified: { |
| 5728 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 5729 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 5730 | |
| 5731 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 5732 | |
| 5733 | // Param will have been canonicalized, but it should just be a |
| 5734 | // qualified version of ParamD, so move the qualifiers to that. |
| 5735 | QualifierCollector Qs(S.Context); |
| 5736 | Qs.strip(Param); |
| 5737 | QualType NonCanonParam = Qs.apply(TParam->getTypeForDecl()); |
| 5738 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 5739 | |
| 5740 | // Arg has also been canonicalized, but there's nothing we can do |
| 5741 | // about that. It also doesn't matter as much, because it won't |
| 5742 | // have any template parameters in it (because deduction isn't |
| 5743 | // done on dependent types). |
| 5744 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 5745 | |
| 5746 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 5747 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 5748 | return; |
| 5749 | } |
| 5750 | |
| 5751 | case Sema::TDK_Inconsistent: { |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5752 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5753 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5754 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5755 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5756 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5757 | which = 1; |
| 5758 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5759 | which = 2; |
| 5760 | } |
| 5761 | |
| 5762 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
| 5763 | << which << ParamD->getDeclName() |
| 5764 | << *Cand->DeductionFailure.getFirstArg() |
| 5765 | << *Cand->DeductionFailure.getSecondArg(); |
| 5766 | return; |
| 5767 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5768 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5769 | case Sema::TDK_InvalidExplicitArguments: |
| 5770 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| 5771 | if (ParamD->getDeclName()) |
| 5772 | S.Diag(Fn->getLocation(), |
| 5773 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 5774 | << ParamD->getDeclName(); |
| 5775 | else { |
| 5776 | int index = 0; |
| 5777 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 5778 | index = TTP->getIndex(); |
| 5779 | else if (NonTypeTemplateParmDecl *NTTP |
| 5780 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 5781 | index = NTTP->getIndex(); |
| 5782 | else |
| 5783 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| 5784 | S.Diag(Fn->getLocation(), |
| 5785 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 5786 | << (index + 1); |
| 5787 | } |
| 5788 | return; |
| 5789 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5790 | case Sema::TDK_TooManyArguments: |
| 5791 | case Sema::TDK_TooFewArguments: |
| 5792 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 5793 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 5794 | |
| 5795 | case Sema::TDK_InstantiationDepth: |
| 5796 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
| 5797 | return; |
| 5798 | |
| 5799 | case Sema::TDK_SubstitutionFailure: { |
| 5800 | std::string ArgString; |
| 5801 | if (TemplateArgumentList *Args |
| 5802 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 5803 | ArgString = S.getTemplateArgumentBindingsText( |
| 5804 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 5805 | *Args); |
| 5806 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 5807 | << ArgString; |
| 5808 | return; |
| 5809 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5810 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5811 | // TODO: diagnose these individually, then kill off |
| 5812 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5813 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5814 | case Sema::TDK_FailedOverloadResolution: |
| 5815 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 5816 | return; |
| 5817 | } |
| 5818 | } |
| 5819 | |
| 5820 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 5821 | /// already generated a primary error at the call site. |
| 5822 | /// |
| 5823 | /// It really does need to be a single diagnostic with its caret |
| 5824 | /// pointed at the candidate declaration. Yes, this creates some |
| 5825 | /// major challenges of technical writing. Yes, this makes pointing |
| 5826 | /// out problems with specific arguments quite awkward. It's still |
| 5827 | /// better than generating twenty screens of text for every failed |
| 5828 | /// overload. |
| 5829 | /// |
| 5830 | /// It would be great to be able to express per-candidate problems |
| 5831 | /// more richly for those diagnostic clients that cared, but we'd |
| 5832 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5833 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 5834 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5835 | FunctionDecl *Fn = Cand->Function; |
| 5836 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5837 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5838 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5839 | std::string FnDesc; |
| 5840 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5841 | |
| 5842 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5843 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5844 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5847 | // We don't really have anything else to say about viable candidates. |
| 5848 | if (Cand->Viable) { |
| 5849 | S.NoteOverloadCandidate(Fn); |
| 5850 | return; |
| 5851 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5852 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5853 | switch (Cand->FailureKind) { |
| 5854 | case ovl_fail_too_many_arguments: |
| 5855 | case ovl_fail_too_few_arguments: |
| 5856 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5857 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5858 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5859 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 5860 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5861 | case ovl_fail_trivial_conversion: |
| 5862 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5863 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5864 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5865 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5866 | case ovl_fail_bad_conversion: { |
| 5867 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 5868 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5869 | if (Cand->Conversions[I].isBad()) |
| 5870 | return DiagnoseBadConversion(S, Cand, I); |
| 5871 | |
| 5872 | // FIXME: this currently happens when we're called from SemaInit |
| 5873 | // when user-conversion overload fails. Figure out how to handle |
| 5874 | // those conditions and diagnose them well. |
| 5875 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5876 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5877 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 5881 | // Desugar the type of the surrogate down to a function type, |
| 5882 | // retaining as many typedefs as possible while still showing |
| 5883 | // the function type (and, therefore, its parameter types). |
| 5884 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 5885 | bool isLValueReference = false; |
| 5886 | bool isRValueReference = false; |
| 5887 | bool isPointer = false; |
| 5888 | if (const LValueReferenceType *FnTypeRef = |
| 5889 | FnType->getAs<LValueReferenceType>()) { |
| 5890 | FnType = FnTypeRef->getPointeeType(); |
| 5891 | isLValueReference = true; |
| 5892 | } else if (const RValueReferenceType *FnTypeRef = |
| 5893 | FnType->getAs<RValueReferenceType>()) { |
| 5894 | FnType = FnTypeRef->getPointeeType(); |
| 5895 | isRValueReference = true; |
| 5896 | } |
| 5897 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5898 | FnType = FnTypePtr->getPointeeType(); |
| 5899 | isPointer = true; |
| 5900 | } |
| 5901 | // Desugar down to a function type. |
| 5902 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5903 | // Reconstruct the pointer/reference as appropriate. |
| 5904 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5905 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5906 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5907 | |
| 5908 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5909 | << FnType; |
| 5910 | } |
| 5911 | |
| 5912 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5913 | const char *Opc, |
| 5914 | SourceLocation OpLoc, |
| 5915 | OverloadCandidate *Cand) { |
| 5916 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5917 | std::string TypeStr("operator"); |
| 5918 | TypeStr += Opc; |
| 5919 | TypeStr += "("; |
| 5920 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5921 | if (Cand->Conversions.size() == 1) { |
| 5922 | TypeStr += ")"; |
| 5923 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5924 | } else { |
| 5925 | TypeStr += ", "; |
| 5926 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5927 | TypeStr += ")"; |
| 5928 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5929 | } |
| 5930 | } |
| 5931 | |
| 5932 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5933 | OverloadCandidate *Cand) { |
| 5934 | unsigned NoOperands = Cand->Conversions.size(); |
| 5935 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5936 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5937 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5938 | if (!ICS.isAmbiguous()) continue; |
| 5939 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5940 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5941 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5942 | } |
| 5943 | } |
| 5944 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5945 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5946 | if (Cand->Function) |
| 5947 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5948 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5949 | return Cand->Surrogate->getLocation(); |
| 5950 | return SourceLocation(); |
| 5951 | } |
| 5952 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5953 | struct CompareOverloadCandidatesForDisplay { |
| 5954 | Sema &S; |
| 5955 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5956 | |
| 5957 | bool operator()(const OverloadCandidate *L, |
| 5958 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5959 | // Fast-path this check. |
| 5960 | if (L == R) return false; |
| 5961 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5962 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5963 | if (L->Viable) { |
| 5964 | if (!R->Viable) return true; |
| 5965 | |
| 5966 | // TODO: introduce a tri-valued comparison for overload |
| 5967 | // candidates. Would be more worthwhile if we had a sort |
| 5968 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5969 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 5970 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5971 | } else if (R->Viable) |
| 5972 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5973 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5974 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5975 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5976 | // Criteria by which we can sort non-viable candidates: |
| 5977 | if (!L->Viable) { |
| 5978 | // 1. Arity mismatches come after other candidates. |
| 5979 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5980 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5981 | return false; |
| 5982 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5983 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5984 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5985 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5986 | // 2. Bad conversions come first and are ordered by the number |
| 5987 | // of bad conversions and quality of good conversions. |
| 5988 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5989 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5990 | return true; |
| 5991 | |
| 5992 | // If there's any ordering between the defined conversions... |
| 5993 | // FIXME: this might not be transitive. |
| 5994 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5995 | |
| 5996 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5997 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5998 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5999 | switch (CompareImplicitConversionSequences(S, |
| 6000 | L->Conversions[I], |
| 6001 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6002 | case ImplicitConversionSequence::Better: |
| 6003 | leftBetter++; |
| 6004 | break; |
| 6005 | |
| 6006 | case ImplicitConversionSequence::Worse: |
| 6007 | leftBetter--; |
| 6008 | break; |
| 6009 | |
| 6010 | case ImplicitConversionSequence::Indistinguishable: |
| 6011 | break; |
| 6012 | } |
| 6013 | } |
| 6014 | if (leftBetter > 0) return true; |
| 6015 | if (leftBetter < 0) return false; |
| 6016 | |
| 6017 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 6018 | return false; |
| 6019 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6020 | // TODO: others? |
| 6021 | } |
| 6022 | |
| 6023 | // Sort everything else by location. |
| 6024 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 6025 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 6026 | |
| 6027 | // Put candidates without locations (e.g. builtins) at the end. |
| 6028 | if (LLoc.isInvalid()) return false; |
| 6029 | if (RLoc.isInvalid()) return true; |
| 6030 | |
| 6031 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6032 | } |
| 6033 | }; |
| 6034 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6035 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 6036 | /// computes up to the first |
| 6037 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 6038 | Expr **Args, unsigned NumArgs) { |
| 6039 | assert(!Cand->Viable); |
| 6040 | |
| 6041 | // Don't do anything on failures other than bad conversion. |
| 6042 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 6043 | |
| 6044 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6045 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6046 | unsigned ConvCount = Cand->Conversions.size(); |
| 6047 | while (true) { |
| 6048 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 6049 | ConvIdx++; |
| 6050 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 6051 | break; |
| 6052 | } |
| 6053 | |
| 6054 | if (ConvIdx == ConvCount) |
| 6055 | return; |
| 6056 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6057 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 6058 | "remaining conversion is initialized?"); |
| 6059 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 6060 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6061 | // operation somehow. |
| 6062 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6063 | |
| 6064 | const FunctionProtoType* Proto; |
| 6065 | unsigned ArgIdx = ConvIdx; |
| 6066 | |
| 6067 | if (Cand->IsSurrogate) { |
| 6068 | QualType ConvType |
| 6069 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 6070 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6071 | ConvType = ConvPtrType->getPointeeType(); |
| 6072 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 6073 | ArgIdx--; |
| 6074 | } else if (Cand->Function) { |
| 6075 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 6076 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 6077 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 6078 | ArgIdx--; |
| 6079 | } else { |
| 6080 | // Builtin binary operator with a bad first conversion. |
| 6081 | assert(ConvCount <= 3); |
| 6082 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 6083 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6084 | = TryCopyInitialization(S, Args[ConvIdx], |
| 6085 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 6086 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6087 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6088 | return; |
| 6089 | } |
| 6090 | |
| 6091 | // Fill in the rest of the conversions. |
| 6092 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6093 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 6094 | if (ArgIdx < NumArgsInProto) |
| 6095 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6096 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 6097 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6098 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6099 | else |
| 6100 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 6101 | } |
| 6102 | } |
| 6103 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6104 | } // end anonymous namespace |
| 6105 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6106 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 6107 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6108 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6109 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 6110 | OverloadCandidateDisplayKind OCD, |
| 6111 | Expr **Args, unsigned NumArgs, |
| 6112 | const char *Opc, |
| 6113 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6114 | // Sort the candidates by viability and position. Sorting directly would |
| 6115 | // be prohibitive, so we make a set of pointers and sort those. |
| 6116 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6117 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 6118 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6119 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6120 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6121 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6122 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6123 | if (Cand->Function || Cand->IsSurrogate) |
| 6124 | Cands.push_back(Cand); |
| 6125 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 6126 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6127 | } |
| 6128 | } |
| 6129 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6130 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6131 | CompareOverloadCandidatesForDisplay(S)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6132 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6133 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6134 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6135 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6136 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6137 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6138 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 6139 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 6140 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6141 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 6142 | // the user with. FIXME: This limit should depend on details of the |
| 6143 | // candidate list. |
| 6144 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 6145 | break; |
| 6146 | } |
| 6147 | ++CandsShown; |
| 6148 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6149 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6150 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6151 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6152 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6153 | else { |
| 6154 | assert(Cand->Viable && |
| 6155 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6156 | // Generally we only see ambiguities including viable builtin |
| 6157 | // operators if overload resolution got screwed up by an |
| 6158 | // ambiguous user-defined conversion. |
| 6159 | // |
| 6160 | // FIXME: It's quite possible for different conversions to see |
| 6161 | // different ambiguities, though. |
| 6162 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6163 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6164 | ReportedAmbiguousConversions = true; |
| 6165 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6166 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6167 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6168 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6169 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6170 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6171 | |
| 6172 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6173 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6176 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6177 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6178 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6179 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6180 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6181 | } |
| 6182 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6183 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 6184 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 6185 | /// expression with overloaded function type and @p ToType is the type |
| 6186 | /// we're trying to resolve to. For example: |
| 6187 | /// |
| 6188 | /// @code |
| 6189 | /// int f(double); |
| 6190 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6191 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6192 | /// int (*pfd)(double) = f; // selects f(double) |
| 6193 | /// @endcode |
| 6194 | /// |
| 6195 | /// This routine returns the resulting FunctionDecl if it could be |
| 6196 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 6197 | /// routine will emit diagnostics if there is an error. |
| 6198 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6199 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6200 | bool Complain, |
| 6201 | DeclAccessPair &FoundResult) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6202 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6203 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6204 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6205 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6206 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 6207 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6208 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6209 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6210 | FunctionType = MemTypePtr->getPointeeType(); |
| 6211 | IsMember = true; |
| 6212 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6213 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6214 | // C++ [over.over]p1: |
| 6215 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6216 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6217 | // C++ [over.over]p1: |
| 6218 | // [...] The overloaded function name can be preceded by the & |
| 6219 | // operator. |
John McCall | c988fab | 2010-08-24 23:26:21 +0000 | [diff] [blame] | 6220 | // However, remember whether the expression has member-pointer form: |
| 6221 | // C++ [expr.unary.op]p4: |
| 6222 | // A pointer to member is only formed when an explicit & is used |
| 6223 | // and its operand is a qualified-id not enclosed in |
| 6224 | // parentheses. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6225 | OverloadExpr::FindResult Ovl = OverloadExpr::find(From); |
| 6226 | OverloadExpr *OvlExpr = Ovl.Expression; |
John McCall | c988fab | 2010-08-24 23:26:21 +0000 | [diff] [blame] | 6227 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6228 | // We expect a pointer or reference to function, or a function pointer. |
| 6229 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 6230 | if (!FunctionType->isFunctionType()) { |
| 6231 | if (Complain) |
| 6232 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 6233 | << OvlExpr->getName() << ToType; |
| 6234 | |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 6238 | // If the overload expression doesn't have the form of a pointer to |
John McCall | c988fab | 2010-08-24 23:26:21 +0000 | [diff] [blame] | 6239 | // member, don't try to convert it to a pointer-to-member type. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6240 | if (IsMember && !Ovl.HasFormOfMemberPointer) { |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 6241 | if (!Complain) return 0; |
| 6242 | |
| 6243 | // TODO: Should we condition this on whether any functions might |
| 6244 | // have matched, or is it more appropriate to do that in callers? |
| 6245 | // TODO: a fixit wouldn't hurt. |
| 6246 | Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 6247 | << ToType << OvlExpr->getSourceRange(); |
| 6248 | return 0; |
| 6249 | } |
| 6250 | |
| 6251 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 6252 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 6253 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 6254 | ExplicitTemplateArgs = &ETABuffer; |
| 6255 | } |
| 6256 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6257 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6258 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6259 | // Look through all of the overloaded functions, searching for one |
| 6260 | // whose type matches exactly. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6261 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6262 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 6263 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6264 | bool FoundNonTemplateFunction = false; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6265 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6266 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6267 | // Look through any using declarations to find the underlying function. |
| 6268 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 6269 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6270 | // C++ [over.over]p3: |
| 6271 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 6272 | // targets of type "pointer-to-function" or "reference-to-function." |
| 6273 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6274 | // type "pointer-to-member-function." |
| 6275 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6277 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6278 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6279 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6280 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6281 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6282 | // static when converting to member pointer. |
| 6283 | if (Method->isStatic() == IsMember) |
| 6284 | continue; |
| 6285 | } else if (IsMember) |
| 6286 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6287 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6288 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6289 | // If the name is a function template, template argument deduction is |
| 6290 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6291 | // resulting template argument list is used to generate a single |
| 6292 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6293 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6294 | // FIXME: We don't really want to build the specialization here, do we? |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6295 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6296 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6297 | if (TemplateDeductionResult Result |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6298 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6299 | FunctionType, Specialization, Info)) { |
| 6300 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6301 | (void)Result; |
| 6302 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6303 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 6304 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6305 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6306 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6307 | Matches.push_back(std::make_pair(I.getPair(), |
| 6308 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6309 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6310 | |
| 6311 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6313 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6314 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6315 | // Skip non-static functions when converting to pointer, and static |
| 6316 | // when converting to member pointer. |
| 6317 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6318 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6319 | |
| 6320 | // If we have explicit template arguments, skip non-templates. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6321 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6322 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6323 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6324 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6325 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6326 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 6327 | QualType ResultTy; |
| 6328 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 6329 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 6330 | ResultTy)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6331 | Matches.push_back(std::make_pair(I.getPair(), |
| 6332 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6333 | FoundNonTemplateFunction = true; |
| 6334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6335 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6338 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6339 | if (Matches.empty()) { |
| 6340 | if (Complain) { |
| 6341 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 6342 | << OvlExpr->getName() << FunctionType; |
| 6343 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6344 | E = OvlExpr->decls_end(); |
| 6345 | I != E; ++I) |
| 6346 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 6347 | NoteOverloadCandidate(F); |
| 6348 | } |
| 6349 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6350 | return 0; |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6351 | } else if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6352 | FunctionDecl *Result = Matches[0].second; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6353 | FoundResult = Matches[0].first; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6354 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6355 | if (Complain) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6356 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6357 | return Result; |
| 6358 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6359 | |
| 6360 | // C++ [over.over]p4: |
| 6361 | // If more than one function is selected, [...] |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6362 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6363 | // [...] and any given function template specialization F1 is |
| 6364 | // eliminated if the set contains a second function template |
| 6365 | // specialization whose function template is more specialized |
| 6366 | // than the function template of F1 according to the partial |
| 6367 | // ordering rules of 14.5.5.2. |
| 6368 | |
| 6369 | // The algorithm specified above is quadratic. We instead use a |
| 6370 | // two-pass algorithm (similar to the one used to identify the |
| 6371 | // best viable function in an overload set) that identifies the |
| 6372 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6373 | |
| 6374 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 6375 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6376 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6377 | |
| 6378 | UnresolvedSetIterator Result = |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6379 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6380 | TPOC_Other, From->getLocStart(), |
| 6381 | PDiag(), |
| 6382 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6383 | << Matches[0].second->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6384 | PDiag(diag::note_ovl_candidate) |
| 6385 | << (unsigned) oc_function_template); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6386 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6387 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6388 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6389 | if (Complain) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6390 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6391 | DiagnoseUseOfDecl(FoundResult, OvlExpr->getNameLoc()); |
| 6392 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6393 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6395 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6396 | // [...] any function template specializations in the set are |
| 6397 | // eliminated if the set also contains a non-template function, [...] |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6398 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6399 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6400 | ++I; |
| 6401 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6402 | Matches[I] = Matches[--N]; |
| 6403 | Matches.set_size(N); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6404 | } |
| 6405 | } |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6406 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6407 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6408 | // selected function. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6409 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6410 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6411 | FoundResult = Matches[0].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6412 | if (Complain) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6413 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6414 | DiagnoseUseOfDecl(Matches[0].first, OvlExpr->getNameLoc()); |
| 6415 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6416 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6418 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6419 | // FIXME: We should probably return the same thing that BestViableFunction |
| 6420 | // returns (even if we issue the diagnostics here). |
| 6421 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6422 | << Matches[0].second->getDeclName(); |
| 6423 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6424 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6425 | return 0; |
| 6426 | } |
| 6427 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6428 | /// \brief Given an expression that refers to an overloaded function, try to |
| 6429 | /// resolve that overloaded function expression down to a single function. |
| 6430 | /// |
| 6431 | /// This routine can only resolve template-ids that refer to a single function |
| 6432 | /// template, where that template-id refers to a single template whose template |
| 6433 | /// arguments are either provided by the template-id or have defaults, |
| 6434 | /// as described in C++0x [temp.arg.explicit]p3. |
| 6435 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 6436 | // C++ [over.over]p1: |
| 6437 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6438 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6439 | // C++ [over.over]p1: |
| 6440 | // [...] The overloaded function name can be preceded by the & |
| 6441 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6442 | |
| 6443 | if (From->getType() != Context.OverloadTy) |
| 6444 | return 0; |
| 6445 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6446 | OverloadExpr *OvlExpr = OverloadExpr::find(From).Expression; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6447 | |
| 6448 | // If we didn't actually find any template-ids, we're done. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6449 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6450 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6451 | |
| 6452 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 6453 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6454 | |
| 6455 | // Look through all of the overloaded functions, searching for one |
| 6456 | // whose type matches exactly. |
| 6457 | FunctionDecl *Matched = 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6458 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6459 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6460 | // C++0x [temp.arg.explicit]p3: |
| 6461 | // [...] In contexts where deduction is done and fails, or in contexts |
| 6462 | // where deduction is not done, if a template argument list is |
| 6463 | // specified and it, along with any default template arguments, |
| 6464 | // identifies a single function template specialization, then the |
| 6465 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 6466 | FunctionTemplateDecl *FunctionTemplate |
| 6467 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6468 | |
| 6469 | // C++ [over.over]p2: |
| 6470 | // If the name is a function template, template argument deduction is |
| 6471 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6472 | // resulting template argument list is used to generate a single |
| 6473 | // function template specialization, which is added to the set of |
| 6474 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6475 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6476 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6477 | if (TemplateDeductionResult Result |
| 6478 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 6479 | Specialization, Info)) { |
| 6480 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6481 | (void)Result; |
| 6482 | continue; |
| 6483 | } |
| 6484 | |
| 6485 | // Multiple matches; we can't resolve to a single declaration. |
| 6486 | if (Matched) |
| 6487 | return 0; |
| 6488 | |
| 6489 | Matched = Specialization; |
| 6490 | } |
| 6491 | |
| 6492 | return Matched; |
| 6493 | } |
| 6494 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6495 | /// \brief Add a single candidate to the overload set. |
| 6496 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6497 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6498 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6499 | Expr **Args, unsigned NumArgs, |
| 6500 | OverloadCandidateSet &CandidateSet, |
| 6501 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6502 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6503 | if (isa<UsingShadowDecl>(Callee)) |
| 6504 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 6505 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6506 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6507 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6508 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6509 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6510 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | if (FunctionTemplateDecl *FuncTemplate |
| 6514 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6515 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 6516 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6517 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6518 | return; |
| 6519 | } |
| 6520 | |
| 6521 | assert(false && "unhandled case in overloaded call candidate"); |
| 6522 | |
| 6523 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 6527 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6528 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6529 | Expr **Args, unsigned NumArgs, |
| 6530 | OverloadCandidateSet &CandidateSet, |
| 6531 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6532 | |
| 6533 | #ifndef NDEBUG |
| 6534 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 6535 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6536 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6537 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 6538 | // and let Y be the lookup set produced by argument dependent |
| 6539 | // lookup (defined as follows). If X contains |
| 6540 | // |
| 6541 | // -- a declaration of a class member, or |
| 6542 | // |
| 6543 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6544 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6545 | // |
| 6546 | // -- a declaration that is neither a function or a function |
| 6547 | // template |
| 6548 | // |
| 6549 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6550 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6551 | if (ULE->requiresADL()) { |
| 6552 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6553 | E = ULE->decls_end(); I != E; ++I) { |
| 6554 | assert(!(*I)->getDeclContext()->isRecord()); |
| 6555 | assert(isa<UsingShadowDecl>(*I) || |
| 6556 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 6557 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6558 | } |
| 6559 | } |
| 6560 | #endif |
| 6561 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6562 | // It would be nice to avoid this copy. |
| 6563 | TemplateArgumentListInfo TABuffer; |
| 6564 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6565 | if (ULE->hasExplicitTemplateArgs()) { |
| 6566 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6567 | ExplicitTemplateArgs = &TABuffer; |
| 6568 | } |
| 6569 | |
| 6570 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6571 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6572 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6573 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6574 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6575 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6576 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6577 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 6578 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6579 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6580 | CandidateSet, |
| 6581 | PartialOverloading); |
| 6582 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6583 | |
| 6584 | /// Attempts to recover from a call where no functions were found. |
| 6585 | /// |
| 6586 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6587 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6588 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6589 | UnresolvedLookupExpr *ULE, |
| 6590 | SourceLocation LParenLoc, |
| 6591 | Expr **Args, unsigned NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6592 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6593 | |
| 6594 | CXXScopeSpec SS; |
| 6595 | if (ULE->getQualifier()) { |
| 6596 | SS.setScopeRep(ULE->getQualifier()); |
| 6597 | SS.setRange(ULE->getQualifierRange()); |
| 6598 | } |
| 6599 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6600 | TemplateArgumentListInfo TABuffer; |
| 6601 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6602 | if (ULE->hasExplicitTemplateArgs()) { |
| 6603 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6604 | ExplicitTemplateArgs = &TABuffer; |
| 6605 | } |
| 6606 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6607 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 6608 | Sema::LookupOrdinaryName); |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 6609 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6610 | return ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6611 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6612 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 6613 | |
| 6614 | // Build an implicit member call if appropriate. Just drop the |
| 6615 | // casts and such from the call, we don't really care. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6616 | ExprResult NewFn = ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6617 | if ((*R.begin())->isCXXClassMember()) |
| 6618 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 6619 | else if (ExplicitTemplateArgs) |
| 6620 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 6621 | else |
| 6622 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 6623 | |
| 6624 | if (NewFn.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6625 | return ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6626 | |
| 6627 | // This shouldn't cause an infinite loop because we're giving it |
| 6628 | // an expression with non-empty lookup results, which should never |
| 6629 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6630 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame^] | 6631 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6632 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6634 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6635 | /// (which eventually refers to the declaration Func) and the call |
| 6636 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 6637 | /// to a specific function. If overload resolution succeeds, returns |
| 6638 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 6639 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6640 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6641 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6642 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6643 | SourceLocation LParenLoc, |
| 6644 | Expr **Args, unsigned NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6645 | SourceLocation RParenLoc) { |
| 6646 | #ifndef NDEBUG |
| 6647 | if (ULE->requiresADL()) { |
| 6648 | // To do ADL, we must have found an unqualified name. |
| 6649 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 6650 | |
| 6651 | // We don't perform ADL for implicit declarations of builtins. |
| 6652 | // Verify that this was correctly set up. |
| 6653 | FunctionDecl *F; |
| 6654 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 6655 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 6656 | F->getBuiltinID() && F->isImplicit()) |
| 6657 | assert(0 && "performing ADL for builtin"); |
| 6658 | |
| 6659 | // We don't perform ADL in C. |
| 6660 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 6661 | } |
| 6662 | #endif |
| 6663 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6664 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 6665 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6666 | // Add the functions denoted by the callee to the set of candidate |
| 6667 | // functions, including those from argument-dependent lookup. |
| 6668 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6669 | |
| 6670 | // If we found nothing, try to recover. |
| 6671 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 6672 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6673 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6674 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame^] | 6675 | RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6676 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6677 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6678 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6679 | case OR_Success: { |
| 6680 | FunctionDecl *FDecl = Best->Function; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6681 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6682 | DiagnoseUseOfDecl(Best->FoundDecl, ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6683 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6684 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 6685 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6686 | |
| 6687 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 6688 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6689 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6690 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6691 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6692 | break; |
| 6693 | |
| 6694 | case OR_Ambiguous: |
| 6695 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6696 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6697 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6698 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6699 | |
| 6700 | case OR_Deleted: |
| 6701 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 6702 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6703 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6704 | << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6705 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6706 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6709 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6710 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6713 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6714 | return Functions.size() > 1 || |
| 6715 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 6716 | } |
| 6717 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6718 | /// \brief Create a unary operation that may resolve to an overloaded |
| 6719 | /// operator. |
| 6720 | /// |
| 6721 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 6722 | /// |
| 6723 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 6724 | /// operator. |
| 6725 | /// |
| 6726 | /// \param Functions The set of non-member functions that will be |
| 6727 | /// considered by overload resolution. The caller needs to build this |
| 6728 | /// set based on the context using, e.g., |
| 6729 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6730 | /// set should not contain any member functions; those will be added |
| 6731 | /// by CreateOverloadedUnaryOp(). |
| 6732 | /// |
| 6733 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6734 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6735 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 6736 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6737 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6738 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6739 | |
| 6740 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 6741 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 6742 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6743 | // TODO: provide better source location info. |
| 6744 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6745 | |
| 6746 | Expr *Args[2] = { Input, 0 }; |
| 6747 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6748 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6749 | // For post-increment and post-decrement, add the implicit '0' as |
| 6750 | // the second argument, so that we know this is a post-increment or |
| 6751 | // post-decrement. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6752 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6753 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 6754 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 6755 | SourceLocation()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6756 | NumArgs = 2; |
| 6757 | } |
| 6758 | |
| 6759 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 6760 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6761 | return Owned(new (Context) UnaryOperator(Input, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 6762 | Opc, |
| 6763 | Context.DependentTy, |
| 6764 | OpLoc)); |
| 6765 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6766 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6767 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6768 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6769 | 0, SourceRange(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6770 | /*ADL*/ true, IsOverloaded(Fns), |
| 6771 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6772 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 6773 | &Args[0], NumArgs, |
| 6774 | Context.DependentTy, |
| 6775 | OpLoc)); |
| 6776 | } |
| 6777 | |
| 6778 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6779 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6780 | |
| 6781 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6782 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6783 | |
| 6784 | // Add operator candidates that are member functions. |
| 6785 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 6786 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6787 | // Add candidates from ADL. |
| 6788 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 6789 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6790 | /*ExplicitTemplateArgs*/ 0, |
| 6791 | CandidateSet); |
| 6792 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6793 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6794 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6795 | |
| 6796 | // Perform overload resolution. |
| 6797 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6798 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6799 | case OR_Success: { |
| 6800 | // We found a built-in operator or an overloaded operator. |
| 6801 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6802 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6803 | if (FnDecl) { |
| 6804 | // We matched an overloaded operator. Build a call to that |
| 6805 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6806 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6807 | // Convert the arguments. |
| 6808 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6809 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6810 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6811 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 6812 | Best->FoundDecl, Method)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6813 | return ExprError(); |
| 6814 | } else { |
| 6815 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6816 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6817 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6818 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6819 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6820 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6821 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6822 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6823 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6826 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6827 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6828 | // Determine the result type |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 6829 | QualType ResultTy = FnDecl->getCallResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6830 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6831 | // Build the actual expression node. |
| 6832 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6833 | SourceLocation()); |
| 6834 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6835 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6836 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6837 | CallExpr *TheCall = |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6838 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6839 | Args, NumArgs, ResultTy, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6840 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6841 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6842 | FnDecl)) |
| 6843 | return ExprError(); |
| 6844 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6845 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6846 | } else { |
| 6847 | // We matched a built-in operator. Convert the arguments, then |
| 6848 | // break out so that we will build the appropriate built-in |
| 6849 | // operator node. |
| 6850 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6851 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6852 | return ExprError(); |
| 6853 | |
| 6854 | break; |
| 6855 | } |
| 6856 | } |
| 6857 | |
| 6858 | case OR_No_Viable_Function: |
| 6859 | // No viable function; fall through to handling this as a |
| 6860 | // built-in operator, which will produce an error message for us. |
| 6861 | break; |
| 6862 | |
| 6863 | case OR_Ambiguous: |
| 6864 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6865 | << UnaryOperator::getOpcodeStr(Opc) |
| 6866 | << Input->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6867 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 6868 | Args, NumArgs, |
| 6869 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6870 | return ExprError(); |
| 6871 | |
| 6872 | case OR_Deleted: |
| 6873 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6874 | << Best->Function->isDeleted() |
| 6875 | << UnaryOperator::getOpcodeStr(Opc) |
| 6876 | << Input->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6877 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6878 | return ExprError(); |
| 6879 | } |
| 6880 | |
| 6881 | // Either we found no viable overloaded operator or we matched a |
| 6882 | // built-in operator. In either case, fall through to trying to |
| 6883 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6884 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6887 | /// \brief Create a binary operation that may resolve to an overloaded |
| 6888 | /// operator. |
| 6889 | /// |
| 6890 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 6891 | /// |
| 6892 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 6893 | /// operator. |
| 6894 | /// |
| 6895 | /// \param Functions The set of non-member functions that will be |
| 6896 | /// considered by overload resolution. The caller needs to build this |
| 6897 | /// set based on the context using, e.g., |
| 6898 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6899 | /// set should not contain any member functions; those will be added |
| 6900 | /// by CreateOverloadedBinOp(). |
| 6901 | /// |
| 6902 | /// \param LHS Left-hand argument. |
| 6903 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6904 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6905 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6906 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6907 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6908 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6909 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6910 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6911 | |
| 6912 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 6913 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 6914 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6915 | |
| 6916 | // If either side is type-dependent, create an appropriate dependent |
| 6917 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6918 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6919 | if (Fns.empty()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6920 | // If there are no functions to store, just build a dependent |
| 6921 | // BinaryOperator or CompoundAssignment. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6922 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6923 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6924 | Context.DependentTy, OpLoc)); |
| 6925 | |
| 6926 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6927 | Context.DependentTy, |
| 6928 | Context.DependentTy, |
| 6929 | Context.DependentTy, |
| 6930 | OpLoc)); |
| 6931 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6932 | |
| 6933 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6934 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6935 | // TODO: provide better source location info in DNLoc component. |
| 6936 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6937 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6938 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6939 | 0, SourceRange(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6940 | /*ADL*/ true, IsOverloaded(Fns), |
| 6941 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6942 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6943 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6944 | Context.DependentTy, |
| 6945 | OpLoc)); |
| 6946 | } |
| 6947 | |
| 6948 | // If this is the .* operator, which is not overloadable, just |
| 6949 | // create a built-in binary operator. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6950 | if (Opc == BO_PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6951 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6952 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6953 | // If this is the assignment operator, we only perform overload resolution |
| 6954 | // if the left-hand side is a class or enumeration type. This is actually |
| 6955 | // a hack. The standard requires that we do overload resolution between the |
| 6956 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6957 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6958 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6959 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6960 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6961 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6962 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6963 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6964 | |
| 6965 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6966 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6967 | |
| 6968 | // Add operator candidates that are member functions. |
| 6969 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6970 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6971 | // Add candidates from ADL. |
| 6972 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6973 | Args, 2, |
| 6974 | /*ExplicitTemplateArgs*/ 0, |
| 6975 | CandidateSet); |
| 6976 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6977 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6978 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6979 | |
| 6980 | // Perform overload resolution. |
| 6981 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6982 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6983 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6984 | // We found a built-in operator or an overloaded operator. |
| 6985 | FunctionDecl *FnDecl = Best->Function; |
| 6986 | |
| 6987 | if (FnDecl) { |
| 6988 | // We matched an overloaded operator. Build a call to that |
| 6989 | // operator. |
| 6990 | |
| 6991 | // Convert the arguments. |
| 6992 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6993 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6994 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6995 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6996 | ExprResult Arg1 |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6997 | = PerformCopyInitialization( |
| 6998 | InitializedEntity::InitializeParameter( |
| 6999 | FnDecl->getParamDecl(0)), |
| 7000 | SourceLocation(), |
| 7001 | Owned(Args[1])); |
| 7002 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7003 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7004 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7005 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7006 | Best->FoundDecl, Method)) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7007 | return ExprError(); |
| 7008 | |
| 7009 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7010 | } else { |
| 7011 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7012 | ExprResult Arg0 |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7013 | = PerformCopyInitialization( |
| 7014 | InitializedEntity::InitializeParameter( |
| 7015 | FnDecl->getParamDecl(0)), |
| 7016 | SourceLocation(), |
| 7017 | Owned(Args[0])); |
| 7018 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7019 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7020 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7021 | ExprResult Arg1 |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7022 | = PerformCopyInitialization( |
| 7023 | InitializedEntity::InitializeParameter( |
| 7024 | FnDecl->getParamDecl(1)), |
| 7025 | SourceLocation(), |
| 7026 | Owned(Args[1])); |
| 7027 | if (Arg1.isInvalid()) |
| 7028 | return ExprError(); |
| 7029 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 7030 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7031 | } |
| 7032 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7033 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 7034 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7035 | // Determine the result type |
| 7036 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7037 | = FnDecl->getType()->getAs<FunctionType>() |
| 7038 | ->getCallResultType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7039 | |
| 7040 | // Build the actual expression node. |
| 7041 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 7042 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7043 | UsualUnaryConversions(FnExpr); |
| 7044 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7045 | CXXOperatorCallExpr *TheCall = |
| 7046 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 7047 | Args, 2, ResultTy, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7048 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7049 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7050 | FnDecl)) |
| 7051 | return ExprError(); |
| 7052 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7053 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7054 | } else { |
| 7055 | // We matched a built-in operator. Convert the arguments, then |
| 7056 | // break out so that we will build the appropriate built-in |
| 7057 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7058 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7059 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7060 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7061 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7062 | return ExprError(); |
| 7063 | |
| 7064 | break; |
| 7065 | } |
| 7066 | } |
| 7067 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7068 | case OR_No_Viable_Function: { |
| 7069 | // C++ [over.match.oper]p9: |
| 7070 | // If the operator is the operator , [...] and there are no |
| 7071 | // viable functions, then the operator is assumed to be the |
| 7072 | // built-in operator and interpreted according to clause 5. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7073 | if (Opc == BO_Comma) |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7074 | break; |
| 7075 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 7076 | // For class as left operand for assignment or compound assigment operator |
| 7077 | // do not fall through to handling in built-in, but report that no overloaded |
| 7078 | // assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7079 | ExprResult Result = ExprError(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7080 | if (Args[0]->getType()->isRecordType() && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7081 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 7082 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 7083 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7084 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7085 | } else { |
| 7086 | // No viable function; try to create a built-in operation, which will |
| 7087 | // produce an error. Then, show the non-viable candidates. |
| 7088 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 7089 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7090 | assert(Result.isInvalid() && |
| 7091 | "C++ binary operator overloading is missing candidates!"); |
| 7092 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7093 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 7094 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7095 | return move(Result); |
| 7096 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7097 | |
| 7098 | case OR_Ambiguous: |
| 7099 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 7100 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7101 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7102 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 7103 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7104 | return ExprError(); |
| 7105 | |
| 7106 | case OR_Deleted: |
| 7107 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7108 | << Best->Function->isDeleted() |
| 7109 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7110 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7111 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7112 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7113 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7114 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7115 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7116 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7117 | } |
| 7118 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7119 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7120 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 7121 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7122 | Expr *Base, Expr *Idx) { |
| 7123 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7124 | DeclarationName OpName = |
| 7125 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 7126 | |
| 7127 | // If either side is type-dependent, create an appropriate dependent |
| 7128 | // expression. |
| 7129 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 7130 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7131 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7132 | // CHECKME: no 'operator' keyword? |
| 7133 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 7134 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7135 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7136 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7137 | 0, SourceRange(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 7138 | /*ADL*/ true, /*Overloaded*/ false, |
| 7139 | UnresolvedSetIterator(), |
| 7140 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7141 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7142 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7143 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 7144 | Args, 2, |
| 7145 | Context.DependentTy, |
| 7146 | RLoc)); |
| 7147 | } |
| 7148 | |
| 7149 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7150 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7151 | |
| 7152 | // Subscript can only be overloaded as a member function. |
| 7153 | |
| 7154 | // Add operator candidates that are member functions. |
| 7155 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7156 | |
| 7157 | // Add builtin operator candidates. |
| 7158 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7159 | |
| 7160 | // Perform overload resolution. |
| 7161 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7162 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7163 | case OR_Success: { |
| 7164 | // We found a built-in operator or an overloaded operator. |
| 7165 | FunctionDecl *FnDecl = Best->Function; |
| 7166 | |
| 7167 | if (FnDecl) { |
| 7168 | // We matched an overloaded operator. Build a call to that |
| 7169 | // operator. |
| 7170 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7171 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7172 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7173 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7174 | // Convert the arguments. |
| 7175 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7176 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7177 | Best->FoundDecl, Method)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7178 | return ExprError(); |
| 7179 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 7180 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7181 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 7182 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7183 | FnDecl->getParamDecl(0)), |
| 7184 | SourceLocation(), |
| 7185 | Owned(Args[1])); |
| 7186 | if (InputInit.isInvalid()) |
| 7187 | return ExprError(); |
| 7188 | |
| 7189 | Args[1] = InputInit.takeAs<Expr>(); |
| 7190 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7191 | // Determine the result type |
| 7192 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7193 | = FnDecl->getType()->getAs<FunctionType>() |
| 7194 | ->getCallResultType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7195 | |
| 7196 | // Build the actual expression node. |
| 7197 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 7198 | LLoc); |
| 7199 | UsualUnaryConversions(FnExpr); |
| 7200 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7201 | CXXOperatorCallExpr *TheCall = |
| 7202 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 7203 | FnExpr, Args, 2, |
| 7204 | ResultTy, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7205 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7206 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7207 | FnDecl)) |
| 7208 | return ExprError(); |
| 7209 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7210 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7211 | } else { |
| 7212 | // We matched a built-in operator. Convert the arguments, then |
| 7213 | // break out so that we will build the appropriate built-in |
| 7214 | // operator node. |
| 7215 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7216 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7217 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7218 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7219 | return ExprError(); |
| 7220 | |
| 7221 | break; |
| 7222 | } |
| 7223 | } |
| 7224 | |
| 7225 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7226 | if (CandidateSet.empty()) |
| 7227 | Diag(LLoc, diag::err_ovl_no_oper) |
| 7228 | << Args[0]->getType() << /*subscript*/ 0 |
| 7229 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 7230 | else |
| 7231 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 7232 | << Args[0]->getType() |
| 7233 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7234 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 7235 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7236 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7237 | } |
| 7238 | |
| 7239 | case OR_Ambiguous: |
| 7240 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 7241 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7242 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 7243 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7244 | return ExprError(); |
| 7245 | |
| 7246 | case OR_Deleted: |
| 7247 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 7248 | << Best->Function->isDeleted() << "[]" |
| 7249 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7250 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 7251 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7252 | return ExprError(); |
| 7253 | } |
| 7254 | |
| 7255 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7256 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7257 | } |
| 7258 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7259 | /// BuildCallToMemberFunction - Build a call to a member |
| 7260 | /// function. MemExpr is the expression that refers to the member |
| 7261 | /// function (and includes the object parameter), Args/NumArgs are the |
| 7262 | /// arguments to the function call (not including the object |
| 7263 | /// parameter). The caller needs to validate that the member |
| 7264 | /// expression refers to a member function or an overloaded member |
| 7265 | /// function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7266 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7267 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 7268 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame^] | 7269 | unsigned NumArgs, SourceLocation RParenLoc) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7270 | // Dig out the member expression. This holds both the object |
| 7271 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7272 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 7273 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7274 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7275 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 7276 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7277 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7278 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 7279 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7280 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7281 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7282 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7283 | } else { |
| 7284 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7285 | Qualifier = UnresExpr->getQualifier(); |
| 7286 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7287 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7288 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7289 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7290 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7291 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7292 | // FIXME: avoid copy. |
| 7293 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7294 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 7295 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7296 | TemplateArgs = &TemplateArgsBuffer; |
| 7297 | } |
| 7298 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7299 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 7300 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 7301 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7302 | NamedDecl *Func = *I; |
| 7303 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 7304 | if (isa<UsingShadowDecl>(Func)) |
| 7305 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 7306 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7307 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7308 | // If explicit template arguments were provided, we can't call a |
| 7309 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7310 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7311 | continue; |
| 7312 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7313 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7314 | Args, NumArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7315 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7316 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7317 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7318 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7319 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7320 | CandidateSet, |
| 7321 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7322 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7324 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7325 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 7326 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7327 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7328 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
| 7329 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7330 | case OR_Success: |
| 7331 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7332 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7333 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7334 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7335 | break; |
| 7336 | |
| 7337 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7338 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7339 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7340 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7341 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7342 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7343 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7344 | |
| 7345 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7346 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7347 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7348 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7349 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7350 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7351 | |
| 7352 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7353 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7354 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7355 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7356 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7357 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7358 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7359 | } |
| 7360 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7361 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7362 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7363 | // If overload resolution picked a static member, build a |
| 7364 | // non-member call based on that function. |
| 7365 | if (Method->isStatic()) { |
| 7366 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 7367 | Args, NumArgs, RParenLoc); |
| 7368 | } |
| 7369 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7370 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
| 7373 | assert(Method && "Member call to something that isn't a method?"); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7374 | CXXMemberCallExpr *TheCall = |
| 7375 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 7376 | Method->getCallResultType(), |
| 7377 | RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7378 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7379 | // Check for a valid return type. |
| 7380 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7381 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7382 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7383 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7384 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7385 | // We only need to do this if there was actually an overload; otherwise |
| 7386 | // it was done at lookup. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7387 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7388 | if (!Method->isStatic() && |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7389 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 7390 | FoundDecl, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7391 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7392 | MemExpr->setBase(ObjectArg); |
| 7393 | |
| 7394 | // Convert the rest of the arguments |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 7395 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7396 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7397 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7398 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7399 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7400 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7401 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 7402 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7403 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7406 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 7407 | /// type (C++ [over.call.object]), which can end up invoking an |
| 7408 | /// overloaded function call operator (@c operator()) or performing a |
| 7409 | /// user-defined conversion on the object argument. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7410 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7411 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 7412 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7413 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7414 | SourceLocation RParenLoc) { |
| 7415 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7416 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7417 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7418 | // C++ [over.call.object]p1: |
| 7419 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7420 | // 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] | 7421 | // candidate functions includes at least the function call |
| 7422 | // operators of T. The function call operators of T are obtained by |
| 7423 | // ordinary lookup of the name operator() in the context of |
| 7424 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7425 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7426 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7427 | |
| 7428 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7429 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7430 | << Object->getSourceRange())) |
| 7431 | return true; |
| 7432 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7433 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 7434 | LookupQualifiedName(R, Record->getDecl()); |
| 7435 | R.suppressDiagnostics(); |
| 7436 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7437 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7438 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7439 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7440 | Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 7441 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7442 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7443 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7444 | // C++ [over.call.object]p2: |
| 7445 | // In addition, for each conversion function declared in T of the |
| 7446 | // form |
| 7447 | // |
| 7448 | // operator conversion-type-id () cv-qualifier; |
| 7449 | // |
| 7450 | // where cv-qualifier is the same cv-qualification as, or a |
| 7451 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 7452 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 7453 | // R", or the type "reference to pointer to function of |
| 7454 | // (P1,...,Pn) returning R", or the type "reference to function |
| 7455 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7456 | // is also considered as a candidate function. Similarly, |
| 7457 | // surrogate call functions are added to the set of candidate |
| 7458 | // functions for each conversion function declared in an |
| 7459 | // accessible base class provided the function is not hidden |
| 7460 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7461 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 7462 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7463 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7464 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7465 | NamedDecl *D = *I; |
| 7466 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 7467 | if (isa<UsingShadowDecl>(D)) |
| 7468 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7469 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7470 | // Skip over templated conversion functions; they aren't |
| 7471 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7472 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7473 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7474 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7475 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7476 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7477 | // Strip the reference type (if any) and then the pointer type (if |
| 7478 | // any) to get down to what might be a function type. |
| 7479 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 7480 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7481 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7482 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7483 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7484 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7485 | Object->getType(), Args, NumArgs, |
| 7486 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7488 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7489 | // Perform overload resolution. |
| 7490 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7491 | switch (CandidateSet.BestViableFunction(*this, Object->getLocStart(), |
| 7492 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7493 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7494 | // Overload resolution succeeded; we'll build the appropriate call |
| 7495 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7496 | break; |
| 7497 | |
| 7498 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7499 | if (CandidateSet.empty()) |
| 7500 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 7501 | << Object->getType() << /*call*/ 1 |
| 7502 | << Object->getSourceRange(); |
| 7503 | else |
| 7504 | Diag(Object->getSourceRange().getBegin(), |
| 7505 | diag::err_ovl_no_viable_object_call) |
| 7506 | << Object->getType() << Object->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7507 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7508 | break; |
| 7509 | |
| 7510 | case OR_Ambiguous: |
| 7511 | Diag(Object->getSourceRange().getBegin(), |
| 7512 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7513 | << Object->getType() << Object->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7514 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7515 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7516 | |
| 7517 | case OR_Deleted: |
| 7518 | Diag(Object->getSourceRange().getBegin(), |
| 7519 | diag::err_ovl_deleted_object_call) |
| 7520 | << Best->Function->isDeleted() |
| 7521 | << Object->getType() << Object->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7522 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7523 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7524 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7525 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7526 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7527 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7528 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7529 | if (Best->Function == 0) { |
| 7530 | // Since there is no function declaration, this is one of the |
| 7531 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7532 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7533 | = cast<CXXConversionDecl>( |
| 7534 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 7535 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7536 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7537 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7538 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7539 | // We selected one of the surrogate functions that converts the |
| 7540 | // object parameter to a function pointer. Perform the conversion |
| 7541 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7542 | |
| 7543 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7544 | // and then call it. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7545 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 7546 | Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7547 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7548 | return ActOnCallExpr(S, CE, LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame^] | 7549 | RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7552 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7553 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7554 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7555 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 7556 | // that calls this method, using Object for the implicit object |
| 7557 | // parameter and passing along the remaining arguments. |
| 7558 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7559 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7560 | |
| 7561 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7562 | unsigned NumArgsToCheck = NumArgs; |
| 7563 | |
| 7564 | // Build the full argument list for the method call (the |
| 7565 | // implicit object parameter is placed at the beginning of the |
| 7566 | // list). |
| 7567 | Expr **MethodArgs; |
| 7568 | if (NumArgs < NumArgsInProto) { |
| 7569 | NumArgsToCheck = NumArgsInProto; |
| 7570 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 7571 | } else { |
| 7572 | MethodArgs = new Expr*[NumArgs + 1]; |
| 7573 | } |
| 7574 | MethodArgs[0] = Object; |
| 7575 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7576 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7577 | |
| 7578 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7579 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7580 | UsualUnaryConversions(NewFn); |
| 7581 | |
| 7582 | // Once we've built TheCall, all of the expressions are properly |
| 7583 | // owned. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7584 | QualType ResultTy = Method->getCallResultType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7585 | CXXOperatorCallExpr *TheCall = |
| 7586 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
| 7587 | MethodArgs, NumArgs + 1, |
| 7588 | ResultTy, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7589 | delete [] MethodArgs; |
| 7590 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7591 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 7592 | Method)) |
| 7593 | return true; |
| 7594 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7595 | // We may have default arguments. If so, we need to allocate more |
| 7596 | // slots in the call for them. |
| 7597 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7598 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7599 | else if (NumArgs > NumArgsInProto) |
| 7600 | NumArgsToCheck = NumArgsInProto; |
| 7601 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7602 | bool IsError = false; |
| 7603 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7604 | // Initialize the implicit object parameter. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7605 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7606 | Best->FoundDecl, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7607 | TheCall->setArg(0, Object); |
| 7608 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7609 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7610 | // Check the argument types. |
| 7611 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7612 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7613 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7614 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7615 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7616 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7617 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7618 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7619 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7620 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7621 | SourceLocation(), Arg); |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7622 | |
| 7623 | IsError |= InputInit.isInvalid(); |
| 7624 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7625 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7626 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 7627 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 7628 | if (DefArg.isInvalid()) { |
| 7629 | IsError = true; |
| 7630 | break; |
| 7631 | } |
| 7632 | |
| 7633 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7634 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7635 | |
| 7636 | TheCall->setArg(i + 1, Arg); |
| 7637 | } |
| 7638 | |
| 7639 | // If this is a variadic call, handle args passed through "...". |
| 7640 | if (Proto->isVariadic()) { |
| 7641 | // Promote the arguments (C99 6.5.2.2p7). |
| 7642 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 7643 | Expr *Arg = Args[i]; |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 7644 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod, 0); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7645 | TheCall->setArg(i + 1, Arg); |
| 7646 | } |
| 7647 | } |
| 7648 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7649 | if (IsError) return true; |
| 7650 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7651 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7652 | return true; |
| 7653 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 7654 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7655 | } |
| 7656 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7657 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7658 | /// (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] | 7659 | /// @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] | 7660 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7661 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7662 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7663 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7664 | SourceLocation Loc = Base->getExprLoc(); |
| 7665 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7666 | // C++ [over.ref]p1: |
| 7667 | // |
| 7668 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 7669 | // for a class object x of type T if T::operator->() exists and if |
| 7670 | // the operator is selected as the best match function by the |
| 7671 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7672 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7673 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7674 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7675 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7676 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 7677 | PDiag(diag::err_typecheck_incomplete_tag) |
| 7678 | << Base->getSourceRange())) |
| 7679 | return ExprError(); |
| 7680 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7681 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7682 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 7683 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7684 | |
| 7685 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7686 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7687 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7688 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7689 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7690 | |
| 7691 | // Perform overload resolution. |
| 7692 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7693 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7694 | case OR_Success: |
| 7695 | // Overload resolution succeeded; we'll build the call below. |
| 7696 | break; |
| 7697 | |
| 7698 | case OR_No_Viable_Function: |
| 7699 | if (CandidateSet.empty()) |
| 7700 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7701 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7702 | else |
| 7703 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7704 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7705 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7706 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7707 | |
| 7708 | case OR_Ambiguous: |
| 7709 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7710 | << "->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7711 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7712 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7713 | |
| 7714 | case OR_Deleted: |
| 7715 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7716 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7717 | << "->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7718 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7719 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7720 | } |
| 7721 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7722 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7723 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7724 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7725 | // Convert the object parameter. |
| 7726 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7727 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 7728 | Best->FoundDecl, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7729 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 7730 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7731 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7732 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 7733 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7734 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7735 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7736 | QualType ResultTy = Method->getCallResultType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7737 | CXXOperatorCallExpr *TheCall = |
| 7738 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 7739 | &Base, 1, ResultTy, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7740 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7741 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7742 | Method)) |
| 7743 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7744 | return Owned(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7745 | } |
| 7746 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7747 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 7748 | /// a C++ overloaded function (possibly with some parentheses and |
| 7749 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 7750 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7751 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7752 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7753 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7754 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7755 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 7756 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7757 | if (SubExpr == PE->getSubExpr()) |
| 7758 | return PE->Retain(); |
| 7759 | |
| 7760 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 7761 | } |
| 7762 | |
| 7763 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7764 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 7765 | Found, Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7766 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7767 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7768 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7769 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7770 | if (SubExpr == ICE->getSubExpr()) |
| 7771 | return ICE->Retain(); |
| 7772 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7773 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
| 7774 | ICE->getCastKind(), |
| 7775 | SubExpr, 0, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7776 | ICE->getValueKind()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7780 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7781 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7782 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7783 | if (Method->isStatic()) { |
| 7784 | // Do nothing: static member functions aren't any different |
| 7785 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7786 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7787 | // Fix the sub expression, which really has to be an |
| 7788 | // UnresolvedLookupExpr holding an overloaded member function |
| 7789 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7790 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7791 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7792 | if (SubExpr == UnOp->getSubExpr()) |
| 7793 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7794 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7795 | assert(isa<DeclRefExpr>(SubExpr) |
| 7796 | && "fixed to something other than a decl ref"); |
| 7797 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 7798 | && "fixed to a member ref with no nested name qualifier"); |
| 7799 | |
| 7800 | // We have taken the address of a pointer to member |
| 7801 | // function. Perform the computation here so that we get the |
| 7802 | // appropriate pointer to member type. |
| 7803 | QualType ClassType |
| 7804 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 7805 | QualType MemPtrType |
| 7806 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 7807 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7808 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7809 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7810 | } |
| 7811 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7812 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7813 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7814 | if (SubExpr == UnOp->getSubExpr()) |
| 7815 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7816 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7817 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7818 | Context.getPointerType(SubExpr->getType()), |
| 7819 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7820 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7821 | |
| 7822 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7823 | // FIXME: avoid copy. |
| 7824 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7825 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7826 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7827 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7828 | } |
| 7829 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7830 | return DeclRefExpr::Create(Context, |
| 7831 | ULE->getQualifier(), |
| 7832 | ULE->getQualifierRange(), |
| 7833 | Fn, |
| 7834 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7835 | Fn->getType(), |
| 7836 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7839 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7840 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7841 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7842 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 7843 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7844 | TemplateArgs = &TemplateArgsBuffer; |
| 7845 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7846 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7847 | Expr *Base; |
| 7848 | |
| 7849 | // If we're filling in |
| 7850 | if (MemExpr->isImplicitAccess()) { |
| 7851 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 7852 | return DeclRefExpr::Create(Context, |
| 7853 | MemExpr->getQualifier(), |
| 7854 | MemExpr->getQualifierRange(), |
| 7855 | Fn, |
| 7856 | MemExpr->getMemberLoc(), |
| 7857 | Fn->getType(), |
| 7858 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 7859 | } else { |
| 7860 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 7861 | if (MemExpr->getQualifier()) |
| 7862 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 7863 | Base = new (Context) CXXThisExpr(Loc, |
| 7864 | MemExpr->getBaseType(), |
| 7865 | /*isImplicit=*/true); |
| 7866 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7867 | } else |
| 7868 | Base = MemExpr->getBase()->Retain(); |
| 7869 | |
| 7870 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7871 | MemExpr->isArrow(), |
| 7872 | MemExpr->getQualifier(), |
| 7873 | MemExpr->getQualifierRange(), |
| 7874 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7875 | Found, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7876 | MemExpr->getMemberNameInfo(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7877 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7878 | Fn->getType()); |
| 7879 | } |
| 7880 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7881 | assert(false && "Invalid reference to overloaded function"); |
| 7882 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7883 | } |
| 7884 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7885 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
| 7886 | DeclAccessPair Found, |
| 7887 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7888 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7891 | } // end namespace clang |