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 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Sema.h" |
| 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" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/CXXInheritance.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 26 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | |
| 31 | namespace clang { |
| 32 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 33 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 34 | bool InOverloadResolution, |
| 35 | StandardConversionSequence &SCS); |
| 36 | static OverloadingResult |
| 37 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 38 | UserDefinedConversionSequence& User, |
| 39 | OverloadCandidateSet& Conversions, |
| 40 | bool AllowExplicit); |
| 41 | |
| 42 | |
| 43 | static ImplicitConversionSequence::CompareKind |
| 44 | CompareStandardConversionSequences(Sema &S, |
| 45 | const StandardConversionSequence& SCS1, |
| 46 | const StandardConversionSequence& SCS2); |
| 47 | |
| 48 | static ImplicitConversionSequence::CompareKind |
| 49 | CompareQualificationConversions(Sema &S, |
| 50 | const StandardConversionSequence& SCS1, |
| 51 | const StandardConversionSequence& SCS2); |
| 52 | |
| 53 | static ImplicitConversionSequence::CompareKind |
| 54 | CompareDerivedToBaseConversions(Sema &S, |
| 55 | const StandardConversionSequence& SCS1, |
| 56 | const StandardConversionSequence& SCS2); |
| 57 | |
| 58 | |
| 59 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 60 | /// GetConversionCategory - Retrieve the implicit conversion |
| 61 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 63 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 64 | static const ImplicitConversionCategory |
| 65 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 66 | ICC_Identity, |
| 67 | ICC_Lvalue_Transformation, |
| 68 | ICC_Lvalue_Transformation, |
| 69 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 70 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 71 | ICC_Qualification_Adjustment, |
| 72 | ICC_Promotion, |
| 73 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 74 | ICC_Promotion, |
| 75 | ICC_Conversion, |
| 76 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 77 | ICC_Conversion, |
| 78 | ICC_Conversion, |
| 79 | ICC_Conversion, |
| 80 | ICC_Conversion, |
| 81 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 82 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 83 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 84 | ICC_Conversion, |
| 85 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 86 | ICC_Conversion |
| 87 | }; |
| 88 | return Category[(int)Kind]; |
| 89 | } |
| 90 | |
| 91 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 92 | /// corresponding to the given implicit conversion kind. |
| 93 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 94 | static const ImplicitConversionRank |
| 95 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 96 | ICR_Exact_Match, |
| 97 | ICR_Exact_Match, |
| 98 | ICR_Exact_Match, |
| 99 | ICR_Exact_Match, |
| 100 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 101 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 102 | ICR_Promotion, |
| 103 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 104 | ICR_Promotion, |
| 105 | ICR_Conversion, |
| 106 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | ICR_Conversion, |
| 108 | ICR_Conversion, |
| 109 | ICR_Conversion, |
| 110 | ICR_Conversion, |
| 111 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 112 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 113 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 114 | ICR_Conversion, |
| 115 | ICR_Conversion, |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 116 | ICR_Complex_Real_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 117 | }; |
| 118 | return Rank[(int)Kind]; |
| 119 | } |
| 120 | |
| 121 | /// GetImplicitConversionName - Return the name of this kind of |
| 122 | /// implicit conversion. |
| 123 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 124 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 125 | "No conversion", |
| 126 | "Lvalue-to-rvalue", |
| 127 | "Array-to-pointer", |
| 128 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 129 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 130 | "Qualification", |
| 131 | "Integral promotion", |
| 132 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 133 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 134 | "Integral conversion", |
| 135 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 136 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 137 | "Floating-integral conversion", |
| 138 | "Pointer conversion", |
| 139 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 140 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 141 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 142 | "Derived-to-base conversion", |
| 143 | "Vector conversion", |
| 144 | "Vector splat", |
| 145 | "Complex-real conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 146 | }; |
| 147 | return Name[Kind]; |
| 148 | } |
| 149 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 150 | /// StandardConversionSequence - Set the standard conversion |
| 151 | /// sequence to the identity conversion. |
| 152 | void StandardConversionSequence::setAsIdentityConversion() { |
| 153 | First = ICK_Identity; |
| 154 | Second = ICK_Identity; |
| 155 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 156 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 157 | ReferenceBinding = false; |
| 158 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 159 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 160 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 163 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 164 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 165 | /// implicit conversions. |
| 166 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 167 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 168 | if (GetConversionRank(First) > Rank) |
| 169 | Rank = GetConversionRank(First); |
| 170 | if (GetConversionRank(Second) > Rank) |
| 171 | Rank = GetConversionRank(Second); |
| 172 | if (GetConversionRank(Third) > Rank) |
| 173 | Rank = GetConversionRank(Third); |
| 174 | return Rank; |
| 175 | } |
| 176 | |
| 177 | /// isPointerConversionToBool - Determines whether this conversion is |
| 178 | /// 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] | 179 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 180 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | // Note that FromType has not necessarily been transformed by the |
| 183 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 184 | // check for their presence as well as checking whether FromType is |
| 185 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 186 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 187 | (getFromType()->isPointerType() || |
| 188 | getFromType()->isObjCObjectPointerType() || |
| 189 | getFromType()->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 190 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 191 | return true; |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 196 | /// isPointerConversionToVoidPointer - Determines whether this |
| 197 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 198 | /// used as part of the ranking of standard conversion sequences (C++ |
| 199 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 201 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 203 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 204 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 205 | |
| 206 | // Note that FromType has not necessarily been transformed by the |
| 207 | // array-to-pointer implicit conversion, so check for its presence |
| 208 | // and redo the conversion to get a pointer. |
| 209 | if (First == ICK_Array_To_Pointer) |
| 210 | FromType = Context.getArrayDecayedType(FromType); |
| 211 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 212 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 213 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 214 | return ToPtrType->getPointeeType()->isVoidType(); |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | /// DebugPrint - Print this standard conversion sequence to standard |
| 220 | /// error. Useful for debugging overloading issues. |
| 221 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 222 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 223 | bool PrintedSomething = false; |
| 224 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 225 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 226 | PrintedSomething = true; |
| 227 | } |
| 228 | |
| 229 | if (Second != ICK_Identity) { |
| 230 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 231 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 233 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 234 | |
| 235 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 236 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 237 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 238 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 239 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 240 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 241 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 242 | PrintedSomething = true; |
| 243 | } |
| 244 | |
| 245 | if (Third != ICK_Identity) { |
| 246 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 247 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 248 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 249 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 250 | PrintedSomething = true; |
| 251 | } |
| 252 | |
| 253 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 254 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 259 | /// error. Useful for debugging overloading issues. |
| 260 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 261 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 262 | if (Before.First || Before.Second || Before.Third) { |
| 263 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 264 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 265 | } |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 266 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 267 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 268 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 269 | After.DebugPrint(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 274 | /// error. Useful for debugging overloading issues. |
| 275 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 276 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 277 | switch (ConversionKind) { |
| 278 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 279 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 280 | Standard.DebugPrint(); |
| 281 | break; |
| 282 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 283 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 284 | UserDefined.DebugPrint(); |
| 285 | break; |
| 286 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 287 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 288 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 289 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 290 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 291 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 292 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 293 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 297 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 298 | } |
| 299 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 300 | void AmbiguousConversionSequence::construct() { |
| 301 | new (&conversions()) ConversionSet(); |
| 302 | } |
| 303 | |
| 304 | void AmbiguousConversionSequence::destruct() { |
| 305 | conversions().~ConversionSet(); |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 310 | FromTypePtr = O.FromTypePtr; |
| 311 | ToTypePtr = O.ToTypePtr; |
| 312 | new (&conversions()) ConversionSet(O.conversions()); |
| 313 | } |
| 314 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 315 | namespace { |
| 316 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 317 | // template parameter and template argument information. |
| 318 | struct DFIParamWithArguments { |
| 319 | TemplateParameter Param; |
| 320 | TemplateArgument FirstArg; |
| 321 | TemplateArgument SecondArg; |
| 322 | }; |
| 323 | } |
| 324 | |
| 325 | /// \brief Convert from Sema's representation of template deduction information |
| 326 | /// to the form used in overload-candidate information. |
| 327 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 328 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 329 | Sema::TemplateDeductionResult TDK, |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 330 | Sema::TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 331 | OverloadCandidate::DeductionFailureInfo Result; |
| 332 | Result.Result = static_cast<unsigned>(TDK); |
| 333 | Result.Data = 0; |
| 334 | switch (TDK) { |
| 335 | case Sema::TDK_Success: |
| 336 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 337 | case Sema::TDK_TooManyArguments: |
| 338 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 339 | break; |
| 340 | |
| 341 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 342 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 343 | Result.Data = Info.Param.getOpaqueValue(); |
| 344 | break; |
| 345 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 346 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 347 | case Sema::TDK_Underqualified: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 348 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 349 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 350 | Saved->Param = Info.Param; |
| 351 | Saved->FirstArg = Info.FirstArg; |
| 352 | Saved->SecondArg = Info.SecondArg; |
| 353 | Result.Data = Saved; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 358 | Result.Data = Info.take(); |
| 359 | break; |
| 360 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 361 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 362 | case Sema::TDK_FailedOverloadResolution: |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | return Result; |
| 367 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 369 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 370 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 371 | case Sema::TDK_Success: |
| 372 | case Sema::TDK_InstantiationDepth: |
| 373 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 374 | case Sema::TDK_TooManyArguments: |
| 375 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 376 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 377 | break; |
| 378 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 379 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 380 | case Sema::TDK_Underqualified: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 381 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 382 | Data = 0; |
| 383 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 384 | |
| 385 | case Sema::TDK_SubstitutionFailure: |
| 386 | // FIXME: Destroy the template arugment list? |
| 387 | Data = 0; |
| 388 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 390 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 391 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 392 | case Sema::TDK_FailedOverloadResolution: |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | TemplateParameter |
| 398 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 399 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 400 | case Sema::TDK_Success: |
| 401 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 402 | case Sema::TDK_TooManyArguments: |
| 403 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 404 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 405 | return TemplateParameter(); |
| 406 | |
| 407 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 408 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 409 | return TemplateParameter::getFromOpaqueValue(Data); |
| 410 | |
| 411 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 412 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 413 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
| 414 | |
| 415 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 416 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 417 | case Sema::TDK_FailedOverloadResolution: |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | return TemplateParameter(); |
| 422 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 423 | |
| 424 | TemplateArgumentList * |
| 425 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 426 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 427 | case Sema::TDK_Success: |
| 428 | case Sema::TDK_InstantiationDepth: |
| 429 | case Sema::TDK_TooManyArguments: |
| 430 | case Sema::TDK_TooFewArguments: |
| 431 | case Sema::TDK_Incomplete: |
| 432 | case Sema::TDK_InvalidExplicitArguments: |
| 433 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 434 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | |
| 437 | case Sema::TDK_SubstitutionFailure: |
| 438 | return static_cast<TemplateArgumentList*>(Data); |
| 439 | |
| 440 | // Unhandled |
| 441 | case Sema::TDK_NonDeducedMismatch: |
| 442 | case Sema::TDK_FailedOverloadResolution: |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 449 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 450 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 451 | case Sema::TDK_Success: |
| 452 | case Sema::TDK_InstantiationDepth: |
| 453 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 454 | case Sema::TDK_TooManyArguments: |
| 455 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 456 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 457 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 458 | return 0; |
| 459 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 460 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 461 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 462 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
| 463 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 464 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 465 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 466 | case Sema::TDK_FailedOverloadResolution: |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | const TemplateArgument * |
| 474 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 475 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 476 | case Sema::TDK_Success: |
| 477 | case Sema::TDK_InstantiationDepth: |
| 478 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 479 | case Sema::TDK_TooManyArguments: |
| 480 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 481 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 482 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 485 | case Sema::TDK_Inconsistent: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 486 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 487 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 488 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 489 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 490 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 491 | case Sema::TDK_FailedOverloadResolution: |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | void OverloadCandidateSet::clear() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 499 | inherited::clear(); |
| 500 | Functions.clear(); |
| 501 | } |
| 502 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 503 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 504 | // overload of the declarations in Old. This routine returns false if |
| 505 | // New and Old cannot be overloaded, e.g., if New has the same |
| 506 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 507 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 508 | // it does return false, MatchedDecl will point to the decl that New |
| 509 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 510 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 511 | // |
| 512 | // Example: Given the following input: |
| 513 | // |
| 514 | // void f(int, float); // #1 |
| 515 | // void f(int, int); // #2 |
| 516 | // int f(int, int); // #3 |
| 517 | // |
| 518 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 520 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 521 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 522 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 523 | // (since they have different signatures), so this routine returns |
| 524 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 525 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 526 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 527 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 528 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 529 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 530 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 531 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 532 | // |
| 533 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 534 | // into a class by a using declaration. The rules for whether to hide |
| 535 | // shadow declarations ignore some properties which otherwise figure |
| 536 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 537 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 538 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 539 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 540 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 541 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 542 | NamedDecl *OldD = *I; |
| 543 | |
| 544 | bool OldIsUsingDecl = false; |
| 545 | if (isa<UsingShadowDecl>(OldD)) { |
| 546 | OldIsUsingDecl = true; |
| 547 | |
| 548 | // We can always introduce two using declarations into the same |
| 549 | // context, even if they have identical signatures. |
| 550 | if (NewIsUsingDecl) continue; |
| 551 | |
| 552 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 553 | } |
| 554 | |
| 555 | // If either declaration was introduced by a using declaration, |
| 556 | // we'll need to use slightly different rules for matching. |
| 557 | // Essentially, these rules are the normal rules, except that |
| 558 | // function templates hide function templates with different |
| 559 | // return types or template parameter lists. |
| 560 | bool UseMemberUsingDeclRules = |
| 561 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 562 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 563 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 564 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 565 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 566 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 567 | continue; |
| 568 | } |
| 569 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 570 | Match = *I; |
| 571 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 572 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 573 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 574 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 575 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 576 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 577 | continue; |
| 578 | } |
| 579 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 580 | Match = *I; |
| 581 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 582 | } |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 583 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 584 | // We can overload with these, which can show up when doing |
| 585 | // redeclaration checks for UsingDecls. |
| 586 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 587 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 588 | // Optimistically assume that an unresolved using decl will |
| 589 | // overload; if it doesn't, we'll have to diagnose during |
| 590 | // template instantiation. |
| 591 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 592 | // (C++ 13p1): |
| 593 | // Only function declarations can be overloaded; object and type |
| 594 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 595 | Match = *I; |
| 596 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 597 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 599 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 600 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 601 | } |
| 602 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 603 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 604 | bool UseUsingDeclRules) { |
John McCall | 7b49202 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 605 | // If both of the functions are extern "C", then they are not |
| 606 | // overloads. |
| 607 | if (Old->isExternC() && New->isExternC()) |
| 608 | return false; |
| 609 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 610 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 611 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 612 | |
| 613 | // C++ [temp.fct]p2: |
| 614 | // A function template can be overloaded with other function templates |
| 615 | // and with normal (non-template) functions. |
| 616 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 617 | return true; |
| 618 | |
| 619 | // Is the function New an overload of the function Old? |
| 620 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 621 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 622 | |
| 623 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 624 | // determine whether they are overloads. If we find any mismatch |
| 625 | // in the signature, they are overloads. |
| 626 | |
| 627 | // If either of these functions is a K&R-style function (no |
| 628 | // prototype), then we consider them to have matching signatures. |
| 629 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 630 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 631 | return false; |
| 632 | |
| 633 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 634 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 635 | |
| 636 | // The signature of a function includes the types of its |
| 637 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 638 | // of the ellipsis; see C++ DR 357). |
| 639 | if (OldQType != NewQType && |
| 640 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 641 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 642 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 643 | return true; |
| 644 | |
| 645 | // C++ [temp.over.link]p4: |
| 646 | // The signature of a function template consists of its function |
| 647 | // signature, its return type and its template parameter list. The names |
| 648 | // of the template parameters are significant only for establishing the |
| 649 | // relationship between the template parameters and the rest of the |
| 650 | // signature. |
| 651 | // |
| 652 | // We check the return type and template parameter lists for function |
| 653 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 654 | // |
| 655 | // However, we don't consider either of these when deciding whether |
| 656 | // a member introduced by a shadow declaration is hidden. |
| 657 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 658 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 659 | OldTemplate->getTemplateParameters(), |
| 660 | false, TPL_TemplateMatch) || |
| 661 | OldType->getResultType() != NewType->getResultType())) |
| 662 | return true; |
| 663 | |
| 664 | // If the function is a class member, its signature includes the |
| 665 | // cv-qualifiers (if any) on the function itself. |
| 666 | // |
| 667 | // As part of this, also check whether one of the member functions |
| 668 | // is static, in which case they are not overloads (C++ |
| 669 | // 13.1p2). While not part of the definition of the signature, |
| 670 | // this check is important to determine whether these functions |
| 671 | // can be overloaded. |
| 672 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 673 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 674 | if (OldMethod && NewMethod && |
| 675 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 676 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 677 | return true; |
| 678 | |
| 679 | // The signatures match; this is not an overload. |
| 680 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 683 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 684 | /// from the given expression (Expr) to the given type (ToType). This |
| 685 | /// function returns an implicit conversion sequence that can be used |
| 686 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 687 | /// |
| 688 | /// void f(float f); |
| 689 | /// void g(int i) { f(i); } |
| 690 | /// |
| 691 | /// this routine would produce an implicit conversion sequence to |
| 692 | /// describe the initialization of f from i, which will be a standard |
| 693 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 694 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 695 | // |
| 696 | /// Note that this routine only determines how the conversion can be |
| 697 | /// performed; it does not actually perform the conversion. As such, |
| 698 | /// it will not produce any diagnostics if no conversion is available, |
| 699 | /// but will instead return an implicit conversion sequence of kind |
| 700 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 701 | /// |
| 702 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 703 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 704 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 705 | /// permitted. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 706 | static ImplicitConversionSequence |
| 707 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 708 | bool SuppressUserConversions, |
| 709 | bool AllowExplicit, |
| 710 | bool InOverloadResolution) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 711 | ImplicitConversionSequence ICS; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 712 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
| 713 | ICS.Standard)) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 714 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 715 | return ICS; |
| 716 | } |
| 717 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 718 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 719 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 720 | return ICS; |
| 721 | } |
| 722 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 723 | // C++ [over.ics.user]p4: |
| 724 | // A conversion of an expression of class type to the same class |
| 725 | // type is given Exact Match rank, and a conversion of an |
| 726 | // expression of class type to a base class of that type is |
| 727 | // given Conversion rank, in spite of the fact that a copy/move |
| 728 | // constructor (i.e., a user-defined conversion function) is |
| 729 | // called for those cases. |
| 730 | QualType FromType = From->getType(); |
| 731 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 732 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 733 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 734 | ICS.setStandard(); |
| 735 | ICS.Standard.setAsIdentityConversion(); |
| 736 | ICS.Standard.setFromType(FromType); |
| 737 | ICS.Standard.setAllToTypes(ToType); |
| 738 | |
| 739 | // We don't actually check at this point whether there is a valid |
| 740 | // copy/move constructor, since overloading just assumes that it |
| 741 | // exists. When we actually perform initialization, we'll find the |
| 742 | // appropriate constructor to copy the returned object, if needed. |
| 743 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 745 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 746 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 747 | ICS.Standard.Second = ICK_Derived_To_Base; |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 748 | |
| 749 | return ICS; |
| 750 | } |
| 751 | |
| 752 | if (SuppressUserConversions) { |
| 753 | // We're not in the case above, so there is no conversion that |
| 754 | // we can perform. |
| 755 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 756 | return ICS; |
| 757 | } |
| 758 | |
| 759 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 760 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 761 | OverloadingResult UserDefResult |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 762 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 763 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 764 | |
| 765 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 766 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 767 | // C++ [over.ics.user]p4: |
| 768 | // A conversion of an expression of class type to the same class |
| 769 | // type is given Exact Match rank, and a conversion of an |
| 770 | // expression of class type to a base class of that type is |
| 771 | // given Conversion rank, in spite of the fact that a copy |
| 772 | // constructor (i.e., a user-defined conversion function) is |
| 773 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 775 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | QualType FromCanon |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 777 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 778 | QualType ToCanon |
| 779 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 780 | if (Constructor->isCopyConstructor() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 781 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 782 | // Turn this into a "standard" conversion sequence, so that it |
| 783 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 784 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 785 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 786 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 787 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 788 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 789 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 790 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 791 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 792 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 793 | |
| 794 | // C++ [over.best.ics]p4: |
| 795 | // However, when considering the argument of a user-defined |
| 796 | // conversion function that is a candidate by 13.3.1.3 when |
| 797 | // invoked for the copying of the temporary in the second step |
| 798 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 799 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 800 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 801 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 802 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 803 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 804 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 805 | ICS.setAmbiguous(); |
| 806 | ICS.Ambiguous.setFromType(From->getType()); |
| 807 | ICS.Ambiguous.setToType(ToType); |
| 808 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 809 | Cand != Conversions.end(); ++Cand) |
| 810 | if (Cand->Viable) |
| 811 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 812 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 813 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 814 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 815 | |
| 816 | return ICS; |
| 817 | } |
| 818 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 819 | bool Sema::TryImplicitConversion(InitializationSequence &Sequence, |
| 820 | const InitializedEntity &Entity, |
| 821 | Expr *Initializer, |
| 822 | bool SuppressUserConversions, |
| 823 | bool AllowExplicitConversions, |
| 824 | bool InOverloadResolution) { |
| 825 | ImplicitConversionSequence ICS |
| 826 | = clang::TryImplicitConversion(*this, Initializer, Entity.getType(), |
| 827 | SuppressUserConversions, |
| 828 | AllowExplicitConversions, |
| 829 | InOverloadResolution); |
| 830 | if (ICS.isBad()) return true; |
| 831 | |
| 832 | // Perform the actual conversion. |
| 833 | Sequence.AddConversionSequenceStep(ICS, Entity.getType()); |
| 834 | return false; |
| 835 | } |
| 836 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 837 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 838 | /// expression From to the type ToType. Returns true if there was an |
| 839 | /// error, false otherwise. The expression From is replaced with the |
| 840 | /// converted expression. Flavor is the kind of conversion we're |
| 841 | /// performing, used in the error message. If @p AllowExplicit, |
| 842 | /// explicit user-defined conversions are permitted. |
| 843 | bool |
| 844 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 845 | AssignmentAction Action, bool AllowExplicit) { |
| 846 | ImplicitConversionSequence ICS; |
| 847 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 848 | } |
| 849 | |
| 850 | bool |
| 851 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 852 | AssignmentAction Action, bool AllowExplicit, |
| 853 | ImplicitConversionSequence& ICS) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 854 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 855 | /*SuppressUserConversions=*/false, |
| 856 | AllowExplicit, |
| 857 | /*InOverloadResolution=*/false); |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 858 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 859 | } |
| 860 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 861 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 862 | /// conversion that strips "noreturn" off the nested function type. |
| 863 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 864 | QualType ToType, QualType &ResultTy) { |
| 865 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 866 | return false; |
| 867 | |
| 868 | // Strip the noreturn off the type we're converting from; noreturn can |
| 869 | // safely be removed. |
| 870 | FromType = Context.getNoReturnType(FromType, false); |
| 871 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 872 | return false; |
| 873 | |
| 874 | ResultTy = FromType; |
| 875 | return true; |
| 876 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 877 | |
| 878 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 879 | /// vector conversion. |
| 880 | /// |
| 881 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 882 | /// conversion. |
| 883 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 884 | QualType ToType, ImplicitConversionKind &ICK) { |
| 885 | // We need at least one of these types to be a vector type to have a vector |
| 886 | // conversion. |
| 887 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 888 | return false; |
| 889 | |
| 890 | // Identical types require no conversions. |
| 891 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 892 | return false; |
| 893 | |
| 894 | // There are no conversions between extended vector types, only identity. |
| 895 | if (ToType->isExtVectorType()) { |
| 896 | // There are no conversions between extended vector types other than the |
| 897 | // identity conversion. |
| 898 | if (FromType->isExtVectorType()) |
| 899 | return false; |
| 900 | |
| 901 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 902 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 903 | ICK = ICK_Vector_Splat; |
| 904 | return true; |
| 905 | } |
| 906 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 907 | |
| 908 | // We can perform the conversion between vector types in the following cases: |
| 909 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 910 | // 2)lax vector conversions are permitted and the vector types are of the |
| 911 | // same size |
| 912 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 913 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | c45eb9c | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 914 | (Context.getLangOptions().LaxVectorConversions && |
| 915 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 916 | ICK = ICK_Vector_Conversion; |
| 917 | return true; |
| 918 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 919 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 921 | return false; |
| 922 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 924 | /// IsStandardConversion - Determines whether there is a standard |
| 925 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 926 | /// expression From to the type ToType. Standard conversion sequences |
| 927 | /// only consider non-class types; for conversions that involve class |
| 928 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 929 | /// contain the standard conversion sequence required to perform this |
| 930 | /// conversion and this routine will return true. Otherwise, this |
| 931 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 932 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 933 | bool InOverloadResolution, |
| 934 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 935 | QualType FromType = From->getType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 937 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 938 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 939 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 940 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 941 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 942 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 944 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 946 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 947 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 948 | return false; |
| 949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 953 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 954 | // array-to-pointer conversion, or function-to-pointer conversion |
| 955 | // (C++ 4p1). |
| 956 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 957 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 958 | DeclAccessPair AccessPair; |
| 959 | if (FunctionDecl *Fn |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 960 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 961 | AccessPair)) { |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 962 | // We were able to resolve the address of the overloaded function, |
| 963 | // so we can convert to the type of that function. |
| 964 | FromType = Fn->getType(); |
| 965 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 966 | if (!Method->isStatic()) { |
| 967 | Type *ClassType |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 968 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 969 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | // If the "from" expression takes the address of the overloaded |
| 974 | // function, update the type of the resulting expression accordingly. |
| 975 | if (FromType->getAs<FunctionType>()) |
| 976 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(From->IgnoreParens())) |
| 977 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 978 | FromType = S.Context.getPointerType(FromType); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 979 | |
| 980 | // Check that we've computed the proper type after overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 981 | assert(S.Context.hasSameType(FromType, |
| 982 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 983 | } else { |
| 984 | return false; |
| 985 | } |
| 986 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 988 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 989 | // converted to an rvalue. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 990 | Expr::isLvalueResult argIsLvalue = From->isLvalue(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 992 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 993 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 994 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 995 | |
| 996 | // If T is a non-class type, the type of the rvalue is the |
| 997 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 998 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 999 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1000 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1001 | } else if (FromType->isArrayType()) { |
| 1002 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1003 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1004 | |
| 1005 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1006 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1007 | // T" (C++ 4.2p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1008 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1009 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1010 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1011 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1012 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1013 | |
| 1014 | // For the purpose of ranking in overload resolution |
| 1015 | // (13.3.3.1.1), this conversion is considered an |
| 1016 | // array-to-pointer conversion followed by a qualification |
| 1017 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1018 | SCS.Second = ICK_Identity; |
| 1019 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1020 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1021 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1022 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1023 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 1024 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1025 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1026 | |
| 1027 | // An lvalue of function type T can be converted to an rvalue of |
| 1028 | // type "pointer to T." The result is a pointer to the |
| 1029 | // function. (C++ 4.3p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1030 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1031 | } else { |
| 1032 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1033 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1034 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1035 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1036 | |
| 1037 | // The second conversion can be an integral promotion, floating |
| 1038 | // point promotion, integral conversion, floating point conversion, |
| 1039 | // floating-integral conversion, pointer conversion, |
| 1040 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1041 | // For overloading in C, this can also be a "compatible-type" |
| 1042 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1043 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1044 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1045 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1046 | // The unqualified versions of the types are the same: there's no |
| 1047 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1048 | SCS.Second = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1049 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1051 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1052 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1053 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1054 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1055 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1056 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1057 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1058 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1059 | SCS.Second = ICK_Complex_Promotion; |
| 1060 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1061 | } else if (FromType->isIntegralOrEnumerationType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1062 | ToType->isIntegralType(S.Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1063 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1064 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1065 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1066 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 1067 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1068 | SCS.Second = ICK_Complex_Conversion; |
| 1069 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1070 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 1071 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 1072 | // Complex-real conversions (C99 6.3.1.7) |
| 1073 | SCS.Second = ICK_Complex_Real; |
| 1074 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1075 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1076 | // Floating point conversions (C++ 4.8). |
| 1077 | SCS.Second = ICK_Floating_Conversion; |
| 1078 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1079 | } else if ((FromType->isRealFloatingType() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1080 | ToType->isIntegralType(S.Context) && !ToType->isBooleanType()) || |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1081 | (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1082 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1083 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1084 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1085 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1086 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1087 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1088 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1089 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1090 | SCS.IncompatibleObjC = IncompatibleObjC; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1091 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
| 1092 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1093 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1094 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1095 | } else if (ToType->isBooleanType() && |
| 1096 | (FromType->isArithmeticType() || |
| 1097 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 1098 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1099 | FromType->isBlockPointerType() || |
| 1100 | FromType->isMemberPointerType() || |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1101 | FromType->isNullPtrType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1102 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1103 | SCS.Second = ICK_Boolean_Conversion; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1104 | FromType = S.Context.BoolTy; |
| 1105 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1106 | SCS.Second = SecondICK; |
| 1107 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1108 | } else if (!S.getLangOptions().CPlusPlus && |
| 1109 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1110 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1111 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1112 | FromType = ToType.getUnqualifiedType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1113 | } else if (IsNoReturnConversion(S.Context, FromType, ToType, FromType)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1114 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1115 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1116 | } else { |
| 1117 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1118 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1119 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1120 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1122 | QualType CanonFrom; |
| 1123 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1124 | // The third conversion can be a qualification conversion (C++ 4p1). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1125 | if (S.IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1126 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1127 | FromType = ToType; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1128 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1129 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1130 | } else { |
| 1131 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1132 | SCS.Third = ICK_Identity; |
| 1133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1135 | // [...] Any difference in top-level cv-qualification is |
| 1136 | // subsumed by the initialization itself and does not constitute |
| 1137 | // a conversion. [...] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1138 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1139 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1140 | if (CanonFrom.getLocalUnqualifiedType() |
| 1141 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1142 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1143 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1144 | FromType = ToType; |
| 1145 | CanonFrom = CanonTo; |
| 1146 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1147 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1148 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1149 | |
| 1150 | // If we have not converted the argument type to the parameter type, |
| 1151 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1152 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1153 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1154 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1155 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1159 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1160 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1161 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1163 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1164 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1165 | if (!To) { |
| 1166 | return false; |
| 1167 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1168 | |
| 1169 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1170 | // unsigned short int can be converted to an rvalue of type int if |
| 1171 | // int can represent all the values of the source type; otherwise, |
| 1172 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1173 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1174 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1175 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1176 | if (// We can promote any signed, promotable integer type to an int |
| 1177 | (FromType->isSignedIntegerType() || |
| 1178 | // We can promote any unsigned integer type whose size is |
| 1179 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1181 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1182 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1185 | return To->getKind() == BuiltinType::UInt; |
| 1186 | } |
| 1187 | |
| 1188 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 1189 | // can be converted to an rvalue of the first of the following types |
| 1190 | // that can represent all the values of its underlying type: int, |
| 1191 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1192 | |
| 1193 | // We pre-calculate the promotion type for enum types. |
| 1194 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 1195 | if (ToType->isIntegerType()) |
| 1196 | return Context.hasSameUnqualifiedType(ToType, |
| 1197 | FromEnumType->getDecl()->getPromotionType()); |
| 1198 | |
| 1199 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1200 | // Determine whether the type we're converting from is signed or |
| 1201 | // unsigned. |
| 1202 | bool FromIsSigned; |
| 1203 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1204 | |
| 1205 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1206 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1207 | |
| 1208 | // The types we'll try to promote to, in the appropriate |
| 1209 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | QualType PromoteTypes[6] = { |
| 1211 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1212 | Context.LongTy, Context.UnsignedLongTy , |
| 1213 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1214 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1215 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1216 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1217 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1219 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1220 | // We found the type that we can promote to. If this is the |
| 1221 | // type we wanted, we have a promotion. Otherwise, no |
| 1222 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1223 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1229 | // rvalue of type int if int can represent all the values of the |
| 1230 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1231 | // unsigned int can represent all the values of the bit-field. If |
| 1232 | // the bit-field is larger yet, no integral promotion applies to |
| 1233 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1234 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1235 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1236 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1237 | using llvm::APSInt; |
| 1238 | if (From) |
| 1239 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1240 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1241 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1242 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1243 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1244 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1246 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1247 | if (BitWidth < ToSize || |
| 1248 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1249 | return To->getKind() == BuiltinType::Int; |
| 1250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1252 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1253 | // that fits into an unsigned int? |
| 1254 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1255 | return To->getKind() == BuiltinType::UInt; |
| 1256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1258 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1259 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1262 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1263 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1264 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1265 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1266 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1267 | |
| 1268 | return false; |
| 1269 | } |
| 1270 | |
| 1271 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1272 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1273 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1275 | /// An rvalue of type float can be converted to an rvalue of type |
| 1276 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1277 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1278 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1279 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1280 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1281 | return true; |
| 1282 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1283 | // C99 6.3.1.5p1: |
| 1284 | // When a float is promoted to double or long double, or a |
| 1285 | // double is promoted to long double [...]. |
| 1286 | if (!getLangOptions().CPlusPlus && |
| 1287 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1288 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1289 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1293 | return false; |
| 1294 | } |
| 1295 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1296 | /// \brief Determine if a conversion is a complex promotion. |
| 1297 | /// |
| 1298 | /// A complex promotion is defined as a complex -> complex conversion |
| 1299 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1300 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1301 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1302 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1303 | if (!FromComplex) |
| 1304 | return false; |
| 1305 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1306 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1307 | if (!ToComplex) |
| 1308 | return false; |
| 1309 | |
| 1310 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1311 | ToComplex->getElementType()) || |
| 1312 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1313 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1316 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1317 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1318 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1319 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1320 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | static QualType |
| 1322 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1323 | QualType ToPointee, QualType ToType, |
| 1324 | ASTContext &Context) { |
| 1325 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 1326 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1327 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
| 1329 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1330 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1331 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1332 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1333 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1334 | |
| 1335 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1336 | // already. |
| 1337 | return Context.getPointerType(ToPointee); |
| 1338 | } |
| 1339 | |
| 1340 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1341 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1342 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1343 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1346 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1347 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1348 | /// not have the right set of qualifiers. |
| 1349 | static QualType |
| 1350 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1351 | QualType ToType, |
| 1352 | ASTContext &Context) { |
| 1353 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1354 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1355 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1356 | |
| 1357 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1358 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1359 | return ToType; |
| 1360 | |
| 1361 | // Just build a canonical type that has the right qualifiers. |
| 1362 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1363 | } |
| 1364 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1366 | bool InOverloadResolution, |
| 1367 | ASTContext &Context) { |
| 1368 | // Handle value-dependent integral null pointer constants correctly. |
| 1369 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1370 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1371 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1372 | return !InOverloadResolution; |
| 1373 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1374 | return Expr->isNullPointerConstant(Context, |
| 1375 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1376 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1377 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1379 | /// IsPointerConversion - Determines whether the conversion of the |
| 1380 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1381 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1382 | /// 4.10). If so, returns true and places the converted type (that |
| 1383 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1384 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1385 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1386 | /// This routine also supports conversions to and from block pointers |
| 1387 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1388 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1389 | /// appropriate overloading rules for Objective-C, we may want to |
| 1390 | /// split the Objective-C checks into a different routine; however, |
| 1391 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1392 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1393 | /// set if the conversion is an allowed Objective-C conversion that |
| 1394 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1395 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1396 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1397 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1399 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1400 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1401 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1402 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1404 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1405 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1406 | ConvertedType = ToType; |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1410 | // Blocks: Block pointers can be converted to void*. |
| 1411 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1412 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1413 | ConvertedType = ToType; |
| 1414 | return true; |
| 1415 | } |
| 1416 | // Blocks: A null pointer constant can be converted to a block |
| 1417 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1419 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1420 | ConvertedType = ToType; |
| 1421 | return true; |
| 1422 | } |
| 1423 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1424 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1425 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1427 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1428 | ConvertedType = ToType; |
| 1429 | return true; |
| 1430 | } |
| 1431 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1432 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1433 | if (!ToTypePtr) |
| 1434 | return false; |
| 1435 | |
| 1436 | // 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] | 1437 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1438 | ConvertedType = ToType; |
| 1439 | return true; |
| 1440 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1441 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1442 | // Beyond this point, both types need to be pointers |
| 1443 | // , including objective-c pointers. |
| 1444 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1445 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1446 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1447 | ToType, Context); |
| 1448 | return true; |
| 1449 | |
| 1450 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1451 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1452 | if (!FromTypePtr) |
| 1453 | return false; |
| 1454 | |
| 1455 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1456 | |
Douglas Gregor | 4e938f57b | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 1457 | // If the unqualified pointee types are the same, this can't be a |
| 1458 | // pointer conversion, so don't do all of the work below. |
| 1459 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1460 | return false; |
| 1461 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1462 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1463 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1464 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1465 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1466 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1468 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1469 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
| 1472 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1473 | // When we're overloading in C, we allow a special kind of pointer |
| 1474 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1476 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1478 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1480 | return true; |
| 1481 | } |
| 1482 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1483 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1485 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1486 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1487 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1488 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1489 | // necessitates this conversion is ill-formed. The result of the |
| 1490 | // conversion is a pointer to the base class sub-object of the |
| 1491 | // derived class object. The null pointer value is converted to |
| 1492 | // the null pointer value of the destination type. |
| 1493 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1494 | // Note that we do not check for ambiguity or inaccessibility |
| 1495 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1496 | if (getLangOptions().CPlusPlus && |
| 1497 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1498 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1499 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1500 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1502 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1503 | ToType, Context); |
| 1504 | return true; |
| 1505 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1507 | return false; |
| 1508 | } |
| 1509 | |
| 1510 | /// isObjCPointerConversion - Determines whether this is an |
| 1511 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1512 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1514 | QualType& ConvertedType, |
| 1515 | bool &IncompatibleObjC) { |
| 1516 | if (!getLangOptions().ObjC1) |
| 1517 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1518 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1519 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1520 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1522 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1523 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1524 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1525 | // 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] | 1526 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1527 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1528 | ConvertedType = ToType; |
| 1529 | return true; |
| 1530 | } |
| 1531 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1533 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1535 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1536 | ConvertedType = ToType; |
| 1537 | return true; |
| 1538 | } |
| 1539 | // Objective C++: We're able to convert from a pointer to an |
| 1540 | // interface to a pointer to a different interface. |
| 1541 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1542 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1543 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1544 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1545 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1546 | FromObjCPtr->getPointeeType())) |
| 1547 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1548 | ConvertedType = ToType; |
| 1549 | return true; |
| 1550 | } |
| 1551 | |
| 1552 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1553 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1554 | // interfaces, which is permitted. However, we're going to |
| 1555 | // complain about it. |
| 1556 | IncompatibleObjC = true; |
| 1557 | ConvertedType = FromType; |
| 1558 | return true; |
| 1559 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1561 | // 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] | 1562 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1563 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1564 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1565 | else if (const BlockPointerType *ToBlockPtr = |
| 1566 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1567 | // 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] | 1568 | // to a block pointer type. |
| 1569 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1570 | ConvertedType = ToType; |
| 1571 | return true; |
| 1572 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1573 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1574 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1575 | else if (FromType->getAs<BlockPointerType>() && |
| 1576 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1577 | // 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] | 1578 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1579 | ConvertedType = ToType; |
| 1580 | return true; |
| 1581 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1582 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1583 | return false; |
| 1584 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1585 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1586 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1587 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1588 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1589 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1590 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1591 | return false; |
| 1592 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1593 | // If we have pointers to pointers, recursively check whether this |
| 1594 | // is an Objective-C conversion. |
| 1595 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1596 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1597 | IncompatibleObjC)) { |
| 1598 | // We always complain about this conversion. |
| 1599 | IncompatibleObjC = true; |
| 1600 | ConvertedType = ToType; |
| 1601 | return true; |
| 1602 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1603 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1604 | // as in I* to id. |
| 1605 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1606 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1607 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1608 | IncompatibleObjC)) { |
| 1609 | ConvertedType = ToType; |
| 1610 | return true; |
| 1611 | } |
| 1612 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1613 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1614 | // differences in the argument and result types are in Objective-C |
| 1615 | // pointer conversions. If so, we permit the conversion (but |
| 1616 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1618 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1619 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1620 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1621 | if (FromFunctionType && ToFunctionType) { |
| 1622 | // If the function types are exactly the same, this isn't an |
| 1623 | // Objective-C pointer conversion. |
| 1624 | if (Context.getCanonicalType(FromPointeeType) |
| 1625 | == Context.getCanonicalType(ToPointeeType)) |
| 1626 | return false; |
| 1627 | |
| 1628 | // Perform the quick checks that will tell us whether these |
| 1629 | // function types are obviously different. |
| 1630 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1631 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1632 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1633 | return false; |
| 1634 | |
| 1635 | bool HasObjCConversion = false; |
| 1636 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1637 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1638 | // Okay, the types match exactly. Nothing to do. |
| 1639 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1640 | ToFunctionType->getResultType(), |
| 1641 | ConvertedType, IncompatibleObjC)) { |
| 1642 | // Okay, we have an Objective-C pointer conversion. |
| 1643 | HasObjCConversion = true; |
| 1644 | } else { |
| 1645 | // Function types are too different. Abort. |
| 1646 | return false; |
| 1647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1649 | // Check argument types. |
| 1650 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1651 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1652 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1653 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1654 | if (Context.getCanonicalType(FromArgType) |
| 1655 | == Context.getCanonicalType(ToArgType)) { |
| 1656 | // Okay, the types match exactly. Nothing to do. |
| 1657 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1658 | ConvertedType, IncompatibleObjC)) { |
| 1659 | // Okay, we have an Objective-C pointer conversion. |
| 1660 | HasObjCConversion = true; |
| 1661 | } else { |
| 1662 | // Argument types are too different. Abort. |
| 1663 | return false; |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | if (HasObjCConversion) { |
| 1668 | // We had an Objective-C conversion. Allow this pointer |
| 1669 | // conversion, but complain about it. |
| 1670 | ConvertedType = ToType; |
| 1671 | IncompatibleObjC = true; |
| 1672 | return true; |
| 1673 | } |
| 1674 | } |
| 1675 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1676 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1677 | } |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1678 | |
| 1679 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1680 | /// for equlity of their argument types. Caller has already checked that |
| 1681 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1682 | /// pointer types which only differ in their protocol qualifiers are equal. |
| 1683 | bool Sema::FunctionArgTypesAreEqual(FunctionProtoType* OldType, |
| 1684 | FunctionProtoType* NewType){ |
| 1685 | if (!getLangOptions().ObjC1) |
| 1686 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1687 | NewType->arg_type_begin()); |
| 1688 | |
| 1689 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1690 | N = NewType->arg_type_begin(), |
| 1691 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1692 | QualType ToType = (*O); |
| 1693 | QualType FromType = (*N); |
| 1694 | if (ToType != FromType) { |
| 1695 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1696 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1697 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1698 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1699 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1700 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1701 | continue; |
| 1702 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1703 | else if (const ObjCObjectPointerType *PTTo = |
| 1704 | ToType->getAs<ObjCObjectPointerType>()) { |
| 1705 | if (const ObjCObjectPointerType *PTFr = |
| 1706 | FromType->getAs<ObjCObjectPointerType>()) |
| 1707 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1708 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1709 | } |
| 1710 | return false; |
| 1711 | } |
| 1712 | } |
| 1713 | return true; |
| 1714 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1715 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1716 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1717 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1718 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1719 | /// conversions for which IsPointerConversion has already returned |
| 1720 | /// true. It returns true and produces a diagnostic if there was an |
| 1721 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1722 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1723 | CastExpr::CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1724 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1725 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1726 | QualType FromType = From->getType(); |
| 1727 | |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1728 | if (CXXBoolLiteralExpr* LitBool |
| 1729 | = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens())) |
| 1730 | if (LitBool->getValue() == false) |
| 1731 | Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false) |
| 1732 | << ToType; |
| 1733 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1734 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1735 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1736 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1737 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1738 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1739 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1740 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1741 | // We must have a derived-to-base conversion. Check an |
| 1742 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1743 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1744 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1745 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1746 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1747 | return true; |
| 1748 | |
| 1749 | // The conversion was successful. |
| 1750 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1754 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1756 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1757 | // Objective-C++ conversions are always okay. |
| 1758 | // FIXME: We should have a different class of conversions for the |
| 1759 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1760 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1761 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1762 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1763 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1764 | return false; |
| 1765 | } |
| 1766 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1767 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1768 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1769 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1770 | /// If so, returns true and places the converted type (that might differ from |
| 1771 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1772 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1773 | QualType ToType, |
| 1774 | bool InOverloadResolution, |
| 1775 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1776 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1777 | if (!ToTypePtr) |
| 1778 | return false; |
| 1779 | |
| 1780 | // 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] | 1781 | if (From->isNullPointerConstant(Context, |
| 1782 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1783 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1784 | ConvertedType = ToType; |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1789 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1790 | if (!FromTypePtr) |
| 1791 | return false; |
| 1792 | |
| 1793 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1794 | // where D is derived from B (C++ 4.11p2). |
| 1795 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1796 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1797 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1798 | |
| 1799 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1800 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1801 | ToClass.getTypePtr()); |
| 1802 | return true; |
| 1803 | } |
| 1804 | |
| 1805 | return false; |
| 1806 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1807 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1808 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1809 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1810 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1811 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1812 | /// true and produces a diagnostic if there was an error, or returns false |
| 1813 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1815 | CastExpr::CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1816 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1817 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1818 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1819 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1820 | if (!FromPtrType) { |
| 1821 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1822 | assert(From->isNullPointerConstant(Context, |
| 1823 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1824 | "Expr must be null pointer constant!"); |
| 1825 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1826 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1827 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1828 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1829 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1830 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1831 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1832 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1833 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1834 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1835 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1836 | // FIXME: What about dependent types? |
| 1837 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1838 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1839 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1840 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1841 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1842 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1843 | assert(DerivationOkay && |
| 1844 | "Should not have been called if derivation isn't OK."); |
| 1845 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1846 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1847 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1848 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1849 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1850 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1851 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1852 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1853 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1855 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1856 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1857 | << FromClass << ToClass << QualType(VBase, 0) |
| 1858 | << From->getSourceRange(); |
| 1859 | return true; |
| 1860 | } |
| 1861 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1862 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1863 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1864 | Paths.front(), |
| 1865 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1866 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1867 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1868 | BuildBasePathArray(Paths, BasePath); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1869 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1870 | return false; |
| 1871 | } |
| 1872 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1873 | /// IsQualificationConversion - Determines whether the conversion from |
| 1874 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1875 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | bool |
| 1877 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1878 | FromType = Context.getCanonicalType(FromType); |
| 1879 | ToType = Context.getCanonicalType(ToType); |
| 1880 | |
| 1881 | // If FromType and ToType are the same type, this is not a |
| 1882 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1883 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1884 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1885 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1886 | // (C++ 4.4p4): |
| 1887 | // A conversion can add cv-qualifiers at levels other than the first |
| 1888 | // in multi-level pointers, subject to the following rules: [...] |
| 1889 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1890 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1891 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1892 | // Within each iteration of the loop, we check the qualifiers to |
| 1893 | // determine if this still looks like a qualification |
| 1894 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1895 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1896 | // until there are no more pointers or pointers-to-members left to |
| 1897 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1898 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1899 | |
| 1900 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1901 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1902 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1903 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1905 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1906 | // every cv for 0 < k < j. |
| 1907 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1908 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1909 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1911 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1912 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1914 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1915 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1916 | |
| 1917 | // We are left with FromType and ToType being the pointee types |
| 1918 | // after unwrapping the original FromType and ToType the same number |
| 1919 | // of types. If we unwrapped any pointers, and if FromType and |
| 1920 | // ToType have the same unqualified type (since we checked |
| 1921 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1922 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1925 | /// Determines whether there is a user-defined conversion sequence |
| 1926 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1927 | /// ToType. If such a conversion exists, User will contain the |
| 1928 | /// user-defined conversion sequence that performs such a conversion |
| 1929 | /// and this routine will return true. Otherwise, this routine returns |
| 1930 | /// false and User is unspecified. |
| 1931 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1932 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1933 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1934 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1935 | static OverloadingResult |
| 1936 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 1937 | UserDefinedConversionSequence& User, |
| 1938 | OverloadCandidateSet& CandidateSet, |
| 1939 | bool AllowExplicit) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1940 | // Whether we will only visit constructors. |
| 1941 | bool ConstructorsOnly = false; |
| 1942 | |
| 1943 | // If the type we are conversion to is a class type, enumerate its |
| 1944 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1945 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1946 | // C++ [over.match.ctor]p1: |
| 1947 | // When objects of class type are direct-initialized (8.5), or |
| 1948 | // copy-initialized from an expression of the same or a |
| 1949 | // derived class type (8.5), overload resolution selects the |
| 1950 | // constructor. [...] For copy-initialization, the candidate |
| 1951 | // functions are all the converting constructors (12.3.1) of |
| 1952 | // that class. The argument list is the expression-list within |
| 1953 | // the parentheses of the initializer. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1954 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1955 | (From->getType()->getAs<RecordType>() && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1956 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1957 | ConstructorsOnly = true; |
| 1958 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1959 | if (S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag())) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1960 | // We're not going to find any constructors. |
| 1961 | } else if (CXXRecordDecl *ToRecordDecl |
| 1962 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1963 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1964 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1965 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1966 | NamedDecl *D = *Con; |
| 1967 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1968 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1969 | // Find the constructor (which may be a template). |
| 1970 | CXXConstructorDecl *Constructor = 0; |
| 1971 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1972 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1973 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1975 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1976 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1977 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1978 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1979 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1980 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1981 | if (ConstructorTmpl) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1982 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 1983 | /*ExplicitArgs*/ 0, |
| 1984 | &From, 1, CandidateSet, |
| 1985 | /*SuppressUserConversions=*/ |
| 1986 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1987 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1988 | // Allow one user-defined conversion when user specifies a |
| 1989 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1990 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 1991 | &From, 1, CandidateSet, |
| 1992 | /*SuppressUserConversions=*/ |
| 1993 | !ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1994 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1995 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1999 | // Enumerate conversion functions, if we're allowed to. |
| 2000 | if (ConstructorsOnly) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2001 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2002 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2003 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2005 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2007 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2008 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2009 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2010 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2011 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2012 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2013 | DeclAccessPair FoundDecl = I.getPair(); |
| 2014 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2015 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2016 | if (isa<UsingShadowDecl>(D)) |
| 2017 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2018 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2019 | CXXConversionDecl *Conv; |
| 2020 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2021 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2022 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2023 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2024 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2025 | |
| 2026 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2027 | if (ConvTemplate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2028 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2029 | ActingContext, From, ToType, |
| 2030 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2031 | else |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2032 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2033 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2037 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2038 | |
| 2039 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2040 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best)) { |
| 2041 | case OR_Success: |
| 2042 | // Record the standard conversion we used and the conversion function. |
| 2043 | if (CXXConstructorDecl *Constructor |
| 2044 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 2045 | // C++ [over.ics.user]p1: |
| 2046 | // If the user-defined conversion is specified by a |
| 2047 | // constructor (12.3.1), the initial standard conversion |
| 2048 | // sequence converts the source type to the type required by |
| 2049 | // the argument of the constructor. |
| 2050 | // |
| 2051 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2052 | if (Best->Conversions[0].isEllipsis()) |
| 2053 | User.EllipsisConversion = true; |
| 2054 | else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2055 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2056 | User.EllipsisConversion = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2057 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2058 | User.ConversionFunction = Constructor; |
| 2059 | User.After.setAsIdentityConversion(); |
| 2060 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2061 | User.After.setAllToTypes(ToType); |
| 2062 | return OR_Success; |
| 2063 | } else if (CXXConversionDecl *Conversion |
| 2064 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 2065 | // C++ [over.ics.user]p1: |
| 2066 | // |
| 2067 | // [...] If the user-defined conversion is specified by a |
| 2068 | // conversion function (12.3.2), the initial standard |
| 2069 | // conversion sequence converts the source type to the |
| 2070 | // implicit object parameter of the conversion function. |
| 2071 | User.Before = Best->Conversions[0].Standard; |
| 2072 | User.ConversionFunction = Conversion; |
| 2073 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2075 | // C++ [over.ics.user]p2: |
| 2076 | // The second standard conversion sequence converts the |
| 2077 | // result of the user-defined conversion to the target type |
| 2078 | // for the sequence. Since an implicit conversion sequence |
| 2079 | // is an initialization, the special rules for |
| 2080 | // initialization by user-defined conversion apply when |
| 2081 | // selecting the best user-defined conversion for a |
| 2082 | // user-defined conversion sequence (see 13.3.3 and |
| 2083 | // 13.3.3.1). |
| 2084 | User.After = Best->FinalConversion; |
| 2085 | return OR_Success; |
| 2086 | } else { |
| 2087 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2088 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2091 | case OR_No_Viable_Function: |
| 2092 | return OR_No_Viable_Function; |
| 2093 | case OR_Deleted: |
| 2094 | // No conversion here! We're done. |
| 2095 | return OR_Deleted; |
| 2096 | |
| 2097 | case OR_Ambiguous: |
| 2098 | return OR_Ambiguous; |
| 2099 | } |
| 2100 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2101 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2102 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2103 | |
| 2104 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2105 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2106 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2107 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2108 | OverloadingResult OvResult = |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2109 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2110 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2111 | if (OvResult == OR_Ambiguous) |
| 2112 | Diag(From->getSourceRange().getBegin(), |
| 2113 | diag::err_typecheck_ambiguous_condition) |
| 2114 | << From->getType() << ToType << From->getSourceRange(); |
| 2115 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2116 | Diag(From->getSourceRange().getBegin(), |
| 2117 | diag::err_typecheck_nonviable_condition) |
| 2118 | << From->getType() << ToType << From->getSourceRange(); |
| 2119 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2120 | return false; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2121 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2122 | return true; |
| 2123 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2124 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2125 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2126 | /// conversion sequences to determine whether one is better than the |
| 2127 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2128 | static ImplicitConversionSequence::CompareKind |
| 2129 | CompareImplicitConversionSequences(Sema &S, |
| 2130 | const ImplicitConversionSequence& ICS1, |
| 2131 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2132 | { |
| 2133 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2134 | // conversion sequences (as defined in 13.3.3.1) |
| 2135 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2136 | // conversion sequence than a user-defined conversion sequence or |
| 2137 | // an ellipsis conversion sequence, and |
| 2138 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2139 | // conversion sequence than an ellipsis conversion sequence |
| 2140 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2142 | // C++0x [over.best.ics]p10: |
| 2143 | // For the purpose of ranking implicit conversion sequences as |
| 2144 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2145 | // treated as a user-defined sequence that is indistinguishable |
| 2146 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2147 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2148 | return ImplicitConversionSequence::Better; |
| 2149 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2150 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2151 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2152 | // The following checks require both conversion sequences to be of |
| 2153 | // the same kind. |
| 2154 | if (ICS1.getKind() != ICS2.getKind()) |
| 2155 | return ImplicitConversionSequence::Indistinguishable; |
| 2156 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2157 | // Two implicit conversion sequences of the same form are |
| 2158 | // indistinguishable conversion sequences unless one of the |
| 2159 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2160 | if (ICS1.isStandard()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2161 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2162 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2163 | // User-defined conversion sequence U1 is a better conversion |
| 2164 | // sequence than another user-defined conversion sequence U2 if |
| 2165 | // they contain the same user-defined conversion function or |
| 2166 | // constructor and if the second standard conversion sequence of |
| 2167 | // U1 is better than the second standard conversion sequence of |
| 2168 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2170 | ICS2.UserDefined.ConversionFunction) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2171 | return CompareStandardConversionSequences(S, |
| 2172 | ICS1.UserDefined.After, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2173 | ICS2.UserDefined.After); |
| 2174 | } |
| 2175 | |
| 2176 | return ImplicitConversionSequence::Indistinguishable; |
| 2177 | } |
| 2178 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2179 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2180 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2181 | Qualifiers Quals; |
| 2182 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
| 2183 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
| 2184 | } |
| 2185 | |
| 2186 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2187 | } |
| 2188 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2189 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2190 | // determine if one is a proper subset of the other. |
| 2191 | static ImplicitConversionSequence::CompareKind |
| 2192 | compareStandardConversionSubsets(ASTContext &Context, |
| 2193 | const StandardConversionSequence& SCS1, |
| 2194 | const StandardConversionSequence& SCS2) { |
| 2195 | ImplicitConversionSequence::CompareKind Result |
| 2196 | = ImplicitConversionSequence::Indistinguishable; |
| 2197 | |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2198 | // the identity conversion sequence is considered to be a subsequence of |
| 2199 | // any non-identity conversion sequence |
| 2200 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2201 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2202 | return ImplicitConversionSequence::Better; |
| 2203 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2204 | return ImplicitConversionSequence::Worse; |
| 2205 | } |
| 2206 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2207 | if (SCS1.Second != SCS2.Second) { |
| 2208 | if (SCS1.Second == ICK_Identity) |
| 2209 | Result = ImplicitConversionSequence::Better; |
| 2210 | else if (SCS2.Second == ICK_Identity) |
| 2211 | Result = ImplicitConversionSequence::Worse; |
| 2212 | else |
| 2213 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2214 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2215 | return ImplicitConversionSequence::Indistinguishable; |
| 2216 | |
| 2217 | if (SCS1.Third == SCS2.Third) { |
| 2218 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2219 | : ImplicitConversionSequence::Indistinguishable; |
| 2220 | } |
| 2221 | |
| 2222 | if (SCS1.Third == ICK_Identity) |
| 2223 | return Result == ImplicitConversionSequence::Worse |
| 2224 | ? ImplicitConversionSequence::Indistinguishable |
| 2225 | : ImplicitConversionSequence::Better; |
| 2226 | |
| 2227 | if (SCS2.Third == ICK_Identity) |
| 2228 | return Result == ImplicitConversionSequence::Better |
| 2229 | ? ImplicitConversionSequence::Indistinguishable |
| 2230 | : ImplicitConversionSequence::Worse; |
| 2231 | |
| 2232 | return ImplicitConversionSequence::Indistinguishable; |
| 2233 | } |
| 2234 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2235 | /// CompareStandardConversionSequences - Compare two standard |
| 2236 | /// conversion sequences to determine whether one is better than the |
| 2237 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2238 | static ImplicitConversionSequence::CompareKind |
| 2239 | CompareStandardConversionSequences(Sema &S, |
| 2240 | const StandardConversionSequence& SCS1, |
| 2241 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2242 | { |
| 2243 | // Standard conversion sequence S1 is a better conversion sequence |
| 2244 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2245 | |
| 2246 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2247 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2248 | // excluding any Lvalue Transformation; the identity conversion |
| 2249 | // sequence is considered to be a subsequence of any |
| 2250 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2251 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2252 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2253 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2254 | |
| 2255 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2256 | // defined below), or, if not that, |
| 2257 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2258 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2259 | if (Rank1 < Rank2) |
| 2260 | return ImplicitConversionSequence::Better; |
| 2261 | else if (Rank2 < Rank1) |
| 2262 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2263 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2264 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2265 | // are indistinguishable unless one of the following rules |
| 2266 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2268 | // A conversion that is not a conversion of a pointer, or |
| 2269 | // pointer to member, to bool is better than another conversion |
| 2270 | // that is such a conversion. |
| 2271 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2272 | return SCS2.isPointerConversionToBool() |
| 2273 | ? ImplicitConversionSequence::Better |
| 2274 | : ImplicitConversionSequence::Worse; |
| 2275 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2276 | // C++ [over.ics.rank]p4b2: |
| 2277 | // |
| 2278 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2279 | // conversion of B* to A* is better than conversion of B* to |
| 2280 | // void*, and conversion of A* to void* is better than conversion |
| 2281 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | bool SCS1ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2283 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | bool SCS2ConvertsToVoid |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2285 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2286 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2287 | // Exactly one of the conversion sequences is a conversion to |
| 2288 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2289 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2290 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2291 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2292 | // Neither conversion sequence converts to a void pointer; compare |
| 2293 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2294 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2295 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2296 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2297 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2298 | // Both conversion sequences are conversions to void |
| 2299 | // pointers. Compare the source types to determine if there's an |
| 2300 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2301 | QualType FromType1 = SCS1.getFromType(); |
| 2302 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2303 | |
| 2304 | // Adjust the types we're converting from via the array-to-pointer |
| 2305 | // conversion, if we need to. |
| 2306 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2307 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2308 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2309 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2310 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2311 | QualType FromPointee1 |
| 2312 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2313 | QualType FromPointee2 |
| 2314 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2315 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2316 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2317 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2318 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2319 | return ImplicitConversionSequence::Worse; |
| 2320 | |
| 2321 | // Objective-C++: If one interface is more specific than the |
| 2322 | // other, it is the better one. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2323 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2324 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2325 | if (FromIface1 && FromIface1) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2326 | if (S.Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2327 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2328 | else if (S.Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2329 | return ImplicitConversionSequence::Worse; |
| 2330 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2331 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2332 | |
| 2333 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2334 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2336 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2337 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2338 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2339 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2340 | // C++0x [over.ics.rank]p3b4: |
| 2341 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2342 | // implicit object parameter of a non-static member function declared |
| 2343 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 2344 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2345 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 2346 | // or if the member function in this case has a ref qualifier. |
| 2347 | // (Of course, we don't have ref qualifiers yet.) |
| 2348 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 2349 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 2350 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2351 | |
| 2352 | // C++ [over.ics.rank]p3b4: |
| 2353 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2354 | // which the references refer are the same type except for |
| 2355 | // top-level cv-qualifiers, and the type to which the reference |
| 2356 | // initialized by S2 refers is more cv-qualified than the type |
| 2357 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2358 | QualType T1 = SCS1.getToType(2); |
| 2359 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2360 | T1 = S.Context.getCanonicalType(T1); |
| 2361 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2362 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2363 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2364 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2365 | if (UnqualT1 == UnqualT2) { |
| 2366 | // If the type is an array type, promote the element qualifiers to the type |
| 2367 | // for comparison. |
| 2368 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2369 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2370 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2371 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2372 | if (T2.isMoreQualifiedThan(T1)) |
| 2373 | return ImplicitConversionSequence::Better; |
| 2374 | else if (T1.isMoreQualifiedThan(T2)) |
| 2375 | return ImplicitConversionSequence::Worse; |
| 2376 | } |
| 2377 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2378 | |
| 2379 | return ImplicitConversionSequence::Indistinguishable; |
| 2380 | } |
| 2381 | |
| 2382 | /// CompareQualificationConversions - Compares two standard conversion |
| 2383 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2385 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2386 | CompareQualificationConversions(Sema &S, |
| 2387 | const StandardConversionSequence& SCS1, |
| 2388 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2389 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2390 | // -- S1 and S2 differ only in their qualification conversion and |
| 2391 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2392 | // cv-qualification signature of type T1 is a proper subset of |
| 2393 | // the cv-qualification signature of type T2, and S1 is not the |
| 2394 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2395 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2396 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2397 | return ImplicitConversionSequence::Indistinguishable; |
| 2398 | |
| 2399 | // FIXME: the example in the standard doesn't use a qualification |
| 2400 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2401 | QualType T1 = SCS1.getToType(2); |
| 2402 | QualType T2 = SCS2.getToType(2); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2403 | T1 = S.Context.getCanonicalType(T1); |
| 2404 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2405 | Qualifiers T1Quals, T2Quals; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2406 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2407 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2408 | |
| 2409 | // If the types are the same, we won't learn anything by unwrapped |
| 2410 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2411 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2412 | return ImplicitConversionSequence::Indistinguishable; |
| 2413 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2414 | // If the type is an array type, promote the element qualifiers to the type |
| 2415 | // for comparison. |
| 2416 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2417 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2418 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2419 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2420 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2422 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2423 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2424 | // Within each iteration of the loop, we check the qualifiers to |
| 2425 | // determine if this still looks like a qualification |
| 2426 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2427 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2428 | // until there are no more pointers or pointers-to-members left |
| 2429 | // to unwrap. This essentially mimics what |
| 2430 | // IsQualificationConversion does, but here we're checking for a |
| 2431 | // strict subset of qualifiers. |
| 2432 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2433 | // The qualifiers are the same, so this doesn't tell us anything |
| 2434 | // about how the sequences rank. |
| 2435 | ; |
| 2436 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2437 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2438 | if (Result == ImplicitConversionSequence::Worse) |
| 2439 | // Neither has qualifiers that are a subset of the other's |
| 2440 | // qualifiers. |
| 2441 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2442 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2443 | Result = ImplicitConversionSequence::Better; |
| 2444 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2445 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2446 | if (Result == ImplicitConversionSequence::Better) |
| 2447 | // Neither has qualifiers that are a subset of the other's |
| 2448 | // qualifiers. |
| 2449 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2451 | Result = ImplicitConversionSequence::Worse; |
| 2452 | } else { |
| 2453 | // Qualifiers are disjoint. |
| 2454 | return ImplicitConversionSequence::Indistinguishable; |
| 2455 | } |
| 2456 | |
| 2457 | // If the types after this point are equivalent, we're done. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2458 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2459 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2462 | // Check that the winning standard conversion sequence isn't using |
| 2463 | // the deprecated string literal array to pointer conversion. |
| 2464 | switch (Result) { |
| 2465 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2466 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2467 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2468 | break; |
| 2469 | |
| 2470 | case ImplicitConversionSequence::Indistinguishable: |
| 2471 | break; |
| 2472 | |
| 2473 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2474 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2475 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2476 | break; |
| 2477 | } |
| 2478 | |
| 2479 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2482 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2483 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2484 | /// various kinds of derived-to-base conversions (C++ |
| 2485 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2486 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2487 | ImplicitConversionSequence::CompareKind |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2488 | CompareDerivedToBaseConversions(Sema &S, |
| 2489 | const StandardConversionSequence& SCS1, |
| 2490 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2491 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2492 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2493 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2494 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2495 | |
| 2496 | // Adjust the types we're converting from via the array-to-pointer |
| 2497 | // conversion, if we need to. |
| 2498 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2499 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2500 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2501 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2502 | |
| 2503 | // Canonicalize all of the types. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2504 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 2505 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 2506 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 2507 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2508 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2509 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2510 | // |
| 2511 | // If class B is derived directly or indirectly from class A and |
| 2512 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2513 | // |
| 2514 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2515 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2516 | |
| 2517 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2519 | SCS2.Second == ICK_Pointer_Conversion && |
| 2520 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2521 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2522 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2524 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2526 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2527 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2528 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2529 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2530 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2531 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2532 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2533 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
| 2534 | const ObjCObjectType* ToIface1 = ToPointee1->getAs<ObjCObjectType>(); |
| 2535 | const ObjCObjectType* ToIface2 = ToPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2536 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2537 | // -- 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] | 2538 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2539 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2540 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2541 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2542 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2543 | |
| 2544 | if (ToIface1 && ToIface2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2545 | if (S.Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2546 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2547 | else if (S.Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2548 | return ImplicitConversionSequence::Worse; |
| 2549 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2550 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2551 | |
| 2552 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2553 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2554 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2555 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2556 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2557 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2559 | if (FromIface1 && FromIface2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2560 | if (S.Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2561 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2562 | else if (S.Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2563 | return ImplicitConversionSequence::Worse; |
| 2564 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2565 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2568 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2569 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2570 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2571 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2572 | const MemberPointerType * FromMemPointer1 = |
| 2573 | FromType1->getAs<MemberPointerType>(); |
| 2574 | const MemberPointerType * ToMemPointer1 = |
| 2575 | ToType1->getAs<MemberPointerType>(); |
| 2576 | const MemberPointerType * FromMemPointer2 = |
| 2577 | FromType2->getAs<MemberPointerType>(); |
| 2578 | const MemberPointerType * ToMemPointer2 = |
| 2579 | ToType2->getAs<MemberPointerType>(); |
| 2580 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2581 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2582 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2583 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2584 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2585 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2586 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2587 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2588 | // 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] | 2589 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2590 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2591 | return ImplicitConversionSequence::Worse; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2592 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2593 | return ImplicitConversionSequence::Better; |
| 2594 | } |
| 2595 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2596 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2597 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2598 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2599 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2600 | return ImplicitConversionSequence::Worse; |
| 2601 | } |
| 2602 | } |
| 2603 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2604 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2605 | // -- 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] | 2606 | // -- binding of an expression of type C to a reference of type |
| 2607 | // B& is better than binding an expression of type C to a |
| 2608 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2609 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2610 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2611 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2612 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2613 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2614 | return ImplicitConversionSequence::Worse; |
| 2615 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2616 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2617 | // -- 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] | 2618 | // -- binding of an expression of type B to a reference of type |
| 2619 | // A& is better than binding an expression of type C to a |
| 2620 | // reference of type A&, |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2621 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2622 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2623 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2624 | return ImplicitConversionSequence::Better; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2625 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2626 | return ImplicitConversionSequence::Worse; |
| 2627 | } |
| 2628 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2629 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2630 | return ImplicitConversionSequence::Indistinguishable; |
| 2631 | } |
| 2632 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2633 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2634 | /// determine whether they are reference-related, |
| 2635 | /// reference-compatible, reference-compatible with added |
| 2636 | /// qualification, or incompatible, for use in C++ initialization by |
| 2637 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2638 | /// type, and the first type (T1) is the pointee type of the reference |
| 2639 | /// type being initialized. |
| 2640 | Sema::ReferenceCompareResult |
| 2641 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2642 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2643 | bool &DerivedToBase, |
| 2644 | bool &ObjCConversion) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2645 | assert(!OrigT1->isReferenceType() && |
| 2646 | "T1 must be the pointee type of the reference type"); |
| 2647 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2648 | |
| 2649 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2650 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2651 | Qualifiers T1Quals, T2Quals; |
| 2652 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2653 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2654 | |
| 2655 | // C++ [dcl.init.ref]p4: |
| 2656 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2657 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2658 | // T1 is a base class of T2. |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2659 | DerivedToBase = false; |
| 2660 | ObjCConversion = false; |
| 2661 | if (UnqualT1 == UnqualT2) { |
| 2662 | // Nothing to do. |
| 2663 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2664 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2665 | DerivedToBase = true; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2666 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 2667 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 2668 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 2669 | ObjCConversion = true; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2670 | else |
| 2671 | return Ref_Incompatible; |
| 2672 | |
| 2673 | // At this point, we know that T1 and T2 are reference-related (at |
| 2674 | // least). |
| 2675 | |
| 2676 | // If the type is an array type, promote the element qualifiers to the type |
| 2677 | // for comparison. |
| 2678 | if (isa<ArrayType>(T1) && T1Quals) |
| 2679 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2680 | if (isa<ArrayType>(T2) && T2Quals) |
| 2681 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2682 | |
| 2683 | // C++ [dcl.init.ref]p4: |
| 2684 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2685 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2686 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2687 | // overload resolution, cases for which cv1 is greater |
| 2688 | // cv-qualification than cv2 are identified as |
| 2689 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2690 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2691 | return Ref_Compatible; |
| 2692 | else if (T1.isMoreQualifiedThan(T2)) |
| 2693 | return Ref_Compatible_With_Added_Qualification; |
| 2694 | else |
| 2695 | return Ref_Related; |
| 2696 | } |
| 2697 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2698 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2699 | /// with DeclType. Return true if something definite is found. |
| 2700 | static bool |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2701 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 2702 | QualType DeclType, SourceLocation DeclLoc, |
| 2703 | Expr *Init, QualType T2, bool AllowRvalues, |
| 2704 | bool AllowExplicit) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2705 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 2706 | CXXRecordDecl *T2RecordDecl |
| 2707 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2708 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2709 | QualType ToType |
| 2710 | = AllowRvalues? DeclType->getAs<ReferenceType>()->getPointeeType() |
| 2711 | : DeclType; |
| 2712 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2713 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2714 | const UnresolvedSetImpl *Conversions |
| 2715 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2716 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2717 | E = Conversions->end(); I != E; ++I) { |
| 2718 | NamedDecl *D = *I; |
| 2719 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2720 | if (isa<UsingShadowDecl>(D)) |
| 2721 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2722 | |
| 2723 | FunctionTemplateDecl *ConvTemplate |
| 2724 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2725 | CXXConversionDecl *Conv; |
| 2726 | if (ConvTemplate) |
| 2727 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2728 | else |
| 2729 | Conv = cast<CXXConversionDecl>(D); |
| 2730 | |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2731 | // If this is an explicit conversion, and we're not allowed to consider |
| 2732 | // explicit conversions, skip it. |
| 2733 | if (!AllowExplicit && Conv->isExplicit()) |
| 2734 | continue; |
| 2735 | |
| 2736 | if (AllowRvalues) { |
| 2737 | bool DerivedToBase = false; |
| 2738 | bool ObjCConversion = false; |
| 2739 | if (!ConvTemplate && |
| 2740 | S.CompareReferenceRelationship(DeclLoc, |
| 2741 | Conv->getConversionType().getNonReferenceType().getUnqualifiedType(), |
| 2742 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| 2743 | DerivedToBase, ObjCConversion) |
| 2744 | == Sema::Ref_Incompatible) |
| 2745 | continue; |
| 2746 | } else { |
| 2747 | // If the conversion function doesn't return a reference type, |
| 2748 | // it can't be considered for this conversion. An rvalue reference |
| 2749 | // is only acceptable if its referencee is a function type. |
| 2750 | |
| 2751 | const ReferenceType *RefType = |
| 2752 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 2753 | if (!RefType || |
| 2754 | (!RefType->isLValueReferenceType() && |
| 2755 | !RefType->getPointeeType()->isFunctionType())) |
| 2756 | continue; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2757 | } |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2758 | |
| 2759 | if (ConvTemplate) |
| 2760 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2761 | Init, ToType, CandidateSet); |
| 2762 | else |
| 2763 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2764 | ToType, CandidateSet); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2768 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2769 | case OR_Success: |
| 2770 | // C++ [over.ics.ref]p1: |
| 2771 | // |
| 2772 | // [...] If the parameter binds directly to the result of |
| 2773 | // applying a conversion function to the argument |
| 2774 | // expression, the implicit conversion sequence is a |
| 2775 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2776 | // second standard conversion sequence either an identity |
| 2777 | // conversion or, if the conversion function returns an |
| 2778 | // entity of a type that is a derived class of the parameter |
| 2779 | // type, a derived-to-base Conversion. |
| 2780 | if (!Best->FinalConversion.DirectBinding) |
| 2781 | return false; |
| 2782 | |
| 2783 | ICS.setUserDefined(); |
| 2784 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2785 | ICS.UserDefined.After = Best->FinalConversion; |
| 2786 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2787 | ICS.UserDefined.EllipsisConversion = false; |
| 2788 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2789 | ICS.UserDefined.After.DirectBinding && |
| 2790 | "Expected a direct reference binding!"); |
| 2791 | return true; |
| 2792 | |
| 2793 | case OR_Ambiguous: |
| 2794 | ICS.setAmbiguous(); |
| 2795 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2796 | Cand != CandidateSet.end(); ++Cand) |
| 2797 | if (Cand->Viable) |
| 2798 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2799 | return true; |
| 2800 | |
| 2801 | case OR_No_Viable_Function: |
| 2802 | case OR_Deleted: |
| 2803 | // There was no suitable conversion, or we found a deleted |
| 2804 | // conversion; continue with other checks. |
| 2805 | return false; |
| 2806 | } |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 2807 | |
| 2808 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2811 | /// \brief Compute an implicit conversion sequence for reference |
| 2812 | /// initialization. |
| 2813 | static ImplicitConversionSequence |
| 2814 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2815 | SourceLocation DeclLoc, |
| 2816 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2817 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2818 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2819 | |
| 2820 | // Most paths end in a failed conversion. |
| 2821 | ImplicitConversionSequence ICS; |
| 2822 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2823 | |
| 2824 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2825 | QualType T2 = Init->getType(); |
| 2826 | |
| 2827 | // If the initializer is the address of an overloaded function, try |
| 2828 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2829 | // type of the resulting function. |
| 2830 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2831 | DeclAccessPair Found; |
| 2832 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2833 | false, Found)) |
| 2834 | T2 = Fn->getType(); |
| 2835 | } |
| 2836 | |
| 2837 | // Compute some basic properties of the types and the initializer. |
| 2838 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2839 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2840 | bool ObjCConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2841 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2842 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2843 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| 2844 | ObjCConversion); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2846 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2847 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2848 | // A reference to type "cv1 T1" is initialized by an expression |
| 2849 | // of type "cv2 T2" as follows: |
| 2850 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2851 | // -- If reference is an lvalue reference and the initializer expression |
| 2852 | // The next bullet point (T1 is a function) is pretty much equivalent to this |
| 2853 | // one, so it's handled here. |
| 2854 | if (!isRValRef || T1->isFunctionType()) { |
| 2855 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2856 | // reference-compatible with "cv2 T2," or |
| 2857 | // |
| 2858 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2859 | if (InitCategory.isLValue() && |
| 2860 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2861 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2862 | // When a parameter of reference type binds directly (8.5.3) |
| 2863 | // to an argument expression, the implicit conversion sequence |
| 2864 | // is the identity conversion, unless the argument expression |
| 2865 | // has a type that is a derived class of the parameter type, |
| 2866 | // in which case the implicit conversion sequence is a |
| 2867 | // derived-to-base Conversion (13.3.3.1). |
| 2868 | ICS.setStandard(); |
| 2869 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2870 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 2871 | : ObjCConversion? ICK_Compatible_Conversion |
| 2872 | : ICK_Identity; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2873 | ICS.Standard.Third = ICK_Identity; |
| 2874 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2875 | ICS.Standard.setToType(0, T2); |
| 2876 | ICS.Standard.setToType(1, T1); |
| 2877 | ICS.Standard.setToType(2, T1); |
| 2878 | ICS.Standard.ReferenceBinding = true; |
| 2879 | ICS.Standard.DirectBinding = true; |
| 2880 | ICS.Standard.RRefBinding = isRValRef; |
| 2881 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2882 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2883 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2884 | // derived-to-base conversions is suppressed when we're |
| 2885 | // computing the implicit conversion sequence (C++ |
| 2886 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2887 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2888 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2889 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2890 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2891 | // not reference-related to T2, and can be implicitly |
| 2892 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2893 | // is reference-compatible with "cv3 T3" 92) (this |
| 2894 | // conversion is selected by enumerating the applicable |
| 2895 | // conversion functions (13.3.1.6) and choosing the best |
| 2896 | // one through overload resolution (13.3)), |
| 2897 | if (!SuppressUserConversions && T2->isRecordType() && |
| 2898 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2899 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2900 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 2901 | Init, T2, /*AllowRvalues=*/false, |
| 2902 | AllowExplicit)) |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2903 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2904 | } |
| 2905 | } |
| 2906 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2907 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 2908 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2909 | // shall be an rvalue reference and the initializer expression shall be |
| 2910 | // an rvalue or have a function type. |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2911 | // |
| 2912 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2913 | // point, which is that, due to p2 (which short-circuits reference |
| 2914 | // binding by only attempting a simple conversion for non-direct |
| 2915 | // bindings) and p3's strange wording, we allow a const volatile |
| 2916 | // reference to bind to an rvalue. Hence the check for the presence |
| 2917 | // of "const" rather than checking for "const" being the only |
| 2918 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2919 | // This is also the point where rvalue references and lvalue inits no longer |
| 2920 | // go together. |
| 2921 | if ((!isRValRef && !T1.isConstQualified()) || |
| 2922 | (isRValRef && InitCategory.isLValue())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2923 | return ICS; |
| 2924 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2925 | // -- If T1 is a function type, then |
| 2926 | // -- if T2 is the same type as T1, the reference is bound to the |
| 2927 | // initializer expression lvalue; |
| 2928 | // -- if T2 is a class type and the initializer expression can be |
| 2929 | // implicitly converted to an lvalue of type T1 [...], the |
| 2930 | // reference is bound to the function lvalue that is the result |
| 2931 | // of the conversion; |
| 2932 | // This is the same as for the lvalue case above, so it was handled there. |
| 2933 | // -- otherwise, the program is ill-formed. |
| 2934 | // This is the one difference to the lvalue case. |
| 2935 | if (T1->isFunctionType()) |
| 2936 | return ICS; |
| 2937 | |
| 2938 | // -- Otherwise, if T2 is a class type and |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2939 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2940 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2941 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2942 | // -- T1 is not reference-related to T2 and the initializer |
| 2943 | // expression can be implicitly converted to an rvalue |
| 2944 | // of type "cv3 T3" (this conversion is selected by |
| 2945 | // enumerating the applicable conversion functions |
| 2946 | // (13.3.1.6) and choosing the best one through overload |
| 2947 | // resolution (13.3)), |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2948 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2949 | // then the reference is bound to the initializer |
| 2950 | // expression rvalue in the first case and to the object |
| 2951 | // that is the result of the conversion in the second case |
| 2952 | // (or, in either case, to the appropriate base class |
| 2953 | // subobject of the object). |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2954 | if (T2->isRecordType()) { |
| 2955 | // First case: "cv1 T1" is reference-compatible with "cv2 T2". This is a |
| 2956 | // direct binding in C++0x but not in C++03. |
| 2957 | if (InitCategory.isRValue() && |
| 2958 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2959 | ICS.setStandard(); |
| 2960 | ICS.Standard.First = ICK_Identity; |
| 2961 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 2962 | : ObjCConversion? ICK_Compatible_Conversion |
| 2963 | : ICK_Identity; |
| 2964 | ICS.Standard.Third = ICK_Identity; |
| 2965 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2966 | ICS.Standard.setToType(0, T2); |
| 2967 | ICS.Standard.setToType(1, T1); |
| 2968 | ICS.Standard.setToType(2, T1); |
| 2969 | ICS.Standard.ReferenceBinding = true; |
| 2970 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
| 2971 | ICS.Standard.RRefBinding = isRValRef; |
| 2972 | ICS.Standard.CopyConstructor = 0; |
| 2973 | return ICS; |
| 2974 | } |
| 2975 | |
| 2976 | // Second case: not reference-related. |
| 2977 | if (RefRelationship == Sema::Ref_Incompatible && |
| 2978 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2979 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 2980 | Init, T2, /*AllowRvalues=*/true, |
| 2981 | AllowExplicit)) |
| 2982 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2983 | } |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2985 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2986 | // initialized from the initializer expression using the |
| 2987 | // rules for a non-reference copy initialization (8.5). The |
| 2988 | // reference is then bound to the temporary. If T1 is |
| 2989 | // reference-related to T2, cv1 must be the same |
| 2990 | // cv-qualification as, or greater cv-qualification than, |
| 2991 | // cv2; otherwise, the program is ill-formed. |
| 2992 | if (RefRelationship == Sema::Ref_Related) { |
| 2993 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2994 | // we would be reference-compatible or reference-compatible with |
| 2995 | // added qualification. But that wasn't the case, so the reference |
| 2996 | // initialization fails. |
| 2997 | return ICS; |
| 2998 | } |
| 2999 | |
| 3000 | // If at least one of the types is a class type, the types are not |
| 3001 | // related, and we aren't allowed any user conversions, the |
| 3002 | // reference binding fails. This case is important for breaking |
| 3003 | // recursion, since TryImplicitConversion below will attempt to |
| 3004 | // create a temporary through the use of a copy constructor. |
| 3005 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3006 | (T1->isRecordType() || T2->isRecordType())) |
| 3007 | return ICS; |
| 3008 | |
| 3009 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3010 | // When a parameter of reference type is not bound directly to |
| 3011 | // an argument expression, the conversion sequence is the one |
| 3012 | // required to convert the argument expression to the |
| 3013 | // underlying type of the reference according to |
| 3014 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3015 | // to copy-initializing a temporary of the underlying type with |
| 3016 | // the argument expression. Any difference in top-level |
| 3017 | // cv-qualification is subsumed by the initialization itself |
| 3018 | // and does not constitute a conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3019 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3020 | /*AllowExplicit=*/false, |
| 3021 | /*InOverloadResolution=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3022 | |
| 3023 | // Of course, that's still a reference binding. |
| 3024 | if (ICS.isStandard()) { |
| 3025 | ICS.Standard.ReferenceBinding = true; |
| 3026 | ICS.Standard.RRefBinding = isRValRef; |
| 3027 | } else if (ICS.isUserDefined()) { |
| 3028 | ICS.UserDefined.After.ReferenceBinding = true; |
| 3029 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 3030 | } |
| 3031 | return ICS; |
| 3032 | } |
| 3033 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3034 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3035 | /// ToType from the expression From. Return the implicit conversion |
| 3036 | /// sequence required to pass this argument, which may be a bad |
| 3037 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3038 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3039 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3040 | static ImplicitConversionSequence |
| 3041 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | b7f9e6a | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 3042 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3043 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3044 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3045 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3046 | /*FIXME:*/From->getLocStart(), |
| 3047 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3048 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3049 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3050 | return TryImplicitConversion(S, From, ToType, |
| 3051 | SuppressUserConversions, |
| 3052 | /*AllowExplicit=*/false, |
| 3053 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3056 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3057 | /// parameter of the given member function (@c Method) from the |
| 3058 | /// expression @p From. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3059 | static ImplicitConversionSequence |
| 3060 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
| 3061 | CXXMethodDecl *Method, |
| 3062 | CXXRecordDecl *ActingContext) { |
| 3063 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3064 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3065 | // const volatile object. |
| 3066 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3067 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3068 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3069 | |
| 3070 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3071 | // to exit early. |
| 3072 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3073 | |
| 3074 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3075 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3076 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3077 | FromType = PT->getPointeeType(); |
| 3078 | |
| 3079 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3080 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3081 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3082 | // where X is the class of which the function is a member |
| 3083 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 3084 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3086 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3087 | // reference binding here, that allows class rvalues to bind to |
| 3088 | // non-constant references. |
| 3089 | |
| 3090 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 3091 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3092 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3093 | if (ImplicitParamType.getCVRQualifiers() |
| 3094 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3095 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3096 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3097 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3098 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3099 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3100 | |
| 3101 | // Check that we have either the same type or a derived type. It |
| 3102 | // affects the conversion rank. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3103 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3104 | ImplicitConversionKind SecondKind; |
| 3105 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3106 | SecondKind = ICK_Identity; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3107 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3108 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3109 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3110 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3111 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3112 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3113 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3114 | |
| 3115 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3116 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3117 | ICS.Standard.setAsIdentityConversion(); |
| 3118 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3119 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3120 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3121 | ICS.Standard.ReferenceBinding = true; |
| 3122 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 3123 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3124 | return ICS; |
| 3125 | } |
| 3126 | |
| 3127 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3128 | /// the implicit object parameter for the given Method with the given |
| 3129 | /// expression. |
| 3130 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3131 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 3132 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3133 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3134 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3135 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3137 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3138 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3139 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3140 | FromRecordType = PT->getPointeeType(); |
| 3141 | DestType = Method->getThisType(Context); |
| 3142 | } else { |
| 3143 | FromRecordType = From->getType(); |
| 3144 | DestType = ImplicitParamRecordType; |
| 3145 | } |
| 3146 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3147 | // Note that we always use the true parent context when performing |
| 3148 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3149 | ImplicitConversionSequence ICS |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3150 | = TryObjectArgumentInitialization(*this, From->getType(), Method, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3151 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3152 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3153 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3154 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3155 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3156 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3157 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3158 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3160 | if (!Context.hasSameType(From->getType(), DestType)) |
Anders Carlsson | f1b48b7 | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3161 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3162 | From->getType()->isPointerType() ? |
| 3163 | ImplicitCastExpr::RValue : ImplicitCastExpr::LValue); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3164 | return false; |
| 3165 | } |
| 3166 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3167 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3168 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3169 | static ImplicitConversionSequence |
| 3170 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3171 | // FIXME: This is pretty broken. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3172 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3173 | // FIXME: Are these flags correct? |
| 3174 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3176 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3180 | /// of the expression From to bool (C++0x [conv]p3). |
| 3181 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3182 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3183 | if (!ICS.isBad()) |
| 3184 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3185 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3186 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3187 | return Diag(From->getSourceRange().getBegin(), |
| 3188 | diag::err_typecheck_bool_condition) |
| 3189 | << From->getType() << From->getSourceRange(); |
| 3190 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3191 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3192 | |
| 3193 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3194 | /// expression From to 'id'. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3195 | static ImplicitConversionSequence |
| 3196 | TryContextuallyConvertToObjCId(Sema &S, Expr *From) { |
| 3197 | QualType Ty = S.Context.getObjCIdType(); |
| 3198 | return TryImplicitConversion(S, From, Ty, |
| 3199 | // FIXME: Are these flags correct? |
| 3200 | /*SuppressUserConversions=*/false, |
| 3201 | /*AllowExplicit=*/true, |
| 3202 | /*InOverloadResolution=*/false); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3203 | } |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3204 | |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3205 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3206 | /// of the expression From to 'id'. |
| 3207 | bool Sema::PerformContextuallyConvertToObjCId(Expr *&From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3208 | QualType Ty = Context.getObjCIdType(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3209 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(*this, From); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3210 | if (!ICS.isBad()) |
| 3211 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| 3212 | return true; |
| 3213 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3214 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3215 | /// \brief Attempt to convert the given expression to an integral or |
| 3216 | /// enumeration type. |
| 3217 | /// |
| 3218 | /// This routine will attempt to convert an expression of class type to an |
| 3219 | /// integral or enumeration type, if that class type only has a single |
| 3220 | /// conversion to an integral or enumeration type. |
| 3221 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3222 | /// \param Loc The source location of the construct that requires the |
| 3223 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3224 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3225 | /// \param FromE The expression we're converting from. |
| 3226 | /// |
| 3227 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3228 | /// have integral or enumeration type. |
| 3229 | /// |
| 3230 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3231 | /// incomplete class type. |
| 3232 | /// |
| 3233 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3234 | /// explicit conversion function (because no implicit conversion functions |
| 3235 | /// were available). This is a recovery mode. |
| 3236 | /// |
| 3237 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3238 | /// showing which conversion was picked. |
| 3239 | /// |
| 3240 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3241 | /// conversion function that could convert to integral or enumeration type. |
| 3242 | /// |
| 3243 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
| 3244 | /// usable conversion function. |
| 3245 | /// |
| 3246 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3247 | /// function, which may be an extension in this case. |
| 3248 | /// |
| 3249 | /// \returns The expression, converted to an integral or enumeration type if |
| 3250 | /// successful. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3251 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3252 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3253 | const PartialDiagnostic &NotIntDiag, |
| 3254 | const PartialDiagnostic &IncompleteDiag, |
| 3255 | const PartialDiagnostic &ExplicitConvDiag, |
| 3256 | const PartialDiagnostic &ExplicitConvNote, |
| 3257 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3258 | const PartialDiagnostic &AmbigNote, |
| 3259 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3260 | // We can't perform any more checking for type-dependent expressions. |
| 3261 | if (From->isTypeDependent()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3262 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3263 | |
| 3264 | // If the expression already has integral or enumeration type, we're golden. |
| 3265 | QualType T = From->getType(); |
| 3266 | if (T->isIntegralOrEnumerationType()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3267 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3268 | |
| 3269 | // FIXME: Check for missing '()' if T is a function type? |
| 3270 | |
| 3271 | // If we don't have a class type in C++, there's no way we can get an |
| 3272 | // expression of integral or enumeration type. |
| 3273 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3274 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3275 | Diag(Loc, NotIntDiag) |
| 3276 | << T << From->getSourceRange(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3277 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | // We must have a complete class type. |
| 3281 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3282 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3283 | |
| 3284 | // Look for a conversion to an integral or enumeration type. |
| 3285 | UnresolvedSet<4> ViableConversions; |
| 3286 | UnresolvedSet<4> ExplicitConversions; |
| 3287 | const UnresolvedSetImpl *Conversions |
| 3288 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
| 3289 | |
| 3290 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3291 | E = Conversions->end(); |
| 3292 | I != E; |
| 3293 | ++I) { |
| 3294 | if (CXXConversionDecl *Conversion |
| 3295 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3296 | if (Conversion->getConversionType().getNonReferenceType() |
| 3297 | ->isIntegralOrEnumerationType()) { |
| 3298 | if (Conversion->isExplicit()) |
| 3299 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3300 | else |
| 3301 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | switch (ViableConversions.size()) { |
| 3306 | case 0: |
| 3307 | if (ExplicitConversions.size() == 1) { |
| 3308 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3309 | CXXConversionDecl *Conversion |
| 3310 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3311 | |
| 3312 | // The user probably meant to invoke the given explicit |
| 3313 | // conversion; use it. |
| 3314 | QualType ConvTy |
| 3315 | = Conversion->getConversionType().getNonReferenceType(); |
| 3316 | std::string TypeStr; |
| 3317 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
| 3318 | |
| 3319 | Diag(Loc, ExplicitConvDiag) |
| 3320 | << T << ConvTy |
| 3321 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3322 | "static_cast<" + TypeStr + ">(") |
| 3323 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3324 | ")"); |
| 3325 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3326 | << ConvTy->isEnumeralType() << ConvTy; |
| 3327 | |
| 3328 | // If we aren't in a SFINAE context, build a call to the |
| 3329 | // explicit conversion function. |
| 3330 | if (isSFINAEContext()) |
| 3331 | return ExprError(); |
| 3332 | |
| 3333 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3334 | From = BuildCXXMemberCallExpr(From, Found, Conversion); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | // We'll complain below about a non-integral condition type. |
| 3338 | break; |
| 3339 | |
| 3340 | case 1: { |
| 3341 | // Apply this conversion. |
| 3342 | DeclAccessPair Found = ViableConversions[0]; |
| 3343 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3344 | |
| 3345 | CXXConversionDecl *Conversion |
| 3346 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3347 | QualType ConvTy |
| 3348 | = Conversion->getConversionType().getNonReferenceType(); |
| 3349 | if (ConvDiag.getDiagID()) { |
| 3350 | if (isSFINAEContext()) |
| 3351 | return ExprError(); |
| 3352 | |
| 3353 | Diag(Loc, ConvDiag) |
| 3354 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3355 | } |
| 3356 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3357 | From = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3358 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3359 | break; |
| 3360 | } |
| 3361 | |
| 3362 | default: |
| 3363 | Diag(Loc, AmbigDiag) |
| 3364 | << T << From->getSourceRange(); |
| 3365 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3366 | CXXConversionDecl *Conv |
| 3367 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3368 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3369 | Diag(Conv->getLocation(), AmbigNote) |
| 3370 | << ConvTy->isEnumeralType() << ConvTy; |
| 3371 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3372 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3375 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3376 | Diag(Loc, NotIntDiag) |
| 3377 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3378 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3379 | return Owned(From); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3382 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3383 | /// candidate functions, using the given function call arguments. If |
| 3384 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3385 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3386 | /// |
| 3387 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3388 | /// based on an incomplete set of function arguments. This feature is used by |
| 3389 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | void |
| 3391 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3392 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3393 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3394 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3395 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3396 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3398 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3399 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3401 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3403 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3404 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3405 | // If we get here, it's because we're calling a member function |
| 3406 | // that is named without a member access expression (e.g., |
| 3407 | // "this->f") that was either written explicitly or created |
| 3408 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3409 | // function, e.g., X::f(). We use an empty type for the implied |
| 3410 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3411 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3412 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3413 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3414 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3415 | return; |
| 3416 | } |
| 3417 | // We treat a constructor like a non-member function, since its object |
| 3418 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3421 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3422 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3424 | // Overload resolution is always an unevaluated context. |
| 3425 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3426 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3427 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3428 | // C++ [class.copy]p3: |
| 3429 | // A member function template is never instantiated to perform the copy |
| 3430 | // of a class object to an object of its class type. |
| 3431 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 3432 | if (NumArgs == 1 && |
| 3433 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3434 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3435 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3436 | return; |
| 3437 | } |
| 3438 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3439 | // Add this candidate |
| 3440 | CandidateSet.push_back(OverloadCandidate()); |
| 3441 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3442 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3443 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3444 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3445 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3446 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3447 | |
| 3448 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3449 | |
| 3450 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3451 | // parameters is viable only if it has an ellipsis in its parameter |
| 3452 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3453 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 3454 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3455 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3456 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3457 | return; |
| 3458 | } |
| 3459 | |
| 3460 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3461 | // is viable only if the (m+1)st parameter has a default argument |
| 3462 | // (8.3.6). For the purposes of overload resolution, the |
| 3463 | // parameter list is truncated on the right, so that there are |
| 3464 | // exactly m parameters. |
| 3465 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3466 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3467 | // Not enough arguments. |
| 3468 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3469 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3470 | return; |
| 3471 | } |
| 3472 | |
| 3473 | // Determine the implicit conversion sequences for each of the |
| 3474 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3475 | Candidate.Conversions.resize(NumArgs); |
| 3476 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3477 | if (ArgIdx < NumArgsInProto) { |
| 3478 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3479 | // exist for each argument an implicit conversion sequence |
| 3480 | // (13.3.3.1) that converts that argument to the corresponding |
| 3481 | // parameter of F. |
| 3482 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3484 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3485 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3486 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3487 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3488 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3489 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3490 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3491 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3492 | } else { |
| 3493 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3494 | // argument for which there is no corresponding parameter is |
| 3495 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3496 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | } |
| 3500 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3501 | /// \brief Add all of the function declarations in the given function set to |
| 3502 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3503 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3504 | Expr **Args, unsigned NumArgs, |
| 3505 | OverloadCandidateSet& CandidateSet, |
| 3506 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3507 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3508 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3509 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3510 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3511 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3512 | cast<CXXMethodDecl>(FD)->getParent(), |
| 3513 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3514 | CandidateSet, SuppressUserConversions); |
| 3515 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3516 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3517 | SuppressUserConversions); |
| 3518 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3519 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3520 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3521 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3522 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3523 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3524 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3525 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3526 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3527 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3528 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3529 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3530 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3531 | Args, NumArgs, CandidateSet, |
| 3532 | SuppressUserConversions); |
| 3533 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3534 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3537 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3538 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3539 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3540 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3541 | Expr **Args, unsigned NumArgs, |
| 3542 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3543 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3544 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3545 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3546 | |
| 3547 | if (isa<UsingShadowDecl>(Decl)) |
| 3548 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 3549 | |
| 3550 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3551 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3552 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3553 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3554 | /*ExplicitArgs*/ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3555 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3556 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3557 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3558 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3559 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3560 | ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3561 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3565 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3566 | /// of candidate functions, using the given function call arguments |
| 3567 | /// and the object argument (@c Object). For example, in a call |
| 3568 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 3569 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 3570 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3571 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3573 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3574 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 3575 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3576 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3577 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3579 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3580 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3581 | assert(!isa<CXXConstructorDecl>(Method) && |
| 3582 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3583 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3584 | if (!CandidateSet.isNewCandidate(Method)) |
| 3585 | return; |
| 3586 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3587 | // Overload resolution is always an unevaluated context. |
| 3588 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3589 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3590 | // Add this candidate |
| 3591 | CandidateSet.push_back(OverloadCandidate()); |
| 3592 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3593 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3594 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3595 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3596 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3597 | |
| 3598 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3599 | |
| 3600 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3601 | // parameters is viable only if it has an ellipsis in its parameter |
| 3602 | // list (8.3.5). |
| 3603 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3604 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3605 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3606 | return; |
| 3607 | } |
| 3608 | |
| 3609 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3610 | // is viable only if the (m+1)st parameter has a default argument |
| 3611 | // (8.3.6). For the purposes of overload resolution, the |
| 3612 | // parameter list is truncated on the right, so that there are |
| 3613 | // exactly m parameters. |
| 3614 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 3615 | if (NumArgs < MinRequiredArgs) { |
| 3616 | // Not enough arguments. |
| 3617 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3618 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3619 | return; |
| 3620 | } |
| 3621 | |
| 3622 | Candidate.Viable = true; |
| 3623 | Candidate.Conversions.resize(NumArgs + 1); |
| 3624 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3625 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3626 | // The implicit object argument is ignored. |
| 3627 | Candidate.IgnoreObjectArgument = true; |
| 3628 | else { |
| 3629 | // Determine the implicit conversion sequence for the object |
| 3630 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3631 | Candidate.Conversions[0] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3632 | = TryObjectArgumentInitialization(*this, ObjectType, Method, |
| 3633 | ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3634 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3635 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3636 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3637 | return; |
| 3638 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | // Determine the implicit conversion sequences for each of the |
| 3642 | // arguments. |
| 3643 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3644 | if (ArgIdx < NumArgsInProto) { |
| 3645 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3646 | // exist for each argument an implicit conversion sequence |
| 3647 | // (13.3.3.1) that converts that argument to the corresponding |
| 3648 | // parameter of F. |
| 3649 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3651 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3652 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3653 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3654 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3655 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3656 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3657 | break; |
| 3658 | } |
| 3659 | } else { |
| 3660 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3661 | // argument for which there is no corresponding parameter is |
| 3662 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3663 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3664 | } |
| 3665 | } |
| 3666 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 3667 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3668 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 3669 | /// set, using template argument deduction to produce an appropriate member |
| 3670 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3672 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3673 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3674 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3675 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3676 | QualType ObjectType, |
| 3677 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3678 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3679 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3680 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3681 | return; |
| 3682 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3683 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3685 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | // 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] | 3687 | // candidate functions in the usual way.113) A given name can refer to one |
| 3688 | // or more function templates and also to a set of overloaded non-template |
| 3689 | // functions. In such a case, the candidate functions generated from each |
| 3690 | // function template are combined with the set of non-template candidate |
| 3691 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3692 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3693 | FunctionDecl *Specialization = 0; |
| 3694 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3695 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3696 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3697 | CandidateSet.push_back(OverloadCandidate()); |
| 3698 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3699 | Candidate.FoundDecl = FoundDecl; |
| 3700 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 3701 | Candidate.Viable = false; |
| 3702 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3703 | Candidate.IsSurrogate = false; |
| 3704 | Candidate.IgnoreObjectArgument = false; |
| 3705 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3706 | Info); |
| 3707 | return; |
| 3708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3710 | // Add the function template specialization produced by template argument |
| 3711 | // deduction as a candidate. |
| 3712 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3713 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3714 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3715 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3716 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3717 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3720 | /// \brief Add a C++ function template specialization as a candidate |
| 3721 | /// in the candidate set, using template argument deduction to produce |
| 3722 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3723 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3724 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3725 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3726 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3727 | Expr **Args, unsigned NumArgs, |
| 3728 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3729 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3730 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3731 | return; |
| 3732 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3733 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3735 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | // 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] | 3737 | // candidate functions in the usual way.113) A given name can refer to one |
| 3738 | // or more function templates and also to a set of overloaded non-template |
| 3739 | // functions. In such a case, the candidate functions generated from each |
| 3740 | // function template are combined with the set of non-template candidate |
| 3741 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3742 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3743 | FunctionDecl *Specialization = 0; |
| 3744 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3745 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3746 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3747 | CandidateSet.push_back(OverloadCandidate()); |
| 3748 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3749 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3750 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3751 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3752 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3753 | Candidate.IsSurrogate = false; |
| 3754 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3755 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3756 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3757 | return; |
| 3758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3760 | // Add the function template specialization produced by template argument |
| 3761 | // deduction as a candidate. |
| 3762 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3763 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3764 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3767 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3769 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | /// 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] | 3771 | /// (which may or may not be the same type as the type that the |
| 3772 | /// conversion function produces). |
| 3773 | void |
| 3774 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3775 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3776 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3777 | Expr *From, QualType ToType, |
| 3778 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3779 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3780 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3781 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3782 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3783 | return; |
| 3784 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3785 | // Overload resolution is always an unevaluated context. |
| 3786 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3787 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3788 | // Add this candidate |
| 3789 | CandidateSet.push_back(OverloadCandidate()); |
| 3790 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3791 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3792 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3793 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3794 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3795 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3796 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3797 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3798 | Candidate.Viable = true; |
| 3799 | Candidate.Conversions.resize(1); |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | bca3932 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 3801 | // C++ [over.match.funcs]p4: |
| 3802 | // For conversion functions, the function is considered to be a member of |
| 3803 | // the class of the implicit implied object argument for the purpose of |
| 3804 | // defining the type of the implicit object parameter. |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3805 | // |
| 3806 | // Determine the implicit conversion sequence for the implicit |
| 3807 | // object parameter. |
| 3808 | QualType ImplicitParamType = From->getType(); |
| 3809 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 3810 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 3811 | CXXRecordDecl *ConversionContext |
| 3812 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
| 3813 | |
| 3814 | Candidate.Conversions[0] |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3815 | = TryObjectArgumentInitialization(*this, From->getType(), Conversion, |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3816 | ConversionContext); |
| 3817 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3818 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3819 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3820 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3821 | return; |
| 3822 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 3823 | |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3824 | // We won't go through a user-define type conversion function to convert a |
| 3825 | // derived to base as such conversions are given Conversion Rank. They only |
| 3826 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3827 | QualType FromCanon |
| 3828 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3829 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3830 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3831 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3832 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3833 | return; |
| 3834 | } |
| 3835 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3836 | // To determine what the conversion from the result of calling the |
| 3837 | // conversion function to the type we're eventually trying to |
| 3838 | // convert to (ToType), we need to synthesize a call to the |
| 3839 | // conversion function and attempt copy initialization from it. This |
| 3840 | // makes sure that we get the right semantics with respect to |
| 3841 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3842 | // call on the stack and we don't need its arguments to be |
| 3843 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3845 | From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3846 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 3847 | Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3848 | CastExpr::CK_FunctionToPointerDecay, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3849 | &ConversionRef, ImplicitCastExpr::RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | |
| 3851 | // 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] | 3852 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3853 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3855 | Conversion->getConversionType().getNonLValueExprType(Context), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3856 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3858 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3859 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3860 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3862 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3863 | case ImplicitConversionSequence::StandardConversion: |
| 3864 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3865 | |
| 3866 | // C++ [over.ics.user]p3: |
| 3867 | // If the user-defined conversion is specified by a specialization of a |
| 3868 | // conversion function template, the second standard conversion sequence |
| 3869 | // shall have exact match rank. |
| 3870 | if (Conversion->getPrimaryTemplate() && |
| 3871 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3872 | Candidate.Viable = false; |
| 3873 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3874 | } |
| 3875 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3876 | break; |
| 3877 | |
| 3878 | case ImplicitConversionSequence::BadConversion: |
| 3879 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3880 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3881 | break; |
| 3882 | |
| 3883 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3884 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3885 | "Can only end up with a standard conversion sequence or failure"); |
| 3886 | } |
| 3887 | } |
| 3888 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3889 | /// \brief Adds a conversion function template specialization |
| 3890 | /// candidate to the overload set, using template argument deduction |
| 3891 | /// to deduce the template arguments of the conversion function |
| 3892 | /// template from the type that we are converting to (C++ |
| 3893 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3895 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3896 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3897 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3898 | Expr *From, QualType ToType, |
| 3899 | OverloadCandidateSet &CandidateSet) { |
| 3900 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3901 | "Only conversion function templates permitted here"); |
| 3902 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3903 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3904 | return; |
| 3905 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3906 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3907 | CXXConversionDecl *Specialization = 0; |
| 3908 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3910 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3911 | CandidateSet.push_back(OverloadCandidate()); |
| 3912 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3913 | Candidate.FoundDecl = FoundDecl; |
| 3914 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3915 | Candidate.Viable = false; |
| 3916 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3917 | Candidate.IsSurrogate = false; |
| 3918 | Candidate.IgnoreObjectArgument = false; |
| 3919 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3920 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3921 | return; |
| 3922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3924 | // Add the conversion function template specialization produced by |
| 3925 | // template argument deduction as a candidate. |
| 3926 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3927 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3928 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3931 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3932 | /// converts the given @c Object to a function pointer via the |
| 3933 | /// conversion function @c Conversion, and then attempts to call it |
| 3934 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3935 | /// the type of function that we'll eventually be calling. |
| 3936 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3937 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3938 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3939 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3940 | QualType ObjectType, |
| 3941 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3942 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3943 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3944 | return; |
| 3945 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3946 | // Overload resolution is always an unevaluated context. |
| 3947 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3948 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3949 | CandidateSet.push_back(OverloadCandidate()); |
| 3950 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3951 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3952 | Candidate.Function = 0; |
| 3953 | Candidate.Surrogate = Conversion; |
| 3954 | Candidate.Viable = true; |
| 3955 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3956 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3957 | Candidate.Conversions.resize(NumArgs + 1); |
| 3958 | |
| 3959 | // Determine the implicit conversion sequence for the implicit |
| 3960 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | ImplicitConversionSequence ObjectInit |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3962 | = TryObjectArgumentInitialization(*this, ObjectType, Conversion, |
| 3963 | ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3964 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3965 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3966 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3967 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3968 | return; |
| 3969 | } |
| 3970 | |
| 3971 | // The first conversion is actually a user-defined conversion whose |
| 3972 | // first conversion is ObjectInit's standard conversion (which is |
| 3973 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3974 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3975 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3976 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3977 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3979 | = Candidate.Conversions[0].UserDefined.Before; |
| 3980 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3981 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3982 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3983 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3984 | |
| 3985 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3986 | // parameters is viable only if it has an ellipsis in its parameter |
| 3987 | // list (8.3.5). |
| 3988 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3989 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3990 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3991 | return; |
| 3992 | } |
| 3993 | |
| 3994 | // Function types don't have any default arguments, so just check if |
| 3995 | // we have enough arguments. |
| 3996 | if (NumArgs < NumArgsInProto) { |
| 3997 | // Not enough arguments. |
| 3998 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3999 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4000 | return; |
| 4001 | } |
| 4002 | |
| 4003 | // Determine the implicit conversion sequences for each of the |
| 4004 | // arguments. |
| 4005 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4006 | if (ArgIdx < NumArgsInProto) { |
| 4007 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4008 | // exist for each argument an implicit conversion sequence |
| 4009 | // (13.3.3.1) that converts that argument to the corresponding |
| 4010 | // parameter of F. |
| 4011 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4013 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4014 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4015 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4016 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4017 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4018 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4019 | break; |
| 4020 | } |
| 4021 | } else { |
| 4022 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4023 | // argument for which there is no corresponding parameter is |
| 4024 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4025 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | } |
| 4029 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4030 | /// \brief Add overload candidates for overloaded operators that are |
| 4031 | /// member functions. |
| 4032 | /// |
| 4033 | /// Add the overloaded operator candidates that are member functions |
| 4034 | /// for the operator Op that was used in an operator expression such |
| 4035 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4036 | /// CandidateSet will store the added overload candidates. (C++ |
| 4037 | /// [over.match.oper]). |
| 4038 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4039 | SourceLocation OpLoc, |
| 4040 | Expr **Args, unsigned NumArgs, |
| 4041 | OverloadCandidateSet& CandidateSet, |
| 4042 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4043 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4044 | |
| 4045 | // C++ [over.match.oper]p3: |
| 4046 | // For a unary operator @ with an operand of a type whose |
| 4047 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4048 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4049 | // a right operand of a type whose cv-unqualified version is T2, |
| 4050 | // three sets of candidate functions, designated member |
| 4051 | // candidates, non-member candidates and built-in candidates, are |
| 4052 | // constructed as follows: |
| 4053 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4054 | |
| 4055 | // -- If T1 is a class type, the set of member candidates is the |
| 4056 | // result of the qualified lookup of T1::operator@ |
| 4057 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4058 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4059 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4060 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4061 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4062 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4064 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4065 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4066 | Operators.suppressDiagnostics(); |
| 4067 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4069 | OperEnd = Operators.end(); |
| 4070 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4071 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4072 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4073 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4074 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4075 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4078 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4079 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4080 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4081 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4082 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4083 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4084 | /// (at the beginning of the argument list) that will be contextually |
| 4085 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4086 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4087 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4088 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4089 | bool IsAssignmentOperator, |
| 4090 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4091 | // Overload resolution is always an unevaluated context. |
| 4092 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 4093 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4094 | // Add this candidate |
| 4095 | CandidateSet.push_back(OverloadCandidate()); |
| 4096 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4097 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4098 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4099 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4100 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4101 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4102 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4103 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4104 | |
| 4105 | // Determine the implicit conversion sequences for each of the |
| 4106 | // arguments. |
| 4107 | Candidate.Viable = true; |
| 4108 | Candidate.Conversions.resize(NumArgs); |
| 4109 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4110 | // C++ [over.match.oper]p4: |
| 4111 | // For the built-in assignment operators, conversions of the |
| 4112 | // left operand are restricted as follows: |
| 4113 | // -- no temporaries are introduced to hold the left operand, and |
| 4114 | // -- no user-defined conversions are applied to the left |
| 4115 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4116 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4117 | // |
| 4118 | // We block these conversions by turning off user-defined |
| 4119 | // conversions, since that is the only way that initialization of |
| 4120 | // a reference to a non-class type can occur from something that |
| 4121 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4122 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4124 | "Contextual conversion to bool requires bool type"); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4125 | Candidate.Conversions[ArgIdx] |
| 4126 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4127 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4129 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4130 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4131 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4132 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4133 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4134 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4135 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4136 | break; |
| 4137 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4142 | /// candidate operator functions for built-in operators (C++ |
| 4143 | /// [over.built]). The types are separated into pointer types and |
| 4144 | /// enumeration types. |
| 4145 | class BuiltinCandidateTypeSet { |
| 4146 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4147 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4148 | |
| 4149 | /// PointerTypes - The set of pointer types that will be used in the |
| 4150 | /// built-in candidates. |
| 4151 | TypeSet PointerTypes; |
| 4152 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4153 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4154 | /// used in the built-in candidates. |
| 4155 | TypeSet MemberPointerTypes; |
| 4156 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4157 | /// EnumerationTypes - The set of enumeration types that will be |
| 4158 | /// used in the built-in candidates. |
| 4159 | TypeSet EnumerationTypes; |
| 4160 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4161 | /// \brief The set of vector types that will be used in the built-in |
| 4162 | /// candidates. |
| 4163 | TypeSet VectorTypes; |
| 4164 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4165 | /// Sema - The semantic analysis instance where we are building the |
| 4166 | /// candidate type set. |
| 4167 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4169 | /// Context - The AST context in which we will build the type sets. |
| 4170 | ASTContext &Context; |
| 4171 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4172 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4173 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4174 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4175 | |
| 4176 | public: |
| 4177 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4178 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4179 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4180 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4181 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4182 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4183 | void AddTypesConvertedFrom(QualType Ty, |
| 4184 | SourceLocation Loc, |
| 4185 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4186 | bool AllowExplicitConversions, |
| 4187 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4188 | |
| 4189 | /// pointer_begin - First pointer type found; |
| 4190 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4191 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4192 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4193 | iterator pointer_end() { return PointerTypes.end(); } |
| 4194 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4195 | /// member_pointer_begin - First member pointer type found; |
| 4196 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4197 | |
| 4198 | /// member_pointer_end - Past the last member pointer type found; |
| 4199 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4200 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4201 | /// enumeration_begin - First enumeration type found; |
| 4202 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4203 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4204 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4205 | iterator enumeration_end() { return EnumerationTypes.end(); } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4206 | |
| 4207 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4208 | iterator vector_end() { return VectorTypes.end(); } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4209 | }; |
| 4210 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4211 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4212 | /// the set of pointer types along with any more-qualified variants of |
| 4213 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4214 | /// will add "int const *", "int const volatile *", "int const |
| 4215 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4216 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4217 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4218 | /// |
| 4219 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4220 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4221 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4222 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4223 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4224 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4225 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4226 | return false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4227 | |
| 4228 | QualType PointeeTy; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4229 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4230 | bool buildObjCPtr = false; |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4231 | if (!PointerTy) { |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4232 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4233 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4234 | buildObjCPtr = true; |
| 4235 | } |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4236 | else |
| 4237 | assert(false && "type was not a pointer type!"); |
| 4238 | } |
| 4239 | else |
| 4240 | PointeeTy = PointerTy->getPointeeType(); |
| 4241 | |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4242 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4243 | // (the qualifier would sink to the element type), and for another, the |
| 4244 | // only overload situation where it matters is subscript or pointer +- int, |
| 4245 | // and those shouldn't have qualifier variants anyway. |
| 4246 | if (PointeeTy->isArrayType()) |
| 4247 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4248 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4249 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4250 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4251 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4252 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 4253 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4254 | // Iterate through all strict supersets of BaseCVR. |
| 4255 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4256 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4257 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4258 | // in the types. |
| 4259 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4260 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4261 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | 957b4df | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4262 | if (!buildObjCPtr) |
| 4263 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 4264 | else |
| 4265 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | return true; |
| 4269 | } |
| 4270 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4271 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4272 | /// to the set of pointer types along with any more-qualified variants of |
| 4273 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4274 | /// will add "int const *", "int const volatile *", "int const |
| 4275 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4276 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4277 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4278 | /// |
| 4279 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4280 | bool |
| 4281 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4282 | QualType Ty) { |
| 4283 | // Insert this type. |
| 4284 | if (!MemberPointerTypes.insert(Ty)) |
| 4285 | return false; |
| 4286 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4287 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4288 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4289 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4290 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4291 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4292 | // (the qualifier would sink to the element type), and for another, the |
| 4293 | // only overload situation where it matters is subscript or pointer +- int, |
| 4294 | // and those shouldn't have qualifier variants anyway. |
| 4295 | if (PointeeTy->isArrayType()) |
| 4296 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4297 | const Type *ClassTy = PointerTy->getClass(); |
| 4298 | |
| 4299 | // Iterate through all strict supersets of the pointee type's CVR |
| 4300 | // qualifiers. |
| 4301 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4302 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4303 | if ((CVR | BaseCVR) != CVR) continue; |
| 4304 | |
| 4305 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 4306 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | return true; |
| 4310 | } |
| 4311 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4312 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4313 | /// 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] | 4314 | /// primarily interested in pointer types and enumeration types. We also |
| 4315 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4316 | /// AllowUserConversions is true if we should look at the conversion |
| 4317 | /// functions of a class type, and AllowExplicitConversions if we |
| 4318 | /// should also include the explicit conversion functions of a class |
| 4319 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4321 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4322 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4323 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4324 | bool AllowExplicitConversions, |
| 4325 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4326 | // Only deal with canonical types. |
| 4327 | Ty = Context.getCanonicalType(Ty); |
| 4328 | |
| 4329 | // Look through reference types; they aren't part of the type of an |
| 4330 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4331 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4332 | Ty = RefTy->getPointeeType(); |
| 4333 | |
| 4334 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4335 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4336 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 4337 | // If we're dealing with an array type, decay to the pointer. |
| 4338 | if (Ty->isArrayType()) |
| 4339 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
Fariborz Jahanian | 2e2acec | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4340 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 4341 | PointerTypes.insert(Ty); |
| 4342 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4343 | // Insert our type, and its more-qualified variants, into the set |
| 4344 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4345 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4346 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4347 | } else if (Ty->isMemberPointerType()) { |
| 4348 | // Member pointers are far easier, since the pointee can't be converted. |
| 4349 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4350 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4351 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4352 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4353 | } else if (Ty->isVectorType()) { |
| 4354 | VectorTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4355 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4356 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4357 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4358 | // No conversion functions in incomplete types. |
| 4359 | return; |
| 4360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4362 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4363 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 4364 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4365 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4366 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4367 | NamedDecl *D = I.getDecl(); |
| 4368 | if (isa<UsingShadowDecl>(D)) |
| 4369 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4370 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4371 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4372 | // about which builtin types we can convert to. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4373 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4374 | continue; |
| 4375 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4376 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4377 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4378 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4379 | VisibleQuals); |
| 4380 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4381 | } |
| 4382 | } |
| 4383 | } |
| 4384 | } |
| 4385 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4386 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4387 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4388 | /// given type to the candidate set. |
| 4389 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4390 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4392 | unsigned NumArgs, |
| 4393 | OverloadCandidateSet &CandidateSet) { |
| 4394 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4395 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4396 | // T& operator=(T&, T) |
| 4397 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4398 | ParamTypes[1] = T; |
| 4399 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4400 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4402 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4403 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4404 | ParamTypes[0] |
| 4405 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4406 | ParamTypes[1] = T; |
| 4407 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4408 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4409 | } |
| 4410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4412 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4413 | /// 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] | 4414 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4415 | Qualifiers VRQuals; |
| 4416 | const RecordType *TyRec; |
| 4417 | if (const MemberPointerType *RHSMPType = |
| 4418 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4419 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4420 | else |
| 4421 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4422 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4423 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4424 | VRQuals.addVolatile(); |
| 4425 | VRQuals.addRestrict(); |
| 4426 | return VRQuals; |
| 4427 | } |
| 4428 | |
| 4429 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4430 | if (!ClassDecl->hasDefinition()) |
| 4431 | return VRQuals; |
| 4432 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4433 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4434 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4435 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4436 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4437 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4438 | NamedDecl *D = I.getDecl(); |
| 4439 | if (isa<UsingShadowDecl>(D)) |
| 4440 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4441 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4442 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4443 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4444 | CanTy = ResTypeRef->getPointeeType(); |
| 4445 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4446 | // as see them. |
| 4447 | bool done = false; |
| 4448 | while (!done) { |
| 4449 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4450 | CanTy = ResTypePtr->getPointeeType(); |
| 4451 | else if (const MemberPointerType *ResTypeMPtr = |
| 4452 | CanTy->getAs<MemberPointerType>()) |
| 4453 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4454 | else |
| 4455 | done = true; |
| 4456 | if (CanTy.isVolatileQualified()) |
| 4457 | VRQuals.addVolatile(); |
| 4458 | if (CanTy.isRestrictQualified()) |
| 4459 | VRQuals.addRestrict(); |
| 4460 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4461 | return VRQuals; |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | return VRQuals; |
| 4466 | } |
| 4467 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4468 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 4469 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 4470 | /// on the operator @p Op and the arguments given. For example, if the |
| 4471 | /// operator is a binary '+', this routine might add "int |
| 4472 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4473 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4475 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4476 | Expr **Args, unsigned NumArgs, |
| 4477 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4478 | // The set of "promoted arithmetic types", which are the arithmetic |
| 4479 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 4480 | // that the first few of these types are the promoted integral |
| 4481 | // types; these types need to be first. |
| 4482 | // FIXME: What about complex? |
| 4483 | const unsigned FirstIntegralType = 0; |
| 4484 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4486 | LastPromotedIntegralType = 13; |
| 4487 | const unsigned FirstPromotedArithmeticType = 7, |
| 4488 | LastPromotedArithmeticType = 16; |
| 4489 | const unsigned NumArithmeticTypes = 16; |
| 4490 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 4492 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4493 | Context.SignedCharTy, Context.ShortTy, |
| 4494 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 4495 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 4496 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 4497 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 4498 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 4499 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 4500 | "Invalid first promoted integral type"); |
| 4501 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 4502 | == Context.UnsignedLongLongTy && |
| 4503 | "Invalid last promoted integral type"); |
| 4504 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 4505 | "Invalid first promoted arithmetic type"); |
| 4506 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 4507 | == Context.LongDoubleTy && |
| 4508 | "Invalid last promoted arithmetic type"); |
| 4509 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4510 | // Find all of the types that the arguments can convert to, but only |
| 4511 | // if the operator we're looking at has built-in operator candidates |
| 4512 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4513 | Qualifiers VisibleTypeConversionsQuals; |
| 4514 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4515 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4516 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 4517 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4518 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4519 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4520 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 4521 | OpLoc, |
| 4522 | true, |
| 4523 | (Op == OO_Exclaim || |
| 4524 | Op == OO_AmpAmp || |
| 4525 | Op == OO_PipePipe), |
| 4526 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4527 | |
| 4528 | bool isComparison = false; |
| 4529 | switch (Op) { |
| 4530 | case OO_None: |
| 4531 | case NUM_OVERLOADED_OPERATORS: |
| 4532 | assert(false && "Expected an overloaded operator"); |
| 4533 | break; |
| 4534 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4535 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4536 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4537 | goto UnaryStar; |
| 4538 | else |
| 4539 | goto BinaryStar; |
| 4540 | break; |
| 4541 | |
| 4542 | case OO_Plus: // '+' is either unary or binary |
| 4543 | if (NumArgs == 1) |
| 4544 | goto UnaryPlus; |
| 4545 | else |
| 4546 | goto BinaryPlus; |
| 4547 | break; |
| 4548 | |
| 4549 | case OO_Minus: // '-' is either unary or binary |
| 4550 | if (NumArgs == 1) |
| 4551 | goto UnaryMinus; |
| 4552 | else |
| 4553 | goto BinaryMinus; |
| 4554 | break; |
| 4555 | |
| 4556 | case OO_Amp: // '&' is either unary or binary |
| 4557 | if (NumArgs == 1) |
| 4558 | goto UnaryAmp; |
| 4559 | else |
| 4560 | goto BinaryAmp; |
| 4561 | |
| 4562 | case OO_PlusPlus: |
| 4563 | case OO_MinusMinus: |
| 4564 | // C++ [over.built]p3: |
| 4565 | // |
| 4566 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 4567 | // is either volatile or empty, there exist candidate operator |
| 4568 | // functions of the form |
| 4569 | // |
| 4570 | // VQ T& operator++(VQ T&); |
| 4571 | // T operator++(VQ T&, int); |
| 4572 | // |
| 4573 | // C++ [over.built]p4: |
| 4574 | // |
| 4575 | // For every pair (T, VQ), where T is an arithmetic type other |
| 4576 | // than bool, and VQ is either volatile or empty, there exist |
| 4577 | // candidate operator functions of the form |
| 4578 | // |
| 4579 | // VQ T& operator--(VQ T&); |
| 4580 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4582 | Arith < NumArithmeticTypes; ++Arith) { |
| 4583 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4584 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4585 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4586 | |
| 4587 | // Non-volatile version. |
| 4588 | if (NumArgs == 1) |
| 4589 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4590 | else |
| 4591 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4592 | // heuristic to reduce number of builtin candidates in the set. |
| 4593 | // Add volatile version only if there are conversions to a volatile type. |
| 4594 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4595 | // Volatile version |
| 4596 | ParamTypes[0] |
| 4597 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 4598 | if (NumArgs == 1) |
| 4599 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4600 | else |
| 4601 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 4602 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | // C++ [over.built]p5: |
| 4606 | // |
| 4607 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4608 | // cv-unqualified object type, and VQ is either volatile or |
| 4609 | // empty, there exist candidate operator functions of the form |
| 4610 | // |
| 4611 | // T*VQ& operator++(T*VQ&); |
| 4612 | // T*VQ& operator--(T*VQ&); |
| 4613 | // T* operator++(T*VQ&, int); |
| 4614 | // T* operator--(T*VQ&, int); |
| 4615 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4616 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4617 | // Skip pointer types that aren't pointers to object types. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4618 | if (!(*Ptr)->getPointeeType()->isIncompleteOrObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4619 | continue; |
| 4620 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4621 | QualType ParamTypes[2] = { |
| 4622 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4623 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4625 | // Without volatile |
| 4626 | if (NumArgs == 1) |
| 4627 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4628 | else |
| 4629 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4630 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4631 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4632 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4633 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4634 | ParamTypes[0] |
| 4635 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4636 | if (NumArgs == 1) |
| 4637 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4638 | else |
| 4639 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4640 | } |
| 4641 | } |
| 4642 | break; |
| 4643 | |
| 4644 | UnaryStar: |
| 4645 | // C++ [over.built]p6: |
| 4646 | // For every cv-qualified or cv-unqualified object type T, there |
| 4647 | // exist candidate operator functions of the form |
| 4648 | // |
| 4649 | // T& operator*(T*); |
| 4650 | // |
| 4651 | // C++ [over.built]p7: |
| 4652 | // For every function type T, there exist candidate operator |
| 4653 | // functions of the form |
| 4654 | // T& operator*(T*); |
| 4655 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4656 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4657 | QualType ParamTy = *Ptr; |
Argyrios Kyrtzidis | 42d0f2a | 2010-08-23 07:12:16 +0000 | [diff] [blame] | 4658 | QualType PointeeTy = ParamTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4660 | &ParamTy, Args, 1, CandidateSet); |
| 4661 | } |
| 4662 | break; |
| 4663 | |
| 4664 | UnaryPlus: |
| 4665 | // C++ [over.built]p8: |
| 4666 | // For every type T, there exist candidate operator functions of |
| 4667 | // the form |
| 4668 | // |
| 4669 | // T* operator+(T*); |
| 4670 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4671 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4672 | QualType ParamTy = *Ptr; |
| 4673 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 4674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4676 | // Fall through |
| 4677 | |
| 4678 | UnaryMinus: |
| 4679 | // C++ [over.built]p9: |
| 4680 | // For every promoted arithmetic type T, there exist candidate |
| 4681 | // operator functions of the form |
| 4682 | // |
| 4683 | // T operator+(T); |
| 4684 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4686 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 4687 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 4688 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 4689 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4690 | |
| 4691 | // Extension: We also add these operators for vector types. |
| 4692 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4693 | VecEnd = CandidateTypes.vector_end(); |
| 4694 | Vec != VecEnd; ++Vec) { |
| 4695 | QualType VecTy = *Vec; |
| 4696 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4697 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4698 | break; |
| 4699 | |
| 4700 | case OO_Tilde: |
| 4701 | // C++ [over.built]p10: |
| 4702 | // For every promoted integral type T, there exist candidate |
| 4703 | // operator functions of the form |
| 4704 | // |
| 4705 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4707 | Int < LastPromotedIntegralType; ++Int) { |
| 4708 | QualType IntTy = ArithmeticTypes[Int]; |
| 4709 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 4710 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4711 | |
| 4712 | // Extension: We also add this operator for vector types. |
| 4713 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4714 | VecEnd = CandidateTypes.vector_end(); |
| 4715 | Vec != VecEnd; ++Vec) { |
| 4716 | QualType VecTy = *Vec; |
| 4717 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4718 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4719 | break; |
| 4720 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4721 | case OO_New: |
| 4722 | case OO_Delete: |
| 4723 | case OO_Array_New: |
| 4724 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4725 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4726 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4727 | break; |
| 4728 | |
| 4729 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4730 | UnaryAmp: |
| 4731 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4732 | // C++ [over.match.oper]p3: |
| 4733 | // -- For the operator ',', the unary operator '&', or the |
| 4734 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4735 | break; |
| 4736 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4737 | case OO_EqualEqual: |
| 4738 | case OO_ExclaimEqual: |
| 4739 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | // For every pointer to member type T, there exist candidate operator |
| 4741 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4742 | // |
| 4743 | // bool operator==(T,T); |
| 4744 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4746 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4747 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4748 | MemPtr != MemPtrEnd; |
| 4749 | ++MemPtr) { |
| 4750 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4751 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4754 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4755 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4756 | case OO_Less: |
| 4757 | case OO_Greater: |
| 4758 | case OO_LessEqual: |
| 4759 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4760 | // C++ [over.built]p15: |
| 4761 | // |
| 4762 | // For every pointer or enumeration type T, there exist |
| 4763 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4764 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4765 | // bool operator<(T, T); |
| 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 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4772 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4773 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4774 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4775 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4777 | = CandidateTypes.enumeration_begin(); |
| 4778 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4779 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4780 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4781 | } |
| 4782 | |
| 4783 | // Fall through. |
| 4784 | isComparison = true; |
| 4785 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4786 | BinaryPlus: |
| 4787 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4788 | if (!isComparison) { |
| 4789 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4790 | |
| 4791 | // C++ [over.built]p13: |
| 4792 | // |
| 4793 | // For every cv-qualified or cv-unqualified object type T |
| 4794 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4796 | // T* operator+(T*, ptrdiff_t); |
| 4797 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4798 | // T* operator-(T*, ptrdiff_t); |
| 4799 | // T* operator+(ptrdiff_t, T*); |
| 4800 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4801 | // |
| 4802 | // C++ [over.built]p14: |
| 4803 | // |
| 4804 | // For every T, where T is a pointer to object type, there |
| 4805 | // exist candidate operator functions of the form |
| 4806 | // |
| 4807 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4809 | = CandidateTypes.pointer_begin(); |
| 4810 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4811 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4812 | |
| 4813 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4814 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4815 | |
| 4816 | if (Op == OO_Plus) { |
| 4817 | // T* operator+(ptrdiff_t, T*); |
| 4818 | ParamTypes[0] = ParamTypes[1]; |
| 4819 | ParamTypes[1] = *Ptr; |
| 4820 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4821 | } else { |
| 4822 | // ptrdiff_t operator-(T, T); |
| 4823 | ParamTypes[1] = *Ptr; |
| 4824 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4825 | Args, 2, CandidateSet); |
| 4826 | } |
| 4827 | } |
| 4828 | } |
| 4829 | // Fall through |
| 4830 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4831 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4832 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4833 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4834 | // C++ [over.built]p12: |
| 4835 | // |
| 4836 | // For every pair of promoted arithmetic types L and R, there |
| 4837 | // exist candidate operator functions of the form |
| 4838 | // |
| 4839 | // LR operator*(L, R); |
| 4840 | // LR operator/(L, R); |
| 4841 | // LR operator+(L, R); |
| 4842 | // LR operator-(L, R); |
| 4843 | // bool 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 | // |
| 4850 | // where LR is the result of the usual arithmetic conversions |
| 4851 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4852 | // |
| 4853 | // C++ [over.built]p24: |
| 4854 | // |
| 4855 | // For every pair of promoted arithmetic types L and R, there exist |
| 4856 | // candidate operator functions of the form |
| 4857 | // |
| 4858 | // LR operator?(bool, L, R); |
| 4859 | // |
| 4860 | // where LR is the result of the usual arithmetic conversions |
| 4861 | // between types L and R. |
| 4862 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4864 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4866 | Right < LastPromotedArithmeticType; ++Right) { |
| 4867 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4868 | QualType Result |
| 4869 | = isComparison |
| 4870 | ? Context.BoolTy |
| 4871 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4872 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4873 | } |
| 4874 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4875 | |
| 4876 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 4877 | // conditional operator for vector types. |
| 4878 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4879 | Vec1End = CandidateTypes.vector_end(); |
| 4880 | Vec1 != Vec1End; ++Vec1) |
| 4881 | for (BuiltinCandidateTypeSet::iterator |
| 4882 | Vec2 = CandidateTypes.vector_begin(), |
| 4883 | Vec2End = CandidateTypes.vector_end(); |
| 4884 | Vec2 != Vec2End; ++Vec2) { |
| 4885 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 4886 | QualType Result; |
| 4887 | if (isComparison) |
| 4888 | Result = Context.BoolTy; |
| 4889 | else { |
| 4890 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 4891 | Result = *Vec1; |
| 4892 | else |
| 4893 | Result = *Vec2; |
| 4894 | } |
| 4895 | |
| 4896 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4897 | } |
| 4898 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4899 | break; |
| 4900 | |
| 4901 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4902 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4903 | case OO_Caret: |
| 4904 | case OO_Pipe: |
| 4905 | case OO_LessLess: |
| 4906 | case OO_GreaterGreater: |
| 4907 | // C++ [over.built]p17: |
| 4908 | // |
| 4909 | // For every pair of promoted integral types L and R, there |
| 4910 | // exist candidate operator functions of the form |
| 4911 | // |
| 4912 | // LR operator%(L, R); |
| 4913 | // LR operator&(L, R); |
| 4914 | // LR operator^(L, R); |
| 4915 | // LR operator|(L, R); |
| 4916 | // L operator<<(L, R); |
| 4917 | // L operator>>(L, R); |
| 4918 | // |
| 4919 | // where LR is the result of the usual arithmetic conversions |
| 4920 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4922 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4923 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4924 | Right < LastPromotedIntegralType; ++Right) { |
| 4925 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4926 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4927 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4928 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4929 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4930 | } |
| 4931 | } |
| 4932 | break; |
| 4933 | |
| 4934 | case OO_Equal: |
| 4935 | // C++ [over.built]p20: |
| 4936 | // |
| 4937 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4938 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4939 | // empty, there exist candidate operator functions of the form |
| 4940 | // |
| 4941 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4942 | for (BuiltinCandidateTypeSet::iterator |
| 4943 | Enum = CandidateTypes.enumeration_begin(), |
| 4944 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4945 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4947 | CandidateSet); |
| 4948 | for (BuiltinCandidateTypeSet::iterator |
| 4949 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4950 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4951 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4952 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4953 | CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4954 | |
| 4955 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4956 | |
| 4957 | case OO_PlusEqual: |
| 4958 | case OO_MinusEqual: |
| 4959 | // C++ [over.built]p19: |
| 4960 | // |
| 4961 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4962 | // volatile or empty, there exist candidate operator functions |
| 4963 | // of the form |
| 4964 | // |
| 4965 | // T*VQ& operator=(T*VQ&, T*); |
| 4966 | // |
| 4967 | // C++ [over.built]p21: |
| 4968 | // |
| 4969 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4970 | // cv-unqualified object type and VQ is either volatile or |
| 4971 | // empty, there exist candidate operator functions of the form |
| 4972 | // |
| 4973 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4974 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4975 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4976 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4977 | QualType ParamTypes[2]; |
| 4978 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4979 | |
| 4980 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4981 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4982 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4983 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4984 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4985 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4986 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4987 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4988 | ParamTypes[0] |
| 4989 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4990 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4991 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4992 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4993 | } |
| 4994 | // Fall through. |
| 4995 | |
| 4996 | case OO_StarEqual: |
| 4997 | case OO_SlashEqual: |
| 4998 | // C++ [over.built]p18: |
| 4999 | // |
| 5000 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5001 | // VQ is either volatile or empty, and R is a promoted |
| 5002 | // arithmetic type, there exist candidate operator functions of |
| 5003 | // the form |
| 5004 | // |
| 5005 | // VQ L& operator=(VQ L&, R); |
| 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 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5012 | Right < LastPromotedArithmeticType; ++Right) { |
| 5013 | QualType ParamTypes[2]; |
| 5014 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 5015 | |
| 5016 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5017 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 5018 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5019 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5020 | |
| 5021 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 5022 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5023 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 5024 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 5025 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5026 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 5027 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5028 | } |
| 5029 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5030 | |
| 5031 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 5032 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 5033 | Vec1End = CandidateTypes.vector_end(); |
| 5034 | Vec1 != Vec1End; ++Vec1) |
| 5035 | for (BuiltinCandidateTypeSet::iterator |
| 5036 | Vec2 = CandidateTypes.vector_begin(), |
| 5037 | Vec2End = CandidateTypes.vector_end(); |
| 5038 | Vec2 != Vec2End; ++Vec2) { |
| 5039 | QualType ParamTypes[2]; |
| 5040 | ParamTypes[1] = *Vec2; |
| 5041 | // Add this built-in operator as a candidate (VQ is empty). |
| 5042 | ParamTypes[0] = Context.getLValueReferenceType(*Vec1); |
| 5043 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5044 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 5045 | |
| 5046 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5047 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5048 | ParamTypes[0] = Context.getVolatileType(*Vec1); |
| 5049 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 5050 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5051 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 5052 | } |
| 5053 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5054 | break; |
| 5055 | |
| 5056 | case OO_PercentEqual: |
| 5057 | case OO_LessLessEqual: |
| 5058 | case OO_GreaterGreaterEqual: |
| 5059 | case OO_AmpEqual: |
| 5060 | case OO_CaretEqual: |
| 5061 | case OO_PipeEqual: |
| 5062 | // C++ [over.built]p22: |
| 5063 | // |
| 5064 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 5065 | // is either volatile or empty, and R is a promoted integral |
| 5066 | // type, there exist candidate operator functions of the form |
| 5067 | // |
| 5068 | // VQ L& operator%=(VQ L&, R); |
| 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 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5075 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5076 | Right < LastPromotedIntegralType; ++Right) { |
| 5077 | QualType ParamTypes[2]; |
| 5078 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 5079 | |
| 5080 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5081 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5082 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 5083 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5084 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5085 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 5086 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 5087 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 5088 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 5089 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | break; |
| 5093 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5094 | case OO_Exclaim: { |
| 5095 | // C++ [over.operator]p23: |
| 5096 | // |
| 5097 | // There also exist candidate operator functions of the form |
| 5098 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5099 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5100 | // bool operator&&(bool, bool); [BELOW] |
| 5101 | // bool operator||(bool, bool); [BELOW] |
| 5102 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5103 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 5104 | /*IsAssignmentOperator=*/false, |
| 5105 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5106 | break; |
| 5107 | } |
| 5108 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5109 | case OO_AmpAmp: |
| 5110 | case OO_PipePipe: { |
| 5111 | // C++ [over.operator]p23: |
| 5112 | // |
| 5113 | // There also exist candidate operator functions of the form |
| 5114 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5115 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5116 | // bool operator&&(bool, bool); |
| 5117 | // bool operator||(bool, bool); |
| 5118 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5119 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 5120 | /*IsAssignmentOperator=*/false, |
| 5121 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5122 | break; |
| 5123 | } |
| 5124 | |
| 5125 | case OO_Subscript: |
| 5126 | // C++ [over.built]p13: |
| 5127 | // |
| 5128 | // For every cv-qualified or cv-unqualified object type T there |
| 5129 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5131 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5132 | // T& operator[](T*, ptrdiff_t); |
| 5133 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5134 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5135 | // T& operator[](ptrdiff_t, T*); |
| 5136 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 5137 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5138 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Argyrios Kyrtzidis | 42d0f2a | 2010-08-23 07:12:16 +0000 | [diff] [blame] | 5139 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5140 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5141 | |
| 5142 | // T& operator[](T*, ptrdiff_t) |
| 5143 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5144 | |
| 5145 | // T& operator[](ptrdiff_t, T*); |
| 5146 | ParamTypes[0] = ParamTypes[1]; |
| 5147 | ParamTypes[1] = *Ptr; |
| 5148 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5149 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5150 | break; |
| 5151 | |
| 5152 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5153 | // C++ [over.built]p11: |
| 5154 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5155 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5156 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5157 | // there exist candidate operator functions of the form |
| 5158 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5159 | // where CV12 is the union of CV1 and CV2. |
| 5160 | { |
| 5161 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5162 | CandidateTypes.pointer_begin(); |
| 5163 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5164 | QualType C1Ty = (*Ptr); |
| 5165 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5166 | QualifierCollector Q1; |
Argyrios Kyrtzidis | 42d0f2a | 2010-08-23 07:12:16 +0000 | [diff] [blame] | 5167 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 5168 | if (!isa<RecordType>(C1)) |
| 5169 | continue; |
| 5170 | // heuristic to reduce number of builtin candidates in the set. |
| 5171 | // Add volatile/restrict version only if there are conversions to a |
| 5172 | // volatile/restrict type. |
| 5173 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5174 | continue; |
| 5175 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5176 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5177 | for (BuiltinCandidateTypeSet::iterator |
| 5178 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 5179 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 5180 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5181 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5182 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 5183 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5184 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 5185 | break; |
| 5186 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5187 | // build CV12 T& |
| 5188 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5189 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5190 | T.isVolatileQualified()) |
| 5191 | continue; |
| 5192 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5193 | T.isRestrictQualified()) |
| 5194 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5195 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5196 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 5197 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5198 | } |
| 5199 | } |
| 5200 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5201 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5202 | |
| 5203 | case OO_Conditional: |
| 5204 | // Note that we don't consider the first argument, since it has been |
| 5205 | // contextually converted to bool long ago. The candidates below are |
| 5206 | // therefore added as binary. |
| 5207 | // |
| 5208 | // C++ [over.built]p24: |
| 5209 | // For every type T, where T is a pointer or pointer-to-member type, |
| 5210 | // there exist candidate operator functions of the form |
| 5211 | // |
| 5212 | // T operator?(bool, T, T); |
| 5213 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5214 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 5215 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 5216 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5217 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5218 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5219 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5220 | CandidateTypes.member_pointer_begin(), |
| 5221 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 5222 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5223 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5224 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5225 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5226 | } |
| 5227 | } |
| 5228 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5229 | /// \brief Add function candidates found via argument-dependent lookup |
| 5230 | /// to the set of overloading candidates. |
| 5231 | /// |
| 5232 | /// This routine performs argument-dependent name lookup based on the |
| 5233 | /// given function name (which may also be an operator name) and adds |
| 5234 | /// all of the overload candidates found by ADL to the overload |
| 5235 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5236 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5237 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5238 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5239 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5240 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5241 | OverloadCandidateSet& CandidateSet, |
| 5242 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5243 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5244 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 5245 | // FIXME: This approach for uniquing ADL results (and removing |
| 5246 | // redundant candidates from the set) relies on pointer-equality, |
| 5247 | // which means we need to key off the canonical decl. However, |
| 5248 | // always going back to the canonical decl might not get us the |
| 5249 | // right set of default arguments. What default arguments are |
| 5250 | // we supposed to consider on ADL candidates, anyway? |
| 5251 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5252 | // FIXME: Pass in the explicit template arguments? |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5253 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5255 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5256 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5257 | CandEnd = CandidateSet.end(); |
| 5258 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5259 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5260 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5261 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5262 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5263 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5264 | |
| 5265 | // For each of the ADL candidates we found, add it to the overload |
| 5266 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5267 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5268 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5269 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5270 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5271 | continue; |
| 5272 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5273 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5274 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5275 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5276 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5277 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 5278 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5279 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5280 | } |
| 5281 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5282 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 5283 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | bool |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5285 | isBetterOverloadCandidate(Sema &S, |
| 5286 | const OverloadCandidate& Cand1, |
| 5287 | const OverloadCandidate& Cand2, |
| 5288 | SourceLocation Loc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5289 | // Define viable functions to be better candidates than non-viable |
| 5290 | // functions. |
| 5291 | if (!Cand2.Viable) |
| 5292 | return Cand1.Viable; |
| 5293 | else if (!Cand1.Viable) |
| 5294 | return false; |
| 5295 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5296 | // C++ [over.match.best]p1: |
| 5297 | // |
| 5298 | // -- if F is a static member function, ICS1(F) is defined such |
| 5299 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 5300 | // any function G, and, symmetrically, ICS1(G) is neither |
| 5301 | // better nor worse than ICS1(F). |
| 5302 | unsigned StartArg = 0; |
| 5303 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 5304 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5305 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5306 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5307 | // A viable function F1 is defined to be a better function than another |
| 5308 | // 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] | 5309 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5310 | unsigned NumArgs = Cand1.Conversions.size(); |
| 5311 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 5312 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5313 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5314 | switch (CompareImplicitConversionSequences(S, |
| 5315 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5316 | Cand2.Conversions[ArgIdx])) { |
| 5317 | case ImplicitConversionSequence::Better: |
| 5318 | // Cand1 has a better conversion sequence. |
| 5319 | HasBetterConversion = true; |
| 5320 | break; |
| 5321 | |
| 5322 | case ImplicitConversionSequence::Worse: |
| 5323 | // Cand1 can't be better than Cand2. |
| 5324 | return false; |
| 5325 | |
| 5326 | case ImplicitConversionSequence::Indistinguishable: |
| 5327 | // Do nothing. |
| 5328 | break; |
| 5329 | } |
| 5330 | } |
| 5331 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5332 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5333 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5334 | if (HasBetterConversion) |
| 5335 | return true; |
| 5336 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5337 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5338 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 5339 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5340 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 5341 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5342 | |
| 5343 | // -- F1 and F2 are function template specializations, and the function |
| 5344 | // template for F1 is more specialized than the template for F2 |
| 5345 | // 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] | 5346 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 5347 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 5348 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5349 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5350 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 5351 | Cand2.Function->getPrimaryTemplate(), |
| 5352 | Loc, |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 5353 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 5354 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5355 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5356 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5357 | // -- the context is an initialization by user-defined conversion |
| 5358 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 5359 | // from the return type of F1 to the destination type (i.e., |
| 5360 | // the type of the entity being initialized) is a better |
| 5361 | // conversion sequence than the standard conversion sequence |
| 5362 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | if (Cand1.Function && Cand2.Function && |
| 5364 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5365 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5366 | switch (CompareStandardConversionSequences(S, |
| 5367 | Cand1.FinalConversion, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5368 | Cand2.FinalConversion)) { |
| 5369 | case ImplicitConversionSequence::Better: |
| 5370 | // Cand1 has a better conversion sequence. |
| 5371 | return true; |
| 5372 | |
| 5373 | case ImplicitConversionSequence::Worse: |
| 5374 | // Cand1 can't be better than Cand2. |
| 5375 | return false; |
| 5376 | |
| 5377 | case ImplicitConversionSequence::Indistinguishable: |
| 5378 | // Do nothing |
| 5379 | break; |
| 5380 | } |
| 5381 | } |
| 5382 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5383 | return false; |
| 5384 | } |
| 5385 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5386 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5387 | /// within an overload candidate set. |
| 5388 | /// |
| 5389 | /// \param CandidateSet the set of candidate functions. |
| 5390 | /// |
| 5391 | /// \param Loc the location of the function name (or operator symbol) for |
| 5392 | /// which overload resolution occurs. |
| 5393 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5394 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5395 | /// function, Best points to the candidate function found. |
| 5396 | /// |
| 5397 | /// \returns The result of overload resolution. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5398 | OverloadingResult |
| 5399 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
| 5400 | iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5401 | // Find the best viable function. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5402 | Best = end(); |
| 5403 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 5404 | if (Cand->Viable) |
| 5405 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5406 | Best = Cand; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | // If we didn't find any viable functions, abort. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5410 | if (Best == end()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5411 | return OR_No_Viable_Function; |
| 5412 | |
| 5413 | // Make sure that this function is better than every other viable |
| 5414 | // function. If not, we have an ambiguity. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5415 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5417 | Cand != Best && |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5418 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc)) { |
| 5419 | Best = end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5420 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5421 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5423 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5424 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5425 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5426 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5427 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5428 | return OR_Deleted; |
| 5429 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5430 | // C++ [basic.def.odr]p2: |
| 5431 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5432 | // when referred to from a potentially-evaluated expression. [Note: this |
| 5433 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5434 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 5435 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 5436 | if (Best->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5437 | S.MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5438 | return OR_Success; |
| 5439 | } |
| 5440 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5441 | namespace { |
| 5442 | |
| 5443 | enum OverloadCandidateKind { |
| 5444 | oc_function, |
| 5445 | oc_method, |
| 5446 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5447 | oc_function_template, |
| 5448 | oc_method_template, |
| 5449 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5450 | oc_implicit_default_constructor, |
| 5451 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5452 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5453 | }; |
| 5454 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5455 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 5456 | FunctionDecl *Fn, |
| 5457 | std::string &Description) { |
| 5458 | bool isTemplate = false; |
| 5459 | |
| 5460 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 5461 | isTemplate = true; |
| 5462 | Description = S.getTemplateArgumentBindingsText( |
| 5463 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 5464 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5465 | |
| 5466 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5467 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5468 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5469 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5470 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 5471 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
| 5474 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5475 | // This actually gets spelled 'candidate function' for now, but |
| 5476 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5477 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5478 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5479 | |
| 5480 | assert(Meth->isCopyAssignment() |
| 5481 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5482 | return oc_implicit_copy_assignment; |
| 5483 | } |
| 5484 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5485 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | } // end anonymous namespace |
| 5489 | |
| 5490 | // Notes the location of an overload candidate. |
| 5491 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5492 | std::string FnDesc; |
| 5493 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 5494 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 5495 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5498 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 5499 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 5500 | /// target types of the conversion. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5501 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 5502 | Sema &S, |
| 5503 | SourceLocation CaretLoc, |
| 5504 | const PartialDiagnostic &PDiag) const { |
| 5505 | S.Diag(CaretLoc, PDiag) |
| 5506 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5507 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5508 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 5509 | S.NoteOverloadCandidate(*I); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5510 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5513 | namespace { |
| 5514 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5515 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 5516 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 5517 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5518 | assert(Cand->Function && "for now, candidate must be a function"); |
| 5519 | FunctionDecl *Fn = Cand->Function; |
| 5520 | |
| 5521 | // There's a conversion slot for the object argument if this is a |
| 5522 | // non-constructor method. Note that 'I' corresponds the |
| 5523 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5524 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5525 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5526 | if (I == 0) |
| 5527 | isObjectArgument = true; |
| 5528 | else |
| 5529 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5530 | } |
| 5531 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5532 | std::string FnDesc; |
| 5533 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 5534 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5535 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 5536 | QualType FromTy = Conv.Bad.getFromType(); |
| 5537 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5538 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5539 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5540 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5541 | Expr *E = FromExpr->IgnoreParens(); |
| 5542 | if (isa<UnaryOperator>(E)) |
| 5543 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5544 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5545 | |
| 5546 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 5547 | << (unsigned) FnKind << FnDesc |
| 5548 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5549 | << ToTy << Name << I+1; |
| 5550 | return; |
| 5551 | } |
| 5552 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5553 | // Do some hand-waving analysis to see if the non-viability is due |
| 5554 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5555 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 5556 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 5557 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 5558 | CToTy = RT->getPointeeType(); |
| 5559 | else { |
| 5560 | // TODO: detect and diagnose the full richness of const mismatches. |
| 5561 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 5562 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 5563 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 5564 | } |
| 5565 | |
| 5566 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 5567 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 5568 | // It is dumb that we have to do this here. |
| 5569 | while (isa<ArrayType>(CFromTy)) |
| 5570 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5571 | while (isa<ArrayType>(CToTy)) |
| 5572 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5573 | |
| 5574 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 5575 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 5576 | |
| 5577 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 5578 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 5579 | << (unsigned) FnKind << FnDesc |
| 5580 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5581 | << FromTy |
| 5582 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 5583 | << (unsigned) isObjectArgument << I+1; |
| 5584 | return; |
| 5585 | } |
| 5586 | |
| 5587 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5588 | assert(CVR && "unexpected qualifiers mismatch"); |
| 5589 | |
| 5590 | if (isObjectArgument) { |
| 5591 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 5592 | << (unsigned) FnKind << FnDesc |
| 5593 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5594 | << FromTy << (CVR - 1); |
| 5595 | } else { |
| 5596 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 5597 | << (unsigned) FnKind << FnDesc |
| 5598 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5599 | << FromTy << (CVR - 1) << I+1; |
| 5600 | } |
| 5601 | return; |
| 5602 | } |
| 5603 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5604 | // Diagnose references or pointers to incomplete types differently, |
| 5605 | // since it's far from impossible that the incompleteness triggered |
| 5606 | // the failure. |
| 5607 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 5608 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 5609 | TempFromTy = PTy->getPointeeType(); |
| 5610 | if (TempFromTy->isIncompleteType()) { |
| 5611 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 5612 | << (unsigned) FnKind << FnDesc |
| 5613 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5614 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 5615 | return; |
| 5616 | } |
| 5617 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5618 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5619 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5620 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 5621 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 5622 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5623 | FromPtrTy->getPointeeType()) && |
| 5624 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 5625 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
| 5626 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
| 5627 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5628 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5629 | } |
| 5630 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 5631 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 5632 | if (const ObjCObjectPointerType *ToPtrTy |
| 5633 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 5634 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 5635 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 5636 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5637 | FromPtrTy->getPointeeType()) && |
| 5638 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5639 | BaseToDerivedConversion = 2; |
| 5640 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 5641 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 5642 | !FromTy->isIncompleteType() && |
| 5643 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 5644 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 5645 | BaseToDerivedConversion = 3; |
| 5646 | } |
| 5647 | |
| 5648 | if (BaseToDerivedConversion) { |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5649 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5650 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5651 | << (unsigned) FnKind << FnDesc |
| 5652 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5653 | << (BaseToDerivedConversion - 1) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5654 | << FromTy << ToTy << I+1; |
| 5655 | return; |
| 5656 | } |
| 5657 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5658 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5659 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 5660 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5661 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 5662 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 5666 | unsigned NumFormalArgs) { |
| 5667 | // TODO: treat calls to a missing default constructor as a special case |
| 5668 | |
| 5669 | FunctionDecl *Fn = Cand->Function; |
| 5670 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 5671 | |
| 5672 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 5673 | |
| 5674 | // at least / at most / exactly |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5675 | // FIXME: variadic templates "at most" should account for parameter packs |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5676 | unsigned mode, modeCount; |
| 5677 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5678 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 5679 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5680 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5681 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 5682 | mode = 0; // "at least" |
| 5683 | else |
| 5684 | mode = 2; // "exactly" |
| 5685 | modeCount = MinParams; |
| 5686 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5687 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 5688 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5689 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5690 | if (MinParams != FnTy->getNumArgs()) |
| 5691 | mode = 1; // "at most" |
| 5692 | else |
| 5693 | mode = 2; // "exactly" |
| 5694 | modeCount = FnTy->getNumArgs(); |
| 5695 | } |
| 5696 | |
| 5697 | std::string Description; |
| 5698 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 5699 | |
| 5700 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5701 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 5702 | << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5705 | /// Diagnose a failed template-argument deduction. |
| 5706 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 5707 | Expr **Args, unsigned NumArgs) { |
| 5708 | FunctionDecl *Fn = Cand->Function; // pattern |
| 5709 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5710 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5711 | NamedDecl *ParamD; |
| 5712 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 5713 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 5714 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5715 | switch (Cand->DeductionFailure.Result) { |
| 5716 | case Sema::TDK_Success: |
| 5717 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 5718 | |
| 5719 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5720 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 5721 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 5722 | << ParamD->getDeclName(); |
| 5723 | return; |
| 5724 | } |
| 5725 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 5726 | case Sema::TDK_Underqualified: { |
| 5727 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 5728 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 5729 | |
| 5730 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 5731 | |
| 5732 | // Param will have been canonicalized, but it should just be a |
| 5733 | // qualified version of ParamD, so move the qualifiers to that. |
| 5734 | QualifierCollector Qs(S.Context); |
| 5735 | Qs.strip(Param); |
| 5736 | QualType NonCanonParam = Qs.apply(TParam->getTypeForDecl()); |
| 5737 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 5738 | |
| 5739 | // Arg has also been canonicalized, but there's nothing we can do |
| 5740 | // about that. It also doesn't matter as much, because it won't |
| 5741 | // have any template parameters in it (because deduction isn't |
| 5742 | // done on dependent types). |
| 5743 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 5744 | |
| 5745 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 5746 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 5747 | return; |
| 5748 | } |
| 5749 | |
| 5750 | case Sema::TDK_Inconsistent: { |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5751 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5752 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5753 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5754 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5755 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5756 | which = 1; |
| 5757 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5758 | which = 2; |
| 5759 | } |
| 5760 | |
| 5761 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
| 5762 | << which << ParamD->getDeclName() |
| 5763 | << *Cand->DeductionFailure.getFirstArg() |
| 5764 | << *Cand->DeductionFailure.getSecondArg(); |
| 5765 | return; |
| 5766 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5767 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5768 | case Sema::TDK_InvalidExplicitArguments: |
| 5769 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| 5770 | if (ParamD->getDeclName()) |
| 5771 | S.Diag(Fn->getLocation(), |
| 5772 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 5773 | << ParamD->getDeclName(); |
| 5774 | else { |
| 5775 | int index = 0; |
| 5776 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 5777 | index = TTP->getIndex(); |
| 5778 | else if (NonTypeTemplateParmDecl *NTTP |
| 5779 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 5780 | index = NTTP->getIndex(); |
| 5781 | else |
| 5782 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| 5783 | S.Diag(Fn->getLocation(), |
| 5784 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 5785 | << (index + 1); |
| 5786 | } |
| 5787 | return; |
| 5788 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5789 | case Sema::TDK_TooManyArguments: |
| 5790 | case Sema::TDK_TooFewArguments: |
| 5791 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 5792 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 5793 | |
| 5794 | case Sema::TDK_InstantiationDepth: |
| 5795 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
| 5796 | return; |
| 5797 | |
| 5798 | case Sema::TDK_SubstitutionFailure: { |
| 5799 | std::string ArgString; |
| 5800 | if (TemplateArgumentList *Args |
| 5801 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 5802 | ArgString = S.getTemplateArgumentBindingsText( |
| 5803 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 5804 | *Args); |
| 5805 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 5806 | << ArgString; |
| 5807 | return; |
| 5808 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5809 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5810 | // TODO: diagnose these individually, then kill off |
| 5811 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5812 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5813 | case Sema::TDK_FailedOverloadResolution: |
| 5814 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 5815 | return; |
| 5816 | } |
| 5817 | } |
| 5818 | |
| 5819 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 5820 | /// already generated a primary error at the call site. |
| 5821 | /// |
| 5822 | /// It really does need to be a single diagnostic with its caret |
| 5823 | /// pointed at the candidate declaration. Yes, this creates some |
| 5824 | /// major challenges of technical writing. Yes, this makes pointing |
| 5825 | /// out problems with specific arguments quite awkward. It's still |
| 5826 | /// better than generating twenty screens of text for every failed |
| 5827 | /// overload. |
| 5828 | /// |
| 5829 | /// It would be great to be able to express per-candidate problems |
| 5830 | /// more richly for those diagnostic clients that cared, but we'd |
| 5831 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5832 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 5833 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5834 | FunctionDecl *Fn = Cand->Function; |
| 5835 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5836 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5837 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5838 | std::string FnDesc; |
| 5839 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5840 | |
| 5841 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5842 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5843 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5846 | // We don't really have anything else to say about viable candidates. |
| 5847 | if (Cand->Viable) { |
| 5848 | S.NoteOverloadCandidate(Fn); |
| 5849 | return; |
| 5850 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5851 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5852 | switch (Cand->FailureKind) { |
| 5853 | case ovl_fail_too_many_arguments: |
| 5854 | case ovl_fail_too_few_arguments: |
| 5855 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5856 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5857 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5858 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 5859 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5860 | case ovl_fail_trivial_conversion: |
| 5861 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5862 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5863 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5864 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5865 | case ovl_fail_bad_conversion: { |
| 5866 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 5867 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5868 | if (Cand->Conversions[I].isBad()) |
| 5869 | return DiagnoseBadConversion(S, Cand, I); |
| 5870 | |
| 5871 | // FIXME: this currently happens when we're called from SemaInit |
| 5872 | // when user-conversion overload fails. Figure out how to handle |
| 5873 | // those conditions and diagnose them well. |
| 5874 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5875 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5876 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
| 5879 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 5880 | // Desugar the type of the surrogate down to a function type, |
| 5881 | // retaining as many typedefs as possible while still showing |
| 5882 | // the function type (and, therefore, its parameter types). |
| 5883 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 5884 | bool isLValueReference = false; |
| 5885 | bool isRValueReference = false; |
| 5886 | bool isPointer = false; |
| 5887 | if (const LValueReferenceType *FnTypeRef = |
| 5888 | FnType->getAs<LValueReferenceType>()) { |
| 5889 | FnType = FnTypeRef->getPointeeType(); |
| 5890 | isLValueReference = true; |
| 5891 | } else if (const RValueReferenceType *FnTypeRef = |
| 5892 | FnType->getAs<RValueReferenceType>()) { |
| 5893 | FnType = FnTypeRef->getPointeeType(); |
| 5894 | isRValueReference = true; |
| 5895 | } |
| 5896 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5897 | FnType = FnTypePtr->getPointeeType(); |
| 5898 | isPointer = true; |
| 5899 | } |
| 5900 | // Desugar down to a function type. |
| 5901 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5902 | // Reconstruct the pointer/reference as appropriate. |
| 5903 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5904 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5905 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5906 | |
| 5907 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5908 | << FnType; |
| 5909 | } |
| 5910 | |
| 5911 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5912 | const char *Opc, |
| 5913 | SourceLocation OpLoc, |
| 5914 | OverloadCandidate *Cand) { |
| 5915 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5916 | std::string TypeStr("operator"); |
| 5917 | TypeStr += Opc; |
| 5918 | TypeStr += "("; |
| 5919 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5920 | if (Cand->Conversions.size() == 1) { |
| 5921 | TypeStr += ")"; |
| 5922 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5923 | } else { |
| 5924 | TypeStr += ", "; |
| 5925 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5926 | TypeStr += ")"; |
| 5927 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5928 | } |
| 5929 | } |
| 5930 | |
| 5931 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5932 | OverloadCandidate *Cand) { |
| 5933 | unsigned NoOperands = Cand->Conversions.size(); |
| 5934 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5935 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5936 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5937 | if (!ICS.isAmbiguous()) continue; |
| 5938 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5939 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5940 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5941 | } |
| 5942 | } |
| 5943 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5944 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5945 | if (Cand->Function) |
| 5946 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5947 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5948 | return Cand->Surrogate->getLocation(); |
| 5949 | return SourceLocation(); |
| 5950 | } |
| 5951 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5952 | struct CompareOverloadCandidatesForDisplay { |
| 5953 | Sema &S; |
| 5954 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5955 | |
| 5956 | bool operator()(const OverloadCandidate *L, |
| 5957 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5958 | // Fast-path this check. |
| 5959 | if (L == R) return false; |
| 5960 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5961 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5962 | if (L->Viable) { |
| 5963 | if (!R->Viable) return true; |
| 5964 | |
| 5965 | // TODO: introduce a tri-valued comparison for overload |
| 5966 | // candidates. Would be more worthwhile if we had a sort |
| 5967 | // that could exploit it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5968 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 5969 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5970 | } else if (R->Viable) |
| 5971 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5972 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5973 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5974 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5975 | // Criteria by which we can sort non-viable candidates: |
| 5976 | if (!L->Viable) { |
| 5977 | // 1. Arity mismatches come after other candidates. |
| 5978 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5979 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5980 | return false; |
| 5981 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5982 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5983 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5984 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5985 | // 2. Bad conversions come first and are ordered by the number |
| 5986 | // of bad conversions and quality of good conversions. |
| 5987 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5988 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5989 | return true; |
| 5990 | |
| 5991 | // If there's any ordering between the defined conversions... |
| 5992 | // FIXME: this might not be transitive. |
| 5993 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5994 | |
| 5995 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5996 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5997 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5998 | switch (CompareImplicitConversionSequences(S, |
| 5999 | L->Conversions[I], |
| 6000 | R->Conversions[I])) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6001 | case ImplicitConversionSequence::Better: |
| 6002 | leftBetter++; |
| 6003 | break; |
| 6004 | |
| 6005 | case ImplicitConversionSequence::Worse: |
| 6006 | leftBetter--; |
| 6007 | break; |
| 6008 | |
| 6009 | case ImplicitConversionSequence::Indistinguishable: |
| 6010 | break; |
| 6011 | } |
| 6012 | } |
| 6013 | if (leftBetter > 0) return true; |
| 6014 | if (leftBetter < 0) return false; |
| 6015 | |
| 6016 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 6017 | return false; |
| 6018 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6019 | // TODO: others? |
| 6020 | } |
| 6021 | |
| 6022 | // Sort everything else by location. |
| 6023 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 6024 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 6025 | |
| 6026 | // Put candidates without locations (e.g. builtins) at the end. |
| 6027 | if (LLoc.isInvalid()) return false; |
| 6028 | if (RLoc.isInvalid()) return true; |
| 6029 | |
| 6030 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6031 | } |
| 6032 | }; |
| 6033 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6034 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 6035 | /// computes up to the first |
| 6036 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 6037 | Expr **Args, unsigned NumArgs) { |
| 6038 | assert(!Cand->Viable); |
| 6039 | |
| 6040 | // Don't do anything on failures other than bad conversion. |
| 6041 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 6042 | |
| 6043 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6044 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6045 | unsigned ConvCount = Cand->Conversions.size(); |
| 6046 | while (true) { |
| 6047 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 6048 | ConvIdx++; |
| 6049 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 6050 | break; |
| 6051 | } |
| 6052 | |
| 6053 | if (ConvIdx == ConvCount) |
| 6054 | return; |
| 6055 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6056 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 6057 | "remaining conversion is initialized?"); |
| 6058 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 6059 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6060 | // operation somehow. |
| 6061 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6062 | |
| 6063 | const FunctionProtoType* Proto; |
| 6064 | unsigned ArgIdx = ConvIdx; |
| 6065 | |
| 6066 | if (Cand->IsSurrogate) { |
| 6067 | QualType ConvType |
| 6068 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 6069 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6070 | ConvType = ConvPtrType->getPointeeType(); |
| 6071 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 6072 | ArgIdx--; |
| 6073 | } else if (Cand->Function) { |
| 6074 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 6075 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 6076 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 6077 | ArgIdx--; |
| 6078 | } else { |
| 6079 | // Builtin binary operator with a bad first conversion. |
| 6080 | assert(ConvCount <= 3); |
| 6081 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 6082 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6083 | = TryCopyInitialization(S, Args[ConvIdx], |
| 6084 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 6085 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6086 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6087 | return; |
| 6088 | } |
| 6089 | |
| 6090 | // Fill in the rest of the conversions. |
| 6091 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6092 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 6093 | if (ArgIdx < NumArgsInProto) |
| 6094 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6095 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 6096 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 6097 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6098 | else |
| 6099 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 6100 | } |
| 6101 | } |
| 6102 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6103 | } // end anonymous namespace |
| 6104 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6105 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 6106 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6107 | /// set. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6108 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 6109 | OverloadCandidateDisplayKind OCD, |
| 6110 | Expr **Args, unsigned NumArgs, |
| 6111 | const char *Opc, |
| 6112 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6113 | // Sort the candidates by viability and position. Sorting directly would |
| 6114 | // be prohibitive, so we make a set of pointers and sort those. |
| 6115 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6116 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 6117 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6118 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6119 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6120 | else if (OCD == OCD_AllCandidates) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6121 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6122 | if (Cand->Function || Cand->IsSurrogate) |
| 6123 | Cands.push_back(Cand); |
| 6124 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 6125 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6126 | } |
| 6127 | } |
| 6128 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6129 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6130 | CompareOverloadCandidatesForDisplay(S)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6131 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6132 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6133 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6134 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6135 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6136 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6137 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 6138 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 6139 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6140 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 6141 | // the user with. FIXME: This limit should depend on details of the |
| 6142 | // candidate list. |
| 6143 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 6144 | break; |
| 6145 | } |
| 6146 | ++CandsShown; |
| 6147 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6148 | if (Cand->Function) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6149 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6150 | else if (Cand->IsSurrogate) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6151 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6152 | else { |
| 6153 | assert(Cand->Viable && |
| 6154 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6155 | // Generally we only see ambiguities including viable builtin |
| 6156 | // operators if overload resolution got screwed up by an |
| 6157 | // ambiguous user-defined conversion. |
| 6158 | // |
| 6159 | // FIXME: It's quite possible for different conversions to see |
| 6160 | // different ambiguities, though. |
| 6161 | if (!ReportedAmbiguousConversions) { |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6162 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6163 | ReportedAmbiguousConversions = true; |
| 6164 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6165 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6166 | // If this is a viable builtin, print it. |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6167 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6168 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6169 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6170 | |
| 6171 | if (I != E) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6172 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6175 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6176 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6177 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6178 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6179 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6182 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 6183 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 6184 | /// expression with overloaded function type and @p ToType is the type |
| 6185 | /// we're trying to resolve to. For example: |
| 6186 | /// |
| 6187 | /// @code |
| 6188 | /// int f(double); |
| 6189 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6190 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6191 | /// int (*pfd)(double) = f; // selects f(double) |
| 6192 | /// @endcode |
| 6193 | /// |
| 6194 | /// This routine returns the resulting FunctionDecl if it could be |
| 6195 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 6196 | /// routine will emit diagnostics if there is an error. |
| 6197 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6198 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6199 | bool Complain, |
| 6200 | DeclAccessPair &FoundResult) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6201 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6202 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6203 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6204 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6205 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 6206 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6207 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6208 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6209 | FunctionType = MemTypePtr->getPointeeType(); |
| 6210 | IsMember = true; |
| 6211 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6213 | // C++ [over.over]p1: |
| 6214 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6215 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6216 | // C++ [over.over]p1: |
| 6217 | // [...] The overloaded function name can be preceded by the & |
| 6218 | // operator. |
John McCall | c988fab | 2010-08-24 23:26:21 +0000 | [diff] [blame^] | 6219 | // However, remember whether the expression has member-pointer form: |
| 6220 | // C++ [expr.unary.op]p4: |
| 6221 | // A pointer to member is only formed when an explicit & is used |
| 6222 | // and its operand is a qualified-id not enclosed in |
| 6223 | // parentheses. |
| 6224 | bool HasFormOfMemberPointer = false; |
| 6225 | OverloadExpr *OvlExpr; |
| 6226 | { |
| 6227 | Expr *Tmp = From->IgnoreParens(); |
| 6228 | if (isa<UnaryOperator>(Tmp)) { |
| 6229 | Tmp = cast<UnaryOperator>(Tmp)->getSubExpr(); |
| 6230 | OvlExpr = cast<OverloadExpr>(Tmp->IgnoreParens()); |
| 6231 | HasFormOfMemberPointer = (Tmp == OvlExpr && OvlExpr->getQualifier()); |
| 6232 | } else { |
| 6233 | OvlExpr = cast<OverloadExpr>(Tmp); |
| 6234 | } |
| 6235 | } |
| 6236 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6237 | // We expect a pointer or reference to function, or a function pointer. |
| 6238 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 6239 | if (!FunctionType->isFunctionType()) { |
| 6240 | if (Complain) |
| 6241 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 6242 | << OvlExpr->getName() << ToType; |
| 6243 | |
| 6244 | return 0; |
| 6245 | } |
| 6246 | |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 6247 | // 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^] | 6248 | // member, don't try to convert it to a pointer-to-member type. |
| 6249 | if (IsMember && !HasFormOfMemberPointer) { |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 6250 | if (!Complain) return 0; |
| 6251 | |
| 6252 | // TODO: Should we condition this on whether any functions might |
| 6253 | // have matched, or is it more appropriate to do that in callers? |
| 6254 | // TODO: a fixit wouldn't hurt. |
| 6255 | Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 6256 | << ToType << OvlExpr->getSourceRange(); |
| 6257 | return 0; |
| 6258 | } |
| 6259 | |
| 6260 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 6261 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 6262 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 6263 | ExplicitTemplateArgs = &ETABuffer; |
| 6264 | } |
| 6265 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6266 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6267 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6268 | // Look through all of the overloaded functions, searching for one |
| 6269 | // whose type matches exactly. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6270 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6271 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 6272 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6273 | bool FoundNonTemplateFunction = false; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6274 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6275 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6276 | // Look through any using declarations to find the underlying function. |
| 6277 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 6278 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6279 | // C++ [over.over]p3: |
| 6280 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 6281 | // targets of type "pointer-to-function" or "reference-to-function." |
| 6282 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6283 | // type "pointer-to-member-function." |
| 6284 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6285 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6286 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6287 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6288 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6289 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6290 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6291 | // static when converting to member pointer. |
| 6292 | if (Method->isStatic() == IsMember) |
| 6293 | continue; |
| 6294 | } else if (IsMember) |
| 6295 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6296 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6297 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6298 | // If the name is a function template, template argument deduction is |
| 6299 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6300 | // resulting template argument list is used to generate a single |
| 6301 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6302 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6303 | // 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] | 6304 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6305 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6306 | if (TemplateDeductionResult Result |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6307 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6308 | FunctionType, Specialization, Info)) { |
| 6309 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6310 | (void)Result; |
| 6311 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6312 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 6313 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6314 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6315 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6316 | Matches.push_back(std::make_pair(I.getPair(), |
| 6317 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6318 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6319 | |
| 6320 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6322 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6323 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6324 | // Skip non-static functions when converting to pointer, and static |
| 6325 | // when converting to member pointer. |
| 6326 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6327 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6328 | |
| 6329 | // If we have explicit template arguments, skip non-templates. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6330 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6331 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6332 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6333 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6334 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6335 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 6336 | QualType ResultTy; |
| 6337 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 6338 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 6339 | ResultTy)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6340 | Matches.push_back(std::make_pair(I.getPair(), |
| 6341 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6342 | FoundNonTemplateFunction = true; |
| 6343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6344 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6347 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6348 | if (Matches.empty()) { |
| 6349 | if (Complain) { |
| 6350 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 6351 | << OvlExpr->getName() << FunctionType; |
| 6352 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6353 | E = OvlExpr->decls_end(); |
| 6354 | I != E; ++I) |
| 6355 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 6356 | NoteOverloadCandidate(F); |
| 6357 | } |
| 6358 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6359 | return 0; |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6360 | } else if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6361 | FunctionDecl *Result = Matches[0].second; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6362 | FoundResult = Matches[0].first; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6363 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6364 | if (Complain) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6365 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6366 | return Result; |
| 6367 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6368 | |
| 6369 | // C++ [over.over]p4: |
| 6370 | // If more than one function is selected, [...] |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6371 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6372 | // [...] and any given function template specialization F1 is |
| 6373 | // eliminated if the set contains a second function template |
| 6374 | // specialization whose function template is more specialized |
| 6375 | // than the function template of F1 according to the partial |
| 6376 | // ordering rules of 14.5.5.2. |
| 6377 | |
| 6378 | // The algorithm specified above is quadratic. We instead use a |
| 6379 | // two-pass algorithm (similar to the one used to identify the |
| 6380 | // best viable function in an overload set) that identifies the |
| 6381 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6382 | |
| 6383 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 6384 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6385 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6386 | |
| 6387 | UnresolvedSetIterator Result = |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6388 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6389 | TPOC_Other, From->getLocStart(), |
| 6390 | PDiag(), |
| 6391 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6392 | << Matches[0].second->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6393 | PDiag(diag::note_ovl_candidate) |
| 6394 | << (unsigned) oc_function_template); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6395 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6396 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6397 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6398 | if (Complain) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6399 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6400 | DiagnoseUseOfDecl(FoundResult, OvlExpr->getNameLoc()); |
| 6401 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6402 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6404 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6405 | // [...] any function template specializations in the set are |
| 6406 | // eliminated if the set also contains a non-template function, [...] |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6407 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6408 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6409 | ++I; |
| 6410 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6411 | Matches[I] = Matches[--N]; |
| 6412 | Matches.set_size(N); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6413 | } |
| 6414 | } |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6415 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6416 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6417 | // selected function. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6418 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6419 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6420 | FoundResult = Matches[0].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6421 | if (Complain) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6422 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6423 | DiagnoseUseOfDecl(Matches[0].first, OvlExpr->getNameLoc()); |
| 6424 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6425 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6427 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6428 | // FIXME: We should probably return the same thing that BestViableFunction |
| 6429 | // returns (even if we issue the diagnostics here). |
| 6430 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6431 | << Matches[0].second->getDeclName(); |
| 6432 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6433 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6434 | return 0; |
| 6435 | } |
| 6436 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6437 | /// \brief Given an expression that refers to an overloaded function, try to |
| 6438 | /// resolve that overloaded function expression down to a single function. |
| 6439 | /// |
| 6440 | /// This routine can only resolve template-ids that refer to a single function |
| 6441 | /// template, where that template-id refers to a single template whose template |
| 6442 | /// arguments are either provided by the template-id or have defaults, |
| 6443 | /// as described in C++0x [temp.arg.explicit]p3. |
| 6444 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 6445 | // C++ [over.over]p1: |
| 6446 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6447 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6448 | // C++ [over.over]p1: |
| 6449 | // [...] The overloaded function name can be preceded by the & |
| 6450 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6451 | |
| 6452 | if (From->getType() != Context.OverloadTy) |
| 6453 | return 0; |
| 6454 | |
| 6455 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6456 | |
| 6457 | // If we didn't actually find any template-ids, we're done. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6458 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6459 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6460 | |
| 6461 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 6462 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6463 | |
| 6464 | // Look through all of the overloaded functions, searching for one |
| 6465 | // whose type matches exactly. |
| 6466 | FunctionDecl *Matched = 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6467 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6468 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6469 | // C++0x [temp.arg.explicit]p3: |
| 6470 | // [...] In contexts where deduction is done and fails, or in contexts |
| 6471 | // where deduction is not done, if a template argument list is |
| 6472 | // specified and it, along with any default template arguments, |
| 6473 | // identifies a single function template specialization, then the |
| 6474 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 6475 | FunctionTemplateDecl *FunctionTemplate |
| 6476 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6477 | |
| 6478 | // C++ [over.over]p2: |
| 6479 | // If the name is a function template, template argument deduction is |
| 6480 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6481 | // resulting template argument list is used to generate a single |
| 6482 | // function template specialization, which is added to the set of |
| 6483 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6484 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6485 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6486 | if (TemplateDeductionResult Result |
| 6487 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 6488 | Specialization, Info)) { |
| 6489 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6490 | (void)Result; |
| 6491 | continue; |
| 6492 | } |
| 6493 | |
| 6494 | // Multiple matches; we can't resolve to a single declaration. |
| 6495 | if (Matched) |
| 6496 | return 0; |
| 6497 | |
| 6498 | Matched = Specialization; |
| 6499 | } |
| 6500 | |
| 6501 | return Matched; |
| 6502 | } |
| 6503 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6504 | /// \brief Add a single candidate to the overload set. |
| 6505 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6506 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6507 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6508 | Expr **Args, unsigned NumArgs, |
| 6509 | OverloadCandidateSet &CandidateSet, |
| 6510 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6511 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6512 | if (isa<UsingShadowDecl>(Callee)) |
| 6513 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 6514 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6515 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6516 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6517 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6518 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6519 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | if (FunctionTemplateDecl *FuncTemplate |
| 6523 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6524 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 6525 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6526 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6527 | return; |
| 6528 | } |
| 6529 | |
| 6530 | assert(false && "unhandled case in overloaded call candidate"); |
| 6531 | |
| 6532 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6533 | } |
| 6534 | |
| 6535 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 6536 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6537 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6538 | Expr **Args, unsigned NumArgs, |
| 6539 | OverloadCandidateSet &CandidateSet, |
| 6540 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6541 | |
| 6542 | #ifndef NDEBUG |
| 6543 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 6544 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6545 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6546 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 6547 | // and let Y be the lookup set produced by argument dependent |
| 6548 | // lookup (defined as follows). If X contains |
| 6549 | // |
| 6550 | // -- a declaration of a class member, or |
| 6551 | // |
| 6552 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6553 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6554 | // |
| 6555 | // -- a declaration that is neither a function or a function |
| 6556 | // template |
| 6557 | // |
| 6558 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6559 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6560 | if (ULE->requiresADL()) { |
| 6561 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6562 | E = ULE->decls_end(); I != E; ++I) { |
| 6563 | assert(!(*I)->getDeclContext()->isRecord()); |
| 6564 | assert(isa<UsingShadowDecl>(*I) || |
| 6565 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 6566 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6567 | } |
| 6568 | } |
| 6569 | #endif |
| 6570 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6571 | // It would be nice to avoid this copy. |
| 6572 | TemplateArgumentListInfo TABuffer; |
| 6573 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6574 | if (ULE->hasExplicitTemplateArgs()) { |
| 6575 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6576 | ExplicitTemplateArgs = &TABuffer; |
| 6577 | } |
| 6578 | |
| 6579 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6580 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6581 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6582 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6583 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6584 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6585 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6586 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 6587 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6588 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6589 | CandidateSet, |
| 6590 | PartialOverloading); |
| 6591 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6592 | |
| 6593 | /// Attempts to recover from a call where no functions were found. |
| 6594 | /// |
| 6595 | /// Returns true if new candidates were found. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6596 | static ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6597 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6598 | UnresolvedLookupExpr *ULE, |
| 6599 | SourceLocation LParenLoc, |
| 6600 | Expr **Args, unsigned NumArgs, |
| 6601 | SourceLocation *CommaLocs, |
| 6602 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6603 | |
| 6604 | CXXScopeSpec SS; |
| 6605 | if (ULE->getQualifier()) { |
| 6606 | SS.setScopeRep(ULE->getQualifier()); |
| 6607 | SS.setRange(ULE->getQualifierRange()); |
| 6608 | } |
| 6609 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6610 | TemplateArgumentListInfo TABuffer; |
| 6611 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6612 | if (ULE->hasExplicitTemplateArgs()) { |
| 6613 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6614 | ExplicitTemplateArgs = &TABuffer; |
| 6615 | } |
| 6616 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6617 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 6618 | Sema::LookupOrdinaryName); |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 6619 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6620 | return SemaRef.ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6621 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6622 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 6623 | |
| 6624 | // Build an implicit member call if appropriate. Just drop the |
| 6625 | // casts and such from the call, we don't really care. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6626 | ExprResult NewFn = SemaRef.ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6627 | if ((*R.begin())->isCXXClassMember()) |
| 6628 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 6629 | else if (ExplicitTemplateArgs) |
| 6630 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 6631 | else |
| 6632 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 6633 | |
| 6634 | if (NewFn.isInvalid()) |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6635 | return SemaRef.ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6636 | |
| 6637 | // This shouldn't cause an infinite loop because we're giving it |
| 6638 | // an expression with non-empty lookup results, which should never |
| 6639 | // end up here. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6640 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6641 | Sema::MultiExprArg(SemaRef, Args, NumArgs), |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6642 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6643 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 6644 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6645 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6646 | /// (which eventually refers to the declaration Func) and the call |
| 6647 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 6648 | /// to a specific function. If overload resolution succeeds, returns |
| 6649 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 6650 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6651 | /// arguments and Fn, and returns NULL. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6652 | ExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6653 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6654 | SourceLocation LParenLoc, |
| 6655 | Expr **Args, unsigned NumArgs, |
| 6656 | SourceLocation *CommaLocs, |
| 6657 | SourceLocation RParenLoc) { |
| 6658 | #ifndef NDEBUG |
| 6659 | if (ULE->requiresADL()) { |
| 6660 | // To do ADL, we must have found an unqualified name. |
| 6661 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 6662 | |
| 6663 | // We don't perform ADL for implicit declarations of builtins. |
| 6664 | // Verify that this was correctly set up. |
| 6665 | FunctionDecl *F; |
| 6666 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 6667 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 6668 | F->getBuiltinID() && F->isImplicit()) |
| 6669 | assert(0 && "performing ADL for builtin"); |
| 6670 | |
| 6671 | // We don't perform ADL in C. |
| 6672 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 6673 | } |
| 6674 | #endif |
| 6675 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6676 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 6677 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6678 | // Add the functions denoted by the callee to the set of candidate |
| 6679 | // functions, including those from argument-dependent lookup. |
| 6680 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6681 | |
| 6682 | // If we found nothing, try to recover. |
| 6683 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 6684 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6685 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6686 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6687 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6688 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6689 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6690 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6691 | case OR_Success: { |
| 6692 | FunctionDecl *FDecl = Best->Function; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6693 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6694 | DiagnoseUseOfDecl(Best->FoundDecl, ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6695 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6696 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 6697 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6698 | |
| 6699 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 6700 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6701 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6702 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6703 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6704 | break; |
| 6705 | |
| 6706 | case OR_Ambiguous: |
| 6707 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6708 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6709 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6710 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6711 | |
| 6712 | case OR_Deleted: |
| 6713 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 6714 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6715 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6716 | << Fn->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6717 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6718 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6721 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6722 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6723 | } |
| 6724 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6725 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6726 | return Functions.size() > 1 || |
| 6727 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 6728 | } |
| 6729 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6730 | /// \brief Create a unary operation that may resolve to an overloaded |
| 6731 | /// operator. |
| 6732 | /// |
| 6733 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 6734 | /// |
| 6735 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 6736 | /// operator. |
| 6737 | /// |
| 6738 | /// \param Functions The set of non-member functions that will be |
| 6739 | /// considered by overload resolution. The caller needs to build this |
| 6740 | /// set based on the context using, e.g., |
| 6741 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6742 | /// set should not contain any member functions; those will be added |
| 6743 | /// by CreateOverloadedUnaryOp(). |
| 6744 | /// |
| 6745 | /// \param input The input argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6746 | ExprResult |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6747 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 6748 | const UnresolvedSetImpl &Fns, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6749 | Expr *Input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6750 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6751 | |
| 6752 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 6753 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 6754 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6755 | // TODO: provide better source location info. |
| 6756 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6757 | |
| 6758 | Expr *Args[2] = { Input, 0 }; |
| 6759 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6760 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6761 | // For post-increment and post-decrement, add the implicit '0' as |
| 6762 | // the second argument, so that we know this is a post-increment or |
| 6763 | // post-decrement. |
| 6764 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 6765 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6766 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6767 | SourceLocation()); |
| 6768 | NumArgs = 2; |
| 6769 | } |
| 6770 | |
| 6771 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 6772 | if (Fns.empty()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6773 | return Owned(new (Context) UnaryOperator(Input, |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 6774 | Opc, |
| 6775 | Context.DependentTy, |
| 6776 | OpLoc)); |
| 6777 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6778 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6779 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6780 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6781 | 0, SourceRange(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6782 | /*ADL*/ true, IsOverloaded(Fns), |
| 6783 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6784 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 6785 | &Args[0], NumArgs, |
| 6786 | Context.DependentTy, |
| 6787 | OpLoc)); |
| 6788 | } |
| 6789 | |
| 6790 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6791 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6792 | |
| 6793 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6794 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6795 | |
| 6796 | // Add operator candidates that are member functions. |
| 6797 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 6798 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6799 | // Add candidates from ADL. |
| 6800 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 6801 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6802 | /*ExplicitTemplateArgs*/ 0, |
| 6803 | CandidateSet); |
| 6804 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6805 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6806 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6807 | |
| 6808 | // Perform overload resolution. |
| 6809 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6810 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6811 | case OR_Success: { |
| 6812 | // We found a built-in operator or an overloaded operator. |
| 6813 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6814 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6815 | if (FnDecl) { |
| 6816 | // We matched an overloaded operator. Build a call to that |
| 6817 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6818 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6819 | // Convert the arguments. |
| 6820 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6821 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6822 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6823 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 6824 | Best->FoundDecl, Method)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6825 | return ExprError(); |
| 6826 | } else { |
| 6827 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6828 | ExprResult InputInit |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6829 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6830 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6831 | SourceLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6832 | Input); |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6833 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6834 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6835 | Input = InputInit.take(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6838 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6839 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6840 | // Determine the result type |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 6841 | QualType ResultTy = FnDecl->getCallResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6842 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6843 | // Build the actual expression node. |
| 6844 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6845 | SourceLocation()); |
| 6846 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6847 | |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6848 | Args[0] = Input; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6849 | CallExpr *TheCall = |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6850 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6851 | Args, NumArgs, ResultTy, OpLoc); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6852 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6853 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6854 | FnDecl)) |
| 6855 | return ExprError(); |
| 6856 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6857 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6858 | } else { |
| 6859 | // We matched a built-in operator. Convert the arguments, then |
| 6860 | // break out so that we will build the appropriate built-in |
| 6861 | // operator node. |
| 6862 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6863 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6864 | return ExprError(); |
| 6865 | |
| 6866 | break; |
| 6867 | } |
| 6868 | } |
| 6869 | |
| 6870 | case OR_No_Viable_Function: |
| 6871 | // No viable function; fall through to handling this as a |
| 6872 | // built-in operator, which will produce an error message for us. |
| 6873 | break; |
| 6874 | |
| 6875 | case OR_Ambiguous: |
| 6876 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6877 | << UnaryOperator::getOpcodeStr(Opc) |
| 6878 | << Input->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6879 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 6880 | Args, NumArgs, |
| 6881 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6882 | return ExprError(); |
| 6883 | |
| 6884 | case OR_Deleted: |
| 6885 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6886 | << Best->Function->isDeleted() |
| 6887 | << UnaryOperator::getOpcodeStr(Opc) |
| 6888 | << Input->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6889 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6890 | return ExprError(); |
| 6891 | } |
| 6892 | |
| 6893 | // Either we found no viable overloaded operator or we matched a |
| 6894 | // built-in operator. In either case, fall through to trying to |
| 6895 | // build a built-in operation. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6896 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6897 | } |
| 6898 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6899 | /// \brief Create a binary operation that may resolve to an overloaded |
| 6900 | /// operator. |
| 6901 | /// |
| 6902 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 6903 | /// |
| 6904 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 6905 | /// operator. |
| 6906 | /// |
| 6907 | /// \param Functions The set of non-member functions that will be |
| 6908 | /// considered by overload resolution. The caller needs to build this |
| 6909 | /// set based on the context using, e.g., |
| 6910 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6911 | /// set should not contain any member functions; those will be added |
| 6912 | /// by CreateOverloadedBinOp(). |
| 6913 | /// |
| 6914 | /// \param LHS Left-hand argument. |
| 6915 | /// \param RHS Right-hand argument. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6916 | ExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6917 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6918 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6919 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6920 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6921 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6922 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6923 | |
| 6924 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 6925 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 6926 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6927 | |
| 6928 | // If either side is type-dependent, create an appropriate dependent |
| 6929 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6930 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6931 | if (Fns.empty()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6932 | // If there are no functions to store, just build a dependent |
| 6933 | // BinaryOperator or CompoundAssignment. |
| 6934 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 6935 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6936 | Context.DependentTy, OpLoc)); |
| 6937 | |
| 6938 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6939 | Context.DependentTy, |
| 6940 | Context.DependentTy, |
| 6941 | Context.DependentTy, |
| 6942 | OpLoc)); |
| 6943 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6944 | |
| 6945 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6946 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6947 | // TODO: provide better source location info in DNLoc component. |
| 6948 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6949 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6950 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6951 | 0, SourceRange(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6952 | /*ADL*/ true, IsOverloaded(Fns), |
| 6953 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6954 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6955 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6956 | Context.DependentTy, |
| 6957 | OpLoc)); |
| 6958 | } |
| 6959 | |
| 6960 | // If this is the .* operator, which is not overloadable, just |
| 6961 | // create a built-in binary operator. |
| 6962 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6963 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6964 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6965 | // If this is the assignment operator, we only perform overload resolution |
| 6966 | // if the left-hand side is a class or enumeration type. This is actually |
| 6967 | // a hack. The standard requires that we do overload resolution between the |
| 6968 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6969 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6970 | // Note that we go the traditional code path for compound assignment forms. |
| 6971 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6972 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6974 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6975 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6976 | |
| 6977 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6978 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6979 | |
| 6980 | // Add operator candidates that are member functions. |
| 6981 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6982 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6983 | // Add candidates from ADL. |
| 6984 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6985 | Args, 2, |
| 6986 | /*ExplicitTemplateArgs*/ 0, |
| 6987 | CandidateSet); |
| 6988 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6989 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6990 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6991 | |
| 6992 | // Perform overload resolution. |
| 6993 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6994 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6995 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6996 | // We found a built-in operator or an overloaded operator. |
| 6997 | FunctionDecl *FnDecl = Best->Function; |
| 6998 | |
| 6999 | if (FnDecl) { |
| 7000 | // We matched an overloaded operator. Build a call to that |
| 7001 | // operator. |
| 7002 | |
| 7003 | // Convert the arguments. |
| 7004 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 7005 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7006 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 7007 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7008 | ExprResult Arg1 |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7009 | = PerformCopyInitialization( |
| 7010 | InitializedEntity::InitializeParameter( |
| 7011 | FnDecl->getParamDecl(0)), |
| 7012 | SourceLocation(), |
| 7013 | Owned(Args[1])); |
| 7014 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7015 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7016 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7017 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7018 | Best->FoundDecl, Method)) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7019 | return ExprError(); |
| 7020 | |
| 7021 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7022 | } else { |
| 7023 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7024 | ExprResult Arg0 |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7025 | = PerformCopyInitialization( |
| 7026 | InitializedEntity::InitializeParameter( |
| 7027 | FnDecl->getParamDecl(0)), |
| 7028 | SourceLocation(), |
| 7029 | Owned(Args[0])); |
| 7030 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7031 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7032 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7033 | ExprResult Arg1 |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 7034 | = PerformCopyInitialization( |
| 7035 | InitializedEntity::InitializeParameter( |
| 7036 | FnDecl->getParamDecl(1)), |
| 7037 | SourceLocation(), |
| 7038 | Owned(Args[1])); |
| 7039 | if (Arg1.isInvalid()) |
| 7040 | return ExprError(); |
| 7041 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 7042 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7045 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 7046 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7047 | // Determine the result type |
| 7048 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7049 | = FnDecl->getType()->getAs<FunctionType>() |
| 7050 | ->getCallResultType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7051 | |
| 7052 | // Build the actual expression node. |
| 7053 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 7054 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7055 | UsualUnaryConversions(FnExpr); |
| 7056 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7057 | CXXOperatorCallExpr *TheCall = |
| 7058 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 7059 | Args, 2, ResultTy, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7060 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7061 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7062 | FnDecl)) |
| 7063 | return ExprError(); |
| 7064 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7065 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7066 | } else { |
| 7067 | // We matched a built-in operator. Convert the arguments, then |
| 7068 | // break out so that we will build the appropriate built-in |
| 7069 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7070 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7071 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7072 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7073 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7074 | return ExprError(); |
| 7075 | |
| 7076 | break; |
| 7077 | } |
| 7078 | } |
| 7079 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7080 | case OR_No_Viable_Function: { |
| 7081 | // C++ [over.match.oper]p9: |
| 7082 | // If the operator is the operator , [...] and there are no |
| 7083 | // viable functions, then the operator is assumed to be the |
| 7084 | // built-in operator and interpreted according to clause 5. |
| 7085 | if (Opc == BinaryOperator::Comma) |
| 7086 | break; |
| 7087 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 7088 | // For class as left operand for assignment or compound assigment operator |
| 7089 | // do not fall through to handling in built-in, but report that no overloaded |
| 7090 | // assignment operator found |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7091 | ExprResult Result = ExprError(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7092 | if (Args[0]->getType()->isRecordType() && |
| 7093 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 7094 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 7095 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7096 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7097 | } else { |
| 7098 | // No viable function; try to create a built-in operation, which will |
| 7099 | // produce an error. Then, show the non-viable candidates. |
| 7100 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 7101 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7102 | assert(Result.isInvalid() && |
| 7103 | "C++ binary operator overloading is missing candidates!"); |
| 7104 | if (Result.isInvalid()) |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7105 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 7106 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7107 | return move(Result); |
| 7108 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7109 | |
| 7110 | case OR_Ambiguous: |
| 7111 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 7112 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7113 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7114 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 7115 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7116 | return ExprError(); |
| 7117 | |
| 7118 | case OR_Deleted: |
| 7119 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7120 | << Best->Function->isDeleted() |
| 7121 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7122 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7123 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7124 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7125 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7126 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 7127 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 7128 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7131 | ExprResult |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7132 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 7133 | SourceLocation RLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7134 | Expr *Base, Expr *Idx) { |
| 7135 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7136 | DeclarationName OpName = |
| 7137 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 7138 | |
| 7139 | // If either side is type-dependent, create an appropriate dependent |
| 7140 | // expression. |
| 7141 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 7142 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7143 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7144 | // CHECKME: no 'operator' keyword? |
| 7145 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 7146 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7147 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7148 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7149 | 0, SourceRange(), OpNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 7150 | /*ADL*/ true, /*Overloaded*/ false, |
| 7151 | UnresolvedSetIterator(), |
| 7152 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7153 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7154 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7155 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 7156 | Args, 2, |
| 7157 | Context.DependentTy, |
| 7158 | RLoc)); |
| 7159 | } |
| 7160 | |
| 7161 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7162 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7163 | |
| 7164 | // Subscript can only be overloaded as a member function. |
| 7165 | |
| 7166 | // Add operator candidates that are member functions. |
| 7167 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7168 | |
| 7169 | // Add builtin operator candidates. |
| 7170 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7171 | |
| 7172 | // Perform overload resolution. |
| 7173 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7174 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7175 | case OR_Success: { |
| 7176 | // We found a built-in operator or an overloaded operator. |
| 7177 | FunctionDecl *FnDecl = Best->Function; |
| 7178 | |
| 7179 | if (FnDecl) { |
| 7180 | // We matched an overloaded operator. Build a call to that |
| 7181 | // operator. |
| 7182 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7183 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7184 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7185 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7186 | // Convert the arguments. |
| 7187 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7188 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7189 | Best->FoundDecl, Method)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7190 | return ExprError(); |
| 7191 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 7192 | // Convert the arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7193 | ExprResult InputInit |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 7194 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7195 | FnDecl->getParamDecl(0)), |
| 7196 | SourceLocation(), |
| 7197 | Owned(Args[1])); |
| 7198 | if (InputInit.isInvalid()) |
| 7199 | return ExprError(); |
| 7200 | |
| 7201 | Args[1] = InputInit.takeAs<Expr>(); |
| 7202 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7203 | // Determine the result type |
| 7204 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7205 | = FnDecl->getType()->getAs<FunctionType>() |
| 7206 | ->getCallResultType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7207 | |
| 7208 | // Build the actual expression node. |
| 7209 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 7210 | LLoc); |
| 7211 | UsualUnaryConversions(FnExpr); |
| 7212 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7213 | CXXOperatorCallExpr *TheCall = |
| 7214 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 7215 | FnExpr, Args, 2, |
| 7216 | ResultTy, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7217 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7218 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7219 | FnDecl)) |
| 7220 | return ExprError(); |
| 7221 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7222 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7223 | } else { |
| 7224 | // We matched a built-in operator. Convert the arguments, then |
| 7225 | // break out so that we will build the appropriate built-in |
| 7226 | // operator node. |
| 7227 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7228 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7229 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7230 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7231 | return ExprError(); |
| 7232 | |
| 7233 | break; |
| 7234 | } |
| 7235 | } |
| 7236 | |
| 7237 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7238 | if (CandidateSet.empty()) |
| 7239 | Diag(LLoc, diag::err_ovl_no_oper) |
| 7240 | << Args[0]->getType() << /*subscript*/ 0 |
| 7241 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 7242 | else |
| 7243 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 7244 | << Args[0]->getType() |
| 7245 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7246 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 7247 | "[]", LLoc); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7248 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | case OR_Ambiguous: |
| 7252 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 7253 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7254 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 7255 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7256 | return ExprError(); |
| 7257 | |
| 7258 | case OR_Deleted: |
| 7259 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 7260 | << Best->Function->isDeleted() << "[]" |
| 7261 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7262 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 7263 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7264 | return ExprError(); |
| 7265 | } |
| 7266 | |
| 7267 | // We matched a built-in operator; build it. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7268 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7269 | } |
| 7270 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7271 | /// BuildCallToMemberFunction - Build a call to a member |
| 7272 | /// function. MemExpr is the expression that refers to the member |
| 7273 | /// function (and includes the object parameter), Args/NumArgs are the |
| 7274 | /// arguments to the function call (not including the object |
| 7275 | /// parameter). The caller needs to validate that the member |
| 7276 | /// expression refers to a member function or an overloaded member |
| 7277 | /// function. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7278 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7279 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 7280 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7281 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 7282 | SourceLocation RParenLoc) { |
| 7283 | // Dig out the member expression. This holds both the object |
| 7284 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7285 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 7286 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7287 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7288 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 7289 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7290 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7291 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 7292 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7293 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7294 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7295 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7296 | } else { |
| 7297 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7298 | Qualifier = UnresExpr->getQualifier(); |
| 7299 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7300 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7301 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7302 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7303 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7304 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7305 | // FIXME: avoid copy. |
| 7306 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7307 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 7308 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7309 | TemplateArgs = &TemplateArgsBuffer; |
| 7310 | } |
| 7311 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7312 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 7313 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 7314 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7315 | NamedDecl *Func = *I; |
| 7316 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 7317 | if (isa<UsingShadowDecl>(Func)) |
| 7318 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 7319 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7320 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7321 | // If explicit template arguments were provided, we can't call a |
| 7322 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7323 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7324 | continue; |
| 7325 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7326 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7327 | Args, NumArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7328 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7329 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7330 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7331 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7332 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7333 | CandidateSet, |
| 7334 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7335 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7337 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7338 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 7339 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7340 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7341 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
| 7342 | Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7343 | case OR_Success: |
| 7344 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7345 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7346 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7347 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7348 | break; |
| 7349 | |
| 7350 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7351 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7352 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7353 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7354 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7355 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7356 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7357 | |
| 7358 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7359 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7360 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7361 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7362 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7363 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7364 | |
| 7365 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7366 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7367 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7368 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7369 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7370 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7371 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7372 | } |
| 7373 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7374 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7375 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7376 | // If overload resolution picked a static member, build a |
| 7377 | // non-member call based on that function. |
| 7378 | if (Method->isStatic()) { |
| 7379 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 7380 | Args, NumArgs, RParenLoc); |
| 7381 | } |
| 7382 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7383 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7384 | } |
| 7385 | |
| 7386 | assert(Method && "Member call to something that isn't a method?"); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7387 | CXXMemberCallExpr *TheCall = |
| 7388 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
| 7389 | Method->getCallResultType(), |
| 7390 | RParenLoc); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7391 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7392 | // Check for a valid return type. |
| 7393 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7394 | TheCall, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7395 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7396 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7397 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7398 | // We only need to do this if there was actually an overload; otherwise |
| 7399 | // it was done at lookup. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7400 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7401 | if (!Method->isStatic() && |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7402 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 7403 | FoundDecl, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7404 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7405 | MemExpr->setBase(ObjectArg); |
| 7406 | |
| 7407 | // Convert the rest of the arguments |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 7408 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7409 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7410 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7411 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7412 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7413 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7414 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 7415 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7416 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7417 | } |
| 7418 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7419 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 7420 | /// type (C++ [over.call.object]), which can end up invoking an |
| 7421 | /// overloaded function call operator (@c operator()) or performing a |
| 7422 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7423 | Sema::ExprResult |
| 7424 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 7425 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7426 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7427 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7428 | SourceLocation RParenLoc) { |
| 7429 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7430 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7431 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7432 | // C++ [over.call.object]p1: |
| 7433 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7434 | // 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] | 7435 | // candidate functions includes at least the function call |
| 7436 | // operators of T. The function call operators of T are obtained by |
| 7437 | // ordinary lookup of the name operator() in the context of |
| 7438 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7439 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7440 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7441 | |
| 7442 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7443 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7444 | << Object->getSourceRange())) |
| 7445 | return true; |
| 7446 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7447 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 7448 | LookupQualifiedName(R, Record->getDecl()); |
| 7449 | R.suppressDiagnostics(); |
| 7450 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7451 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7452 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7453 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7454 | Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 7455 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7456 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7457 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7458 | // C++ [over.call.object]p2: |
| 7459 | // In addition, for each conversion function declared in T of the |
| 7460 | // form |
| 7461 | // |
| 7462 | // operator conversion-type-id () cv-qualifier; |
| 7463 | // |
| 7464 | // where cv-qualifier is the same cv-qualification as, or a |
| 7465 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 7466 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 7467 | // R", or the type "reference to pointer to function of |
| 7468 | // (P1,...,Pn) returning R", or the type "reference to function |
| 7469 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7470 | // is also considered as a candidate function. Similarly, |
| 7471 | // surrogate call functions are added to the set of candidate |
| 7472 | // functions for each conversion function declared in an |
| 7473 | // accessible base class provided the function is not hidden |
| 7474 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7475 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 7476 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7477 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7478 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7479 | NamedDecl *D = *I; |
| 7480 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 7481 | if (isa<UsingShadowDecl>(D)) |
| 7482 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7483 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7484 | // Skip over templated conversion functions; they aren't |
| 7485 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7486 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7487 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7488 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7489 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7490 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7491 | // Strip the reference type (if any) and then the pointer type (if |
| 7492 | // any) to get down to what might be a function type. |
| 7493 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 7494 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7495 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7496 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7497 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7498 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7499 | Object->getType(), Args, NumArgs, |
| 7500 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7502 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7503 | // Perform overload resolution. |
| 7504 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7505 | switch (CandidateSet.BestViableFunction(*this, Object->getLocStart(), |
| 7506 | Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7507 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7508 | // Overload resolution succeeded; we'll build the appropriate call |
| 7509 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7510 | break; |
| 7511 | |
| 7512 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7513 | if (CandidateSet.empty()) |
| 7514 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 7515 | << Object->getType() << /*call*/ 1 |
| 7516 | << Object->getSourceRange(); |
| 7517 | else |
| 7518 | Diag(Object->getSourceRange().getBegin(), |
| 7519 | diag::err_ovl_no_viable_object_call) |
| 7520 | << Object->getType() << Object->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7521 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7522 | break; |
| 7523 | |
| 7524 | case OR_Ambiguous: |
| 7525 | Diag(Object->getSourceRange().getBegin(), |
| 7526 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7527 | << Object->getType() << Object->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7528 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7529 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7530 | |
| 7531 | case OR_Deleted: |
| 7532 | Diag(Object->getSourceRange().getBegin(), |
| 7533 | diag::err_ovl_deleted_object_call) |
| 7534 | << Best->Function->isDeleted() |
| 7535 | << Object->getType() << Object->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7536 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7537 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7538 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7539 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7540 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7541 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7542 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7543 | if (Best->Function == 0) { |
| 7544 | // Since there is no function declaration, this is one of the |
| 7545 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7546 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7547 | = cast<CXXConversionDecl>( |
| 7548 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 7549 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7550 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7551 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7552 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7553 | // We selected one of the surrogate functions that converts the |
| 7554 | // object parameter to a function pointer. Perform the conversion |
| 7555 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7556 | |
| 7557 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7558 | // and then call it. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7559 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 7560 | Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7561 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7562 | return ActOnCallExpr(S, CE, LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7563 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 7564 | CommaLocs, RParenLoc); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7567 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7568 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7569 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7570 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 7571 | // that calls this method, using Object for the implicit object |
| 7572 | // parameter and passing along the remaining arguments. |
| 7573 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7574 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7575 | |
| 7576 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7577 | unsigned NumArgsToCheck = NumArgs; |
| 7578 | |
| 7579 | // Build the full argument list for the method call (the |
| 7580 | // implicit object parameter is placed at the beginning of the |
| 7581 | // list). |
| 7582 | Expr **MethodArgs; |
| 7583 | if (NumArgs < NumArgsInProto) { |
| 7584 | NumArgsToCheck = NumArgsInProto; |
| 7585 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 7586 | } else { |
| 7587 | MethodArgs = new Expr*[NumArgs + 1]; |
| 7588 | } |
| 7589 | MethodArgs[0] = Object; |
| 7590 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7591 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7592 | |
| 7593 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7594 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7595 | UsualUnaryConversions(NewFn); |
| 7596 | |
| 7597 | // Once we've built TheCall, all of the expressions are properly |
| 7598 | // owned. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7599 | QualType ResultTy = Method->getCallResultType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7600 | CXXOperatorCallExpr *TheCall = |
| 7601 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
| 7602 | MethodArgs, NumArgs + 1, |
| 7603 | ResultTy, RParenLoc); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7604 | delete [] MethodArgs; |
| 7605 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7606 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 7607 | Method)) |
| 7608 | return true; |
| 7609 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7610 | // We may have default arguments. If so, we need to allocate more |
| 7611 | // slots in the call for them. |
| 7612 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7613 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7614 | else if (NumArgs > NumArgsInProto) |
| 7615 | NumArgsToCheck = NumArgsInProto; |
| 7616 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7617 | bool IsError = false; |
| 7618 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7619 | // Initialize the implicit object parameter. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7620 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7621 | Best->FoundDecl, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7622 | TheCall->setArg(0, Object); |
| 7623 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7624 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7625 | // Check the argument types. |
| 7626 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7627 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7628 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7629 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7630 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7631 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7632 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7633 | ExprResult InputInit |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7634 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7635 | Method->getParamDecl(i)), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7636 | SourceLocation(), Arg); |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7637 | |
| 7638 | IsError |= InputInit.isInvalid(); |
| 7639 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7640 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7641 | ExprResult DefArg |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 7642 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 7643 | if (DefArg.isInvalid()) { |
| 7644 | IsError = true; |
| 7645 | break; |
| 7646 | } |
| 7647 | |
| 7648 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7649 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7650 | |
| 7651 | TheCall->setArg(i + 1, Arg); |
| 7652 | } |
| 7653 | |
| 7654 | // If this is a variadic call, handle args passed through "...". |
| 7655 | if (Proto->isVariadic()) { |
| 7656 | // Promote the arguments (C99 6.5.2.2p7). |
| 7657 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 7658 | Expr *Arg = Args[i]; |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 7659 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod, 0); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7660 | TheCall->setArg(i + 1, Arg); |
| 7661 | } |
| 7662 | } |
| 7663 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7664 | if (IsError) return true; |
| 7665 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7666 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7667 | return true; |
| 7668 | |
John McCall | 182f709 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 7669 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7670 | } |
| 7671 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7672 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7673 | /// (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] | 7674 | /// @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] | 7675 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7676 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7677 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7678 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7679 | SourceLocation Loc = Base->getExprLoc(); |
| 7680 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7681 | // C++ [over.ref]p1: |
| 7682 | // |
| 7683 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 7684 | // for a class object x of type T if T::operator->() exists and if |
| 7685 | // the operator is selected as the best match function by the |
| 7686 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7687 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7688 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7689 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7690 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7691 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 7692 | PDiag(diag::err_typecheck_incomplete_tag) |
| 7693 | << Base->getSourceRange())) |
| 7694 | return ExprError(); |
| 7695 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7696 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7697 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 7698 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7699 | |
| 7700 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7701 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7702 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7703 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7704 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7705 | |
| 7706 | // Perform overload resolution. |
| 7707 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7708 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7709 | case OR_Success: |
| 7710 | // Overload resolution succeeded; we'll build the call below. |
| 7711 | break; |
| 7712 | |
| 7713 | case OR_No_Viable_Function: |
| 7714 | if (CandidateSet.empty()) |
| 7715 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7716 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7717 | else |
| 7718 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7719 | << "operator->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7720 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7721 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7722 | |
| 7723 | case OR_Ambiguous: |
| 7724 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7725 | << "->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7726 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7727 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7728 | |
| 7729 | case OR_Deleted: |
| 7730 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7731 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7732 | << "->" << Base->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7733 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7734 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7735 | } |
| 7736 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7737 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7738 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7739 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7740 | // Convert the object parameter. |
| 7741 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7742 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 7743 | Best->FoundDecl, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7744 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 7745 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7746 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7747 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 7748 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7749 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7750 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7751 | QualType ResultTy = Method->getCallResultType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7752 | CXXOperatorCallExpr *TheCall = |
| 7753 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 7754 | &Base, 1, ResultTy, OpLoc); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7755 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7756 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7757 | Method)) |
| 7758 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7759 | return Owned(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7762 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 7763 | /// a C++ overloaded function (possibly with some parentheses and |
| 7764 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 7765 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7766 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7767 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7768 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7769 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7770 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 7771 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7772 | if (SubExpr == PE->getSubExpr()) |
| 7773 | return PE->Retain(); |
| 7774 | |
| 7775 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 7776 | } |
| 7777 | |
| 7778 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7779 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 7780 | Found, Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7781 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7782 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7783 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7784 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7785 | if (SubExpr == ICE->getSubExpr()) |
| 7786 | return ICE->Retain(); |
| 7787 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7788 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
| 7789 | ICE->getCastKind(), |
| 7790 | SubExpr, 0, |
| 7791 | ICE->getCategory()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7792 | } |
| 7793 | |
| 7794 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7795 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7796 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7797 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7798 | if (Method->isStatic()) { |
| 7799 | // Do nothing: static member functions aren't any different |
| 7800 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7801 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7802 | // Fix the sub expression, which really has to be an |
| 7803 | // UnresolvedLookupExpr holding an overloaded member function |
| 7804 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7805 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7806 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7807 | if (SubExpr == UnOp->getSubExpr()) |
| 7808 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7809 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7810 | assert(isa<DeclRefExpr>(SubExpr) |
| 7811 | && "fixed to something other than a decl ref"); |
| 7812 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 7813 | && "fixed to a member ref with no nested name qualifier"); |
| 7814 | |
| 7815 | // We have taken the address of a pointer to member |
| 7816 | // function. Perform the computation here so that we get the |
| 7817 | // appropriate pointer to member type. |
| 7818 | QualType ClassType |
| 7819 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 7820 | QualType MemPtrType |
| 7821 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 7822 | |
| 7823 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7824 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7825 | } |
| 7826 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7827 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7828 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7829 | if (SubExpr == UnOp->getSubExpr()) |
| 7830 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7831 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7832 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7833 | Context.getPointerType(SubExpr->getType()), |
| 7834 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7835 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7836 | |
| 7837 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7838 | // FIXME: avoid copy. |
| 7839 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7840 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7841 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7842 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7843 | } |
| 7844 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7845 | return DeclRefExpr::Create(Context, |
| 7846 | ULE->getQualifier(), |
| 7847 | ULE->getQualifierRange(), |
| 7848 | Fn, |
| 7849 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7850 | Fn->getType(), |
| 7851 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7852 | } |
| 7853 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7854 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7855 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7856 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7857 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 7858 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7859 | TemplateArgs = &TemplateArgsBuffer; |
| 7860 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7861 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7862 | Expr *Base; |
| 7863 | |
| 7864 | // If we're filling in |
| 7865 | if (MemExpr->isImplicitAccess()) { |
| 7866 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 7867 | return DeclRefExpr::Create(Context, |
| 7868 | MemExpr->getQualifier(), |
| 7869 | MemExpr->getQualifierRange(), |
| 7870 | Fn, |
| 7871 | MemExpr->getMemberLoc(), |
| 7872 | Fn->getType(), |
| 7873 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 7874 | } else { |
| 7875 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 7876 | if (MemExpr->getQualifier()) |
| 7877 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 7878 | Base = new (Context) CXXThisExpr(Loc, |
| 7879 | MemExpr->getBaseType(), |
| 7880 | /*isImplicit=*/true); |
| 7881 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7882 | } else |
| 7883 | Base = MemExpr->getBase()->Retain(); |
| 7884 | |
| 7885 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7886 | MemExpr->isArrow(), |
| 7887 | MemExpr->getQualifier(), |
| 7888 | MemExpr->getQualifierRange(), |
| 7889 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7890 | Found, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7891 | MemExpr->getMemberNameInfo(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7892 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7893 | Fn->getType()); |
| 7894 | } |
| 7895 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7896 | assert(false && "Invalid reference to overloaded function"); |
| 7897 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7900 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
| 7901 | DeclAccessPair Found, |
| 7902 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7903 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7906 | } // end namespace clang |