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 | |
| 14 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 16 | #include "SemaInit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | /// GetConversionCategory - Retrieve the implicit conversion |
| 32 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 35 | static const ImplicitConversionCategory |
| 36 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 37 | ICC_Identity, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 41 | ICC_Identity, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 42 | ICC_Qualification_Adjustment, |
| 43 | ICC_Promotion, |
| 44 | ICC_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 45 | ICC_Promotion, |
| 46 | ICC_Conversion, |
| 47 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
| 52 | ICC_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 54 | ICC_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 55 | ICC_Conversion, |
| 56 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 57 | ICC_Conversion |
| 58 | }; |
| 59 | return Category[(int)Kind]; |
| 60 | } |
| 61 | |
| 62 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 63 | /// corresponding to the given implicit conversion kind. |
| 64 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 65 | static const ImplicitConversionRank |
| 66 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
| 70 | ICR_Exact_Match, |
| 71 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 72 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 73 | ICR_Promotion, |
| 74 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 75 | ICR_Promotion, |
| 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
| 81 | ICR_Conversion, |
| 82 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 83 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 84 | ICR_Conversion, |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 85 | ICR_Conversion, |
| 86 | ICR_Conversion, |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 87 | ICR_Complex_Real_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 88 | }; |
| 89 | return Rank[(int)Kind]; |
| 90 | } |
| 91 | |
| 92 | /// GetImplicitConversionName - Return the name of this kind of |
| 93 | /// implicit conversion. |
| 94 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 95 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 96 | "No conversion", |
| 97 | "Lvalue-to-rvalue", |
| 98 | "Array-to-pointer", |
| 99 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 100 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Qualification", |
| 102 | "Integral promotion", |
| 103 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 104 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | "Integral conversion", |
| 106 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 107 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | "Floating-integral conversion", |
| 109 | "Pointer conversion", |
| 110 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 111 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 112 | "Compatible-types conversion", |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 113 | "Derived-to-base conversion", |
| 114 | "Vector conversion", |
| 115 | "Vector splat", |
| 116 | "Complex-real conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 117 | }; |
| 118 | return Name[Kind]; |
| 119 | } |
| 120 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 121 | /// StandardConversionSequence - Set the standard conversion |
| 122 | /// sequence to the identity conversion. |
| 123 | void StandardConversionSequence::setAsIdentityConversion() { |
| 124 | First = ICK_Identity; |
| 125 | Second = ICK_Identity; |
| 126 | Third = ICK_Identity; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 127 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 128 | ReferenceBinding = false; |
| 129 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 130 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 131 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 134 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 135 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 136 | /// implicit conversions. |
| 137 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 138 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 139 | if (GetConversionRank(First) > Rank) |
| 140 | Rank = GetConversionRank(First); |
| 141 | if (GetConversionRank(Second) > Rank) |
| 142 | Rank = GetConversionRank(Second); |
| 143 | if (GetConversionRank(Third) > Rank) |
| 144 | Rank = GetConversionRank(Third); |
| 145 | return Rank; |
| 146 | } |
| 147 | |
| 148 | /// isPointerConversionToBool - Determines whether this conversion is |
| 149 | /// 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] | 150 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 151 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | // Note that FromType has not necessarily been transformed by the |
| 154 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 155 | // check for their presence as well as checking whether FromType is |
| 156 | // a pointer. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 157 | if (getToType(1)->isBooleanType() && |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 158 | (getFromType()->isPointerType() || |
| 159 | getFromType()->isObjCObjectPointerType() || |
| 160 | getFromType()->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 161 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 162 | return true; |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 167 | /// isPointerConversionToVoidPointer - Determines whether this |
| 168 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 169 | /// used as part of the ranking of standard conversion sequences (C++ |
| 170 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 172 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 174 | QualType FromType = getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 175 | QualType ToType = getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 176 | |
| 177 | // Note that FromType has not necessarily been transformed by the |
| 178 | // array-to-pointer implicit conversion, so check for its presence |
| 179 | // and redo the conversion to get a pointer. |
| 180 | if (First == ICK_Array_To_Pointer) |
| 181 | FromType = Context.getArrayDecayedType(FromType); |
| 182 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 183 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 184 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 185 | return ToPtrType->getPointeeType()->isVoidType(); |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 190 | /// DebugPrint - Print this standard conversion sequence to standard |
| 191 | /// error. Useful for debugging overloading issues. |
| 192 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 193 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 194 | bool PrintedSomething = false; |
| 195 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 196 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 197 | PrintedSomething = true; |
| 198 | } |
| 199 | |
| 200 | if (Second != ICK_Identity) { |
| 201 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 202 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 203 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 204 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 205 | |
| 206 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 207 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 208 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 209 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 210 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 211 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 212 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (Third != ICK_Identity) { |
| 217 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 218 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 220 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | PrintedSomething = true; |
| 222 | } |
| 223 | |
| 224 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 225 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 230 | /// error. Useful for debugging overloading issues. |
| 231 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 232 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 233 | if (Before.First || Before.Second || Before.Third) { |
| 234 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 235 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 236 | } |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 237 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 238 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 239 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 240 | After.DebugPrint(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 245 | /// error. Useful for debugging overloading issues. |
| 246 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 247 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 248 | switch (ConversionKind) { |
| 249 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 250 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 251 | Standard.DebugPrint(); |
| 252 | break; |
| 253 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 254 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 255 | UserDefined.DebugPrint(); |
| 256 | break; |
| 257 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 258 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 259 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 260 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 261 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 262 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 263 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 264 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 268 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 269 | } |
| 270 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 271 | void AmbiguousConversionSequence::construct() { |
| 272 | new (&conversions()) ConversionSet(); |
| 273 | } |
| 274 | |
| 275 | void AmbiguousConversionSequence::destruct() { |
| 276 | conversions().~ConversionSet(); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 281 | FromTypePtr = O.FromTypePtr; |
| 282 | ToTypePtr = O.ToTypePtr; |
| 283 | new (&conversions()) ConversionSet(O.conversions()); |
| 284 | } |
| 285 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 286 | namespace { |
| 287 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 288 | // template parameter and template argument information. |
| 289 | struct DFIParamWithArguments { |
| 290 | TemplateParameter Param; |
| 291 | TemplateArgument FirstArg; |
| 292 | TemplateArgument SecondArg; |
| 293 | }; |
| 294 | } |
| 295 | |
| 296 | /// \brief Convert from Sema's representation of template deduction information |
| 297 | /// to the form used in overload-candidate information. |
| 298 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 299 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 300 | Sema::TemplateDeductionResult TDK, |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 301 | Sema::TemplateDeductionInfo &Info) { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 302 | OverloadCandidate::DeductionFailureInfo Result; |
| 303 | Result.Result = static_cast<unsigned>(TDK); |
| 304 | Result.Data = 0; |
| 305 | switch (TDK) { |
| 306 | case Sema::TDK_Success: |
| 307 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 308 | case Sema::TDK_TooManyArguments: |
| 309 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 313 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 314 | Result.Data = Info.Param.getOpaqueValue(); |
| 315 | break; |
| 316 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 317 | case Sema::TDK_Inconsistent: |
| 318 | case Sema::TDK_InconsistentQuals: { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 319 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 320 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 321 | Saved->Param = Info.Param; |
| 322 | Saved->FirstArg = Info.FirstArg; |
| 323 | Saved->SecondArg = Info.SecondArg; |
| 324 | Result.Data = Saved; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 329 | Result.Data = Info.take(); |
| 330 | break; |
| 331 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 332 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 333 | case Sema::TDK_FailedOverloadResolution: |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | return Result; |
| 338 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 340 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 341 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 342 | case Sema::TDK_Success: |
| 343 | case Sema::TDK_InstantiationDepth: |
| 344 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 345 | case Sema::TDK_TooManyArguments: |
| 346 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 347 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 348 | break; |
| 349 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 350 | case Sema::TDK_Inconsistent: |
| 351 | case Sema::TDK_InconsistentQuals: |
Douglas Gregor | aaa045d | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 352 | // FIXME: Destroy the data? |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 353 | Data = 0; |
| 354 | break; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 355 | |
| 356 | case Sema::TDK_SubstitutionFailure: |
| 357 | // FIXME: Destroy the template arugment list? |
| 358 | Data = 0; |
| 359 | break; |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 361 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 362 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 363 | case Sema::TDK_FailedOverloadResolution: |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | TemplateParameter |
| 369 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 370 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 371 | case Sema::TDK_Success: |
| 372 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 373 | case Sema::TDK_TooManyArguments: |
| 374 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 375 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 376 | return TemplateParameter(); |
| 377 | |
| 378 | case Sema::TDK_Incomplete: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 379 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 380 | return TemplateParameter::getFromOpaqueValue(Data); |
| 381 | |
| 382 | case Sema::TDK_Inconsistent: |
| 383 | case Sema::TDK_InconsistentQuals: |
| 384 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
| 385 | |
| 386 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 387 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 388 | case Sema::TDK_FailedOverloadResolution: |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | return TemplateParameter(); |
| 393 | } |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 394 | |
| 395 | TemplateArgumentList * |
| 396 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 397 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 398 | case Sema::TDK_Success: |
| 399 | case Sema::TDK_InstantiationDepth: |
| 400 | case Sema::TDK_TooManyArguments: |
| 401 | case Sema::TDK_TooFewArguments: |
| 402 | case Sema::TDK_Incomplete: |
| 403 | case Sema::TDK_InvalidExplicitArguments: |
| 404 | case Sema::TDK_Inconsistent: |
| 405 | case Sema::TDK_InconsistentQuals: |
| 406 | return 0; |
| 407 | |
| 408 | case Sema::TDK_SubstitutionFailure: |
| 409 | return static_cast<TemplateArgumentList*>(Data); |
| 410 | |
| 411 | // Unhandled |
| 412 | case Sema::TDK_NonDeducedMismatch: |
| 413 | case Sema::TDK_FailedOverloadResolution: |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 420 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 421 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 422 | case Sema::TDK_Success: |
| 423 | case Sema::TDK_InstantiationDepth: |
| 424 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 425 | case Sema::TDK_TooManyArguments: |
| 426 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 427 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 428 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 429 | return 0; |
| 430 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 431 | case Sema::TDK_Inconsistent: |
| 432 | case Sema::TDK_InconsistentQuals: |
| 433 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
| 434 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 435 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 436 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 437 | case Sema::TDK_FailedOverloadResolution: |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | const TemplateArgument * |
| 445 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 446 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 447 | case Sema::TDK_Success: |
| 448 | case Sema::TDK_InstantiationDepth: |
| 449 | case Sema::TDK_Incomplete: |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 450 | case Sema::TDK_TooManyArguments: |
| 451 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 452 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 453 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 454 | return 0; |
| 455 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 456 | case Sema::TDK_Inconsistent: |
| 457 | case Sema::TDK_InconsistentQuals: |
| 458 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 459 | |
Douglas Gregor | 0ca4c58 | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 460 | // Unhandled |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 461 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 462 | case Sema::TDK_FailedOverloadResolution: |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | void OverloadCandidateSet::clear() { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 470 | inherited::clear(); |
| 471 | Functions.clear(); |
| 472 | } |
| 473 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 475 | // overload of the declarations in Old. This routine returns false if |
| 476 | // New and Old cannot be overloaded, e.g., if New has the same |
| 477 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 478 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 479 | // it does return false, MatchedDecl will point to the decl that New |
| 480 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 481 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | // |
| 483 | // Example: Given the following input: |
| 484 | // |
| 485 | // void f(int, float); // #1 |
| 486 | // void f(int, int); // #2 |
| 487 | // int f(int, int); // #3 |
| 488 | // |
| 489 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 492 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 493 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 494 | // (since they have different signatures), so this routine returns |
| 495 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 496 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 497 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 498 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 499 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 500 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 502 | // point to the FunctionDecl for #2. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 503 | // |
| 504 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 505 | // into a class by a using declaration. The rules for whether to hide |
| 506 | // shadow declarations ignore some properties which otherwise figure |
| 507 | // into a function template's signature. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 508 | Sema::OverloadKind |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 509 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 510 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 511 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 512 | I != E; ++I) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 513 | NamedDecl *OldD = *I; |
| 514 | |
| 515 | bool OldIsUsingDecl = false; |
| 516 | if (isa<UsingShadowDecl>(OldD)) { |
| 517 | OldIsUsingDecl = true; |
| 518 | |
| 519 | // We can always introduce two using declarations into the same |
| 520 | // context, even if they have identical signatures. |
| 521 | if (NewIsUsingDecl) continue; |
| 522 | |
| 523 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 524 | } |
| 525 | |
| 526 | // If either declaration was introduced by a using declaration, |
| 527 | // we'll need to use slightly different rules for matching. |
| 528 | // Essentially, these rules are the normal rules, except that |
| 529 | // function templates hide function templates with different |
| 530 | // return types or template parameter lists. |
| 531 | bool UseMemberUsingDeclRules = |
| 532 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 533 | |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 534 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 535 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 536 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 537 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 538 | continue; |
| 539 | } |
| 540 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 541 | Match = *I; |
| 542 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 543 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 544 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 545 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 546 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 547 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 548 | continue; |
| 549 | } |
| 550 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 551 | Match = *I; |
| 552 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 553 | } |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 554 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 555 | // We can overload with these, which can show up when doing |
| 556 | // redeclaration checks for UsingDecls. |
| 557 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 558 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 559 | // Optimistically assume that an unresolved using decl will |
| 560 | // overload; if it doesn't, we'll have to diagnose during |
| 561 | // template instantiation. |
| 562 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 563 | // (C++ 13p1): |
| 564 | // Only function declarations can be overloaded; object and type |
| 565 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 566 | Match = *I; |
| 567 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 568 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 569 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 570 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 571 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 572 | } |
| 573 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 574 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 575 | bool UseUsingDeclRules) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 576 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 577 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 578 | |
| 579 | // C++ [temp.fct]p2: |
| 580 | // A function template can be overloaded with other function templates |
| 581 | // and with normal (non-template) functions. |
| 582 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 583 | return true; |
| 584 | |
| 585 | // Is the function New an overload of the function Old? |
| 586 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 587 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 588 | |
| 589 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 590 | // determine whether they are overloads. If we find any mismatch |
| 591 | // in the signature, they are overloads. |
| 592 | |
| 593 | // If either of these functions is a K&R-style function (no |
| 594 | // prototype), then we consider them to have matching signatures. |
| 595 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 596 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 597 | return false; |
| 598 | |
| 599 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 600 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 601 | |
| 602 | // The signature of a function includes the types of its |
| 603 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 604 | // of the ellipsis; see C++ DR 357). |
| 605 | if (OldQType != NewQType && |
| 606 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 607 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 608 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 609 | return true; |
| 610 | |
| 611 | // C++ [temp.over.link]p4: |
| 612 | // The signature of a function template consists of its function |
| 613 | // signature, its return type and its template parameter list. The names |
| 614 | // of the template parameters are significant only for establishing the |
| 615 | // relationship between the template parameters and the rest of the |
| 616 | // signature. |
| 617 | // |
| 618 | // We check the return type and template parameter lists for function |
| 619 | // templates first; the remaining checks follow. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 620 | // |
| 621 | // However, we don't consider either of these when deciding whether |
| 622 | // a member introduced by a shadow declaration is hidden. |
| 623 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 624 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 625 | OldTemplate->getTemplateParameters(), |
| 626 | false, TPL_TemplateMatch) || |
| 627 | OldType->getResultType() != NewType->getResultType())) |
| 628 | return true; |
| 629 | |
| 630 | // If the function is a class member, its signature includes the |
| 631 | // cv-qualifiers (if any) on the function itself. |
| 632 | // |
| 633 | // As part of this, also check whether one of the member functions |
| 634 | // is static, in which case they are not overloads (C++ |
| 635 | // 13.1p2). While not part of the definition of the signature, |
| 636 | // this check is important to determine whether these functions |
| 637 | // can be overloaded. |
| 638 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 639 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 640 | if (OldMethod && NewMethod && |
| 641 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 642 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 643 | return true; |
| 644 | |
| 645 | // The signatures match; this is not an overload. |
| 646 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 649 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 650 | /// from the given expression (Expr) to the given type (ToType). This |
| 651 | /// function returns an implicit conversion sequence that can be used |
| 652 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 653 | /// |
| 654 | /// void f(float f); |
| 655 | /// void g(int i) { f(i); } |
| 656 | /// |
| 657 | /// this routine would produce an implicit conversion sequence to |
| 658 | /// describe the initialization of f from i, which will be a standard |
| 659 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 660 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 661 | // |
| 662 | /// Note that this routine only determines how the conversion can be |
| 663 | /// performed; it does not actually perform the conversion. As such, |
| 664 | /// it will not produce any diagnostics if no conversion is available, |
| 665 | /// but will instead return an implicit conversion sequence of kind |
| 666 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 667 | /// |
| 668 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 669 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 670 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 671 | /// permitted. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 672 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 673 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 674 | bool SuppressUserConversions, |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 675 | bool AllowExplicit, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 676 | bool InOverloadResolution) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 677 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 678 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 679 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 680 | return ICS; |
| 681 | } |
| 682 | |
| 683 | if (!getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 684 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 685 | return ICS; |
| 686 | } |
| 687 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 688 | if (SuppressUserConversions) { |
| 689 | // C++ [over.ics.user]p4: |
| 690 | // A conversion of an expression of class type to the same class |
| 691 | // type is given Exact Match rank, and a conversion of an |
| 692 | // expression of class type to a base class of that type is |
| 693 | // given Conversion rank, in spite of the fact that a copy/move |
| 694 | // constructor (i.e., a user-defined conversion function) is |
| 695 | // called for those cases. |
| 696 | QualType FromType = From->getType(); |
| 697 | if (!ToType->getAs<RecordType>() || !FromType->getAs<RecordType>() || |
| 698 | !(Context.hasSameUnqualifiedType(FromType, ToType) || |
| 699 | IsDerivedFrom(FromType, ToType))) { |
| 700 | // We're not in the case above, so there is no conversion that |
| 701 | // we can perform. |
| 702 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 703 | return ICS; |
| 704 | } |
| 705 | |
| 706 | ICS.setStandard(); |
| 707 | ICS.Standard.setAsIdentityConversion(); |
| 708 | ICS.Standard.setFromType(FromType); |
| 709 | ICS.Standard.setAllToTypes(ToType); |
| 710 | |
| 711 | // We don't actually check at this point whether there is a valid |
| 712 | // copy/move constructor, since overloading just assumes that it |
| 713 | // exists. When we actually perform initialization, we'll find the |
| 714 | // appropriate constructor to copy the returned object, if needed. |
| 715 | ICS.Standard.CopyConstructor = 0; |
| 716 | |
| 717 | // Determine whether this is considered a derived-to-base conversion. |
| 718 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 719 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 720 | |
| 721 | return ICS; |
| 722 | } |
| 723 | |
| 724 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 725 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 726 | OverloadingResult UserDefResult |
| 727 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 728 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 729 | |
| 730 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 731 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 732 | // C++ [over.ics.user]p4: |
| 733 | // A conversion of an expression of class type to the same class |
| 734 | // type is given Exact Match rank, and a conversion of an |
| 735 | // expression of class type to a base class of that type is |
| 736 | // given Conversion rank, in spite of the fact that a copy |
| 737 | // constructor (i.e., a user-defined conversion function) is |
| 738 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 740 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 742 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 743 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 744 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 0d6d12b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 745 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 746 | // Turn this into a "standard" conversion sequence, so that it |
| 747 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 748 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 749 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 750 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 751 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 752 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 753 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 754 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 755 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 757 | |
| 758 | // C++ [over.best.ics]p4: |
| 759 | // However, when considering the argument of a user-defined |
| 760 | // conversion function that is a candidate by 13.3.1.3 when |
| 761 | // invoked for the copying of the temporary in the second step |
| 762 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 763 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 764 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 765 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 766 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 767 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 768 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 769 | ICS.setAmbiguous(); |
| 770 | ICS.Ambiguous.setFromType(From->getType()); |
| 771 | ICS.Ambiguous.setToType(ToType); |
| 772 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 773 | Cand != Conversions.end(); ++Cand) |
| 774 | if (Cand->Viable) |
| 775 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 776 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 777 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 778 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 779 | |
| 780 | return ICS; |
| 781 | } |
| 782 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 783 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 784 | /// expression From to the type ToType. Returns true if there was an |
| 785 | /// error, false otherwise. The expression From is replaced with the |
| 786 | /// converted expression. Flavor is the kind of conversion we're |
| 787 | /// performing, used in the error message. If @p AllowExplicit, |
| 788 | /// explicit user-defined conversions are permitted. |
| 789 | bool |
| 790 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 791 | AssignmentAction Action, bool AllowExplicit) { |
| 792 | ImplicitConversionSequence ICS; |
| 793 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 794 | } |
| 795 | |
| 796 | bool |
| 797 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 798 | AssignmentAction Action, bool AllowExplicit, |
| 799 | ImplicitConversionSequence& ICS) { |
| 800 | ICS = TryImplicitConversion(From, ToType, |
| 801 | /*SuppressUserConversions=*/false, |
| 802 | AllowExplicit, |
| 803 | /*InOverloadResolution=*/false); |
| 804 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 805 | } |
| 806 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 807 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 808 | /// conversion that strips "noreturn" off the nested function type. |
| 809 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 810 | QualType ToType, QualType &ResultTy) { |
| 811 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 812 | return false; |
| 813 | |
| 814 | // Strip the noreturn off the type we're converting from; noreturn can |
| 815 | // safely be removed. |
| 816 | FromType = Context.getNoReturnType(FromType, false); |
| 817 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 818 | return false; |
| 819 | |
| 820 | ResultTy = FromType; |
| 821 | return true; |
| 822 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 823 | |
| 824 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 825 | /// vector conversion. |
| 826 | /// |
| 827 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 828 | /// conversion. |
| 829 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 830 | QualType ToType, ImplicitConversionKind &ICK) { |
| 831 | // We need at least one of these types to be a vector type to have a vector |
| 832 | // conversion. |
| 833 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 834 | return false; |
| 835 | |
| 836 | // Identical types require no conversions. |
| 837 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 838 | return false; |
| 839 | |
| 840 | // There are no conversions between extended vector types, only identity. |
| 841 | if (ToType->isExtVectorType()) { |
| 842 | // There are no conversions between extended vector types other than the |
| 843 | // identity conversion. |
| 844 | if (FromType->isExtVectorType()) |
| 845 | return false; |
| 846 | |
| 847 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 848 | if (FromType->isArithmeticType()) { |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 849 | ICK = ICK_Vector_Splat; |
| 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | // If lax vector conversions are permitted and the vector types are of the |
| 855 | // same size, we can perform the conversion. |
| 856 | if (Context.getLangOptions().LaxVectorConversions && |
| 857 | FromType->isVectorType() && ToType->isVectorType() && |
| 858 | Context.getTypeSize(FromType) == Context.getTypeSize(ToType)) { |
| 859 | ICK = ICK_Vector_Conversion; |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | return false; |
| 864 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 865 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 866 | /// IsStandardConversion - Determines whether there is a standard |
| 867 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 868 | /// expression From to the type ToType. Standard conversion sequences |
| 869 | /// only consider non-class types; for conversions that involve class |
| 870 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 871 | /// contain the standard conversion sequence required to perform this |
| 872 | /// conversion and this routine will return true. Otherwise, this |
| 873 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | bool |
| 875 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 876 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 878 | QualType FromType = From->getType(); |
| 879 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 880 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 881 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 882 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 883 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 884 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 885 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 887 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 889 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 890 | if (getLangOptions().CPlusPlus) |
| 891 | return false; |
| 892 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 896 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 897 | // array-to-pointer conversion, or function-to-pointer conversion |
| 898 | // (C++ 4p1). |
| 899 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 900 | if (FromType == Context.OverloadTy) { |
| 901 | DeclAccessPair AccessPair; |
| 902 | if (FunctionDecl *Fn |
| 903 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 904 | AccessPair)) { |
| 905 | // We were able to resolve the address of the overloaded function, |
| 906 | // so we can convert to the type of that function. |
| 907 | FromType = Fn->getType(); |
| 908 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 909 | if (!Method->isStatic()) { |
| 910 | Type *ClassType |
| 911 | = Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 912 | FromType = Context.getMemberPointerType(FromType, ClassType); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // If the "from" expression takes the address of the overloaded |
| 917 | // function, update the type of the resulting expression accordingly. |
| 918 | if (FromType->getAs<FunctionType>()) |
| 919 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(From->IgnoreParens())) |
| 920 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 921 | FromType = Context.getPointerType(FromType); |
| 922 | |
| 923 | // Check that we've computed the proper type after overload resolution. |
| 924 | assert(Context.hasSameType(FromType, |
| 925 | FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
| 926 | } else { |
| 927 | return false; |
| 928 | } |
| 929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 931 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 932 | // converted to an rvalue. |
| 933 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 935 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 936 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 937 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 938 | |
| 939 | // If T is a non-class type, the type of the rvalue is the |
| 940 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 941 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 942 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 943 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 944 | } else if (FromType->isArrayType()) { |
| 945 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 946 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 947 | |
| 948 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 949 | // bound of T" can be converted to an rvalue of type "pointer to |
| 950 | // T" (C++ 4.2p1). |
| 951 | FromType = Context.getArrayDecayedType(FromType); |
| 952 | |
| 953 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 954 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 955 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 956 | |
| 957 | // For the purpose of ranking in overload resolution |
| 958 | // (13.3.3.1.1), this conversion is considered an |
| 959 | // array-to-pointer conversion followed by a qualification |
| 960 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 961 | SCS.Second = ICK_Identity; |
| 962 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 963 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 964 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 965 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 966 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 967 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 968 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 969 | |
| 970 | // An lvalue of function type T can be converted to an rvalue of |
| 971 | // type "pointer to T." The result is a pointer to the |
| 972 | // function. (C++ 4.3p1). |
| 973 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 974 | } else { |
| 975 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 976 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 977 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 978 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 979 | |
| 980 | // The second conversion can be an integral promotion, floating |
| 981 | // point promotion, integral conversion, floating point conversion, |
| 982 | // floating-integral conversion, pointer conversion, |
| 983 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 984 | // For overloading in C, this can also be a "compatible-type" |
| 985 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 986 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 987 | ImplicitConversionKind SecondICK = ICK_Identity; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 988 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 989 | // The unqualified versions of the types are the same: there's no |
| 990 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 991 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 992 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 994 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 995 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 996 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 997 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 998 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 999 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1000 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 1001 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1002 | SCS.Second = ICK_Complex_Promotion; |
| 1003 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1004 | } else if (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1005 | ToType->isIntegralType(Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1006 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1007 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1008 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1009 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 1010 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1011 | SCS.Second = ICK_Complex_Conversion; |
| 1012 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1013 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 1014 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 1015 | // Complex-real conversions (C99 6.3.1.7) |
| 1016 | SCS.Second = ICK_Complex_Real; |
| 1017 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1018 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1019 | // Floating point conversions (C++ 4.8). |
| 1020 | SCS.Second = ICK_Floating_Conversion; |
| 1021 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1022 | } else if ((FromType->isRealFloatingType() && |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1023 | ToType->isIntegralType(Context) && !ToType->isBooleanType()) || |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1024 | (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1025 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1026 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1027 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1028 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1029 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1030 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1031 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1032 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1033 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1034 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 1035 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1036 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1037 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1038 | } else if (ToType->isBooleanType() && |
| 1039 | (FromType->isArithmeticType() || |
| 1040 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 1041 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1042 | FromType->isBlockPointerType() || |
| 1043 | FromType->isMemberPointerType() || |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1044 | FromType->isNullPtrType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1045 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1046 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1047 | FromType = Context.BoolTy; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1048 | } else if (IsVectorConversion(Context, FromType, ToType, SecondICK)) { |
| 1049 | SCS.Second = SecondICK; |
| 1050 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1052 | Context.typesAreCompatible(ToType, FromType)) { |
| 1053 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1054 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1055 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1056 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 1057 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1058 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1059 | } else { |
| 1060 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1061 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1062 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1063 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1064 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1065 | QualType CanonFrom; |
| 1066 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1067 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1068 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1069 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1070 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1071 | CanonFrom = Context.getCanonicalType(FromType); |
| 1072 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1075 | SCS.Third = ICK_Identity; |
| 1076 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1078 | // [...] Any difference in top-level cv-qualification is |
| 1079 | // subsumed by the initialization itself and does not constitute |
| 1080 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1081 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1083 | if (CanonFrom.getLocalUnqualifiedType() |
| 1084 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1085 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1086 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1087 | FromType = ToType; |
| 1088 | CanonFrom = CanonTo; |
| 1089 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1090 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1091 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1092 | |
| 1093 | // If we have not converted the argument type to the parameter type, |
| 1094 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1095 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1096 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1098 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1102 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1103 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1104 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1106 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1107 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1108 | if (!To) { |
| 1109 | return false; |
| 1110 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1111 | |
| 1112 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1113 | // unsigned short int can be converted to an rvalue of type int if |
| 1114 | // int can represent all the values of the source type; otherwise, |
| 1115 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1116 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1117 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1118 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1119 | if (// We can promote any signed, promotable integer type to an int |
| 1120 | (FromType->isSignedIntegerType() || |
| 1121 | // We can promote any unsigned integer type whose size is |
| 1122 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1124 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1125 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1128 | return To->getKind() == BuiltinType::UInt; |
| 1129 | } |
| 1130 | |
| 1131 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 1132 | // can be converted to an rvalue of the first of the following types |
| 1133 | // that can represent all the values of its underlying type: int, |
| 1134 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1135 | |
| 1136 | // We pre-calculate the promotion type for enum types. |
| 1137 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 1138 | if (ToType->isIntegerType()) |
| 1139 | return Context.hasSameUnqualifiedType(ToType, |
| 1140 | FromEnumType->getDecl()->getPromotionType()); |
| 1141 | |
| 1142 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1143 | // Determine whether the type we're converting from is signed or |
| 1144 | // unsigned. |
| 1145 | bool FromIsSigned; |
| 1146 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1147 | |
| 1148 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1149 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1150 | |
| 1151 | // The types we'll try to promote to, in the appropriate |
| 1152 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | QualType PromoteTypes[6] = { |
| 1154 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1155 | Context.LongTy, Context.UnsignedLongTy , |
| 1156 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1157 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1158 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1159 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1160 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1162 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1163 | // We found the type that we can promote to. If this is the |
| 1164 | // type we wanted, we have a promotion. Otherwise, no |
| 1165 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1166 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1172 | // rvalue of type int if int can represent all the values of the |
| 1173 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1174 | // unsigned int can represent all the values of the bit-field. If |
| 1175 | // the bit-field is larger yet, no integral promotion applies to |
| 1176 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1177 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1178 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1179 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1180 | using llvm::APSInt; |
| 1181 | if (From) |
| 1182 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1183 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1184 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1185 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1186 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1187 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1189 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1190 | if (BitWidth < ToSize || |
| 1191 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1192 | return To->getKind() == BuiltinType::Int; |
| 1193 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1195 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1196 | // that fits into an unsigned int? |
| 1197 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1198 | return To->getKind() == BuiltinType::UInt; |
| 1199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1201 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1202 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1205 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1206 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1207 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1208 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1209 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1210 | |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1215 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1216 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1218 | /// An rvalue of type float can be converted to an rvalue of type |
| 1219 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1220 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1221 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1222 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1223 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1224 | return true; |
| 1225 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1226 | // C99 6.3.1.5p1: |
| 1227 | // When a float is promoted to double or long double, or a |
| 1228 | // double is promoted to long double [...]. |
| 1229 | if (!getLangOptions().CPlusPlus && |
| 1230 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1231 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1232 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1236 | return false; |
| 1237 | } |
| 1238 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1239 | /// \brief Determine if a conversion is a complex promotion. |
| 1240 | /// |
| 1241 | /// A complex promotion is defined as a complex -> complex conversion |
| 1242 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1243 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1244 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1245 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1246 | if (!FromComplex) |
| 1247 | return false; |
| 1248 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1249 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1250 | if (!ToComplex) |
| 1251 | return false; |
| 1252 | |
| 1253 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1254 | ToComplex->getElementType()) || |
| 1255 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1256 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1259 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1260 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1261 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1262 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1263 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | static QualType |
| 1265 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1266 | QualType ToPointee, QualType ToType, |
| 1267 | ASTContext &Context) { |
| 1268 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 1269 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1270 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1273 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1274 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1275 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1276 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1279 | // already. |
| 1280 | return Context.getPointerType(ToPointee); |
| 1281 | } |
| 1282 | |
| 1283 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1284 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1285 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1286 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1289 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1290 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1291 | /// not have the right set of qualifiers. |
| 1292 | static QualType |
| 1293 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1294 | QualType ToType, |
| 1295 | ASTContext &Context) { |
| 1296 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1297 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1298 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1299 | |
| 1300 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1301 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1302 | return ToType; |
| 1303 | |
| 1304 | // Just build a canonical type that has the right qualifiers. |
| 1305 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1306 | } |
| 1307 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1309 | bool InOverloadResolution, |
| 1310 | ASTContext &Context) { |
| 1311 | // Handle value-dependent integral null pointer constants correctly. |
| 1312 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1313 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1314 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1315 | return !InOverloadResolution; |
| 1316 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1317 | return Expr->isNullPointerConstant(Context, |
| 1318 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1319 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1322 | /// IsPointerConversion - Determines whether the conversion of the |
| 1323 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1324 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1325 | /// 4.10). If so, returns true and places the converted type (that |
| 1326 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1327 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1328 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1329 | /// This routine also supports conversions to and from block pointers |
| 1330 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1331 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1332 | /// appropriate overloading rules for Objective-C, we may want to |
| 1333 | /// split the Objective-C checks into a different routine; however, |
| 1334 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1335 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1336 | /// set if the conversion is an allowed Objective-C conversion that |
| 1337 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1338 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1339 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1340 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1342 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1343 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1344 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1345 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1347 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1348 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1349 | ConvertedType = ToType; |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1353 | // Blocks: Block pointers can be converted to void*. |
| 1354 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1355 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1356 | ConvertedType = ToType; |
| 1357 | return true; |
| 1358 | } |
| 1359 | // Blocks: A null pointer constant can be converted to a block |
| 1360 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1362 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1363 | ConvertedType = ToType; |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1367 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1368 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1370 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1371 | ConvertedType = ToType; |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1375 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1376 | if (!ToTypePtr) |
| 1377 | return false; |
| 1378 | |
| 1379 | // 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] | 1380 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1381 | ConvertedType = ToType; |
| 1382 | return true; |
| 1383 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1384 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1385 | // Beyond this point, both types need to be pointers |
| 1386 | // , including objective-c pointers. |
| 1387 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1388 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1389 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1390 | ToType, Context); |
| 1391 | return true; |
| 1392 | |
| 1393 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1394 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1395 | if (!FromTypePtr) |
| 1396 | return false; |
| 1397 | |
| 1398 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1400 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1401 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1402 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1403 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1405 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1406 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
| 1409 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1410 | // When we're overloading in C, we allow a special kind of pointer |
| 1411 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1413 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1415 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1417 | return true; |
| 1418 | } |
| 1419 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1420 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1422 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1423 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1424 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1425 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1426 | // necessitates this conversion is ill-formed. The result of the |
| 1427 | // conversion is a pointer to the base class sub-object of the |
| 1428 | // derived class object. The null pointer value is converted to |
| 1429 | // the null pointer value of the destination type. |
| 1430 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1431 | // Note that we do not check for ambiguity or inaccessibility |
| 1432 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1433 | if (getLangOptions().CPlusPlus && |
| 1434 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1435 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1436 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1437 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1439 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1440 | ToType, Context); |
| 1441 | return true; |
| 1442 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
| 1446 | |
| 1447 | /// isObjCPointerConversion - Determines whether this is an |
| 1448 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1449 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1451 | QualType& ConvertedType, |
| 1452 | bool &IncompatibleObjC) { |
| 1453 | if (!getLangOptions().ObjC1) |
| 1454 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1455 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1456 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1457 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1459 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1460 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1461 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1462 | // 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] | 1463 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1464 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1465 | ConvertedType = ToType; |
| 1466 | return true; |
| 1467 | } |
| 1468 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1470 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1472 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1473 | ConvertedType = ToType; |
| 1474 | return true; |
| 1475 | } |
| 1476 | // Objective C++: We're able to convert from a pointer to an |
| 1477 | // interface to a pointer to a different interface. |
| 1478 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1479 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1480 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1481 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1482 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1483 | FromObjCPtr->getPointeeType())) |
| 1484 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1485 | ConvertedType = ToType; |
| 1486 | return true; |
| 1487 | } |
| 1488 | |
| 1489 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1490 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1491 | // interfaces, which is permitted. However, we're going to |
| 1492 | // complain about it. |
| 1493 | IncompatibleObjC = true; |
| 1494 | ConvertedType = FromType; |
| 1495 | return true; |
| 1496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1498 | // 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] | 1499 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1500 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1501 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1502 | else if (const BlockPointerType *ToBlockPtr = |
| 1503 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1504 | // 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] | 1505 | // to a block pointer type. |
| 1506 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1507 | ConvertedType = ToType; |
| 1508 | return true; |
| 1509 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1510 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1511 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1512 | else if (FromType->getAs<BlockPointerType>() && |
| 1513 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1514 | // 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] | 1515 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1516 | ConvertedType = ToType; |
| 1517 | return true; |
| 1518 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1519 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1520 | return false; |
| 1521 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1522 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1523 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1524 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1525 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1526 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1527 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1528 | return false; |
| 1529 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1530 | // If we have pointers to pointers, recursively check whether this |
| 1531 | // is an Objective-C conversion. |
| 1532 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1533 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1534 | IncompatibleObjC)) { |
| 1535 | // We always complain about this conversion. |
| 1536 | IncompatibleObjC = true; |
| 1537 | ConvertedType = ToType; |
| 1538 | return true; |
| 1539 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1540 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1541 | // as in I* to id. |
| 1542 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1543 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1544 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1545 | IncompatibleObjC)) { |
| 1546 | ConvertedType = ToType; |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1550 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1551 | // differences in the argument and result types are in Objective-C |
| 1552 | // pointer conversions. If so, we permit the conversion (but |
| 1553 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1555 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1556 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1557 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1558 | if (FromFunctionType && ToFunctionType) { |
| 1559 | // If the function types are exactly the same, this isn't an |
| 1560 | // Objective-C pointer conversion. |
| 1561 | if (Context.getCanonicalType(FromPointeeType) |
| 1562 | == Context.getCanonicalType(ToPointeeType)) |
| 1563 | return false; |
| 1564 | |
| 1565 | // Perform the quick checks that will tell us whether these |
| 1566 | // function types are obviously different. |
| 1567 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1568 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1569 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1570 | return false; |
| 1571 | |
| 1572 | bool HasObjCConversion = false; |
| 1573 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1574 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1575 | // Okay, the types match exactly. Nothing to do. |
| 1576 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1577 | ToFunctionType->getResultType(), |
| 1578 | ConvertedType, IncompatibleObjC)) { |
| 1579 | // Okay, we have an Objective-C pointer conversion. |
| 1580 | HasObjCConversion = true; |
| 1581 | } else { |
| 1582 | // Function types are too different. Abort. |
| 1583 | return false; |
| 1584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1586 | // Check argument types. |
| 1587 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1588 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1589 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1590 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1591 | if (Context.getCanonicalType(FromArgType) |
| 1592 | == Context.getCanonicalType(ToArgType)) { |
| 1593 | // Okay, the types match exactly. Nothing to do. |
| 1594 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1595 | ConvertedType, IncompatibleObjC)) { |
| 1596 | // Okay, we have an Objective-C pointer conversion. |
| 1597 | HasObjCConversion = true; |
| 1598 | } else { |
| 1599 | // Argument types are too different. Abort. |
| 1600 | return false; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | if (HasObjCConversion) { |
| 1605 | // We had an Objective-C conversion. Allow this pointer |
| 1606 | // conversion, but complain about it. |
| 1607 | ConvertedType = ToType; |
| 1608 | IncompatibleObjC = true; |
| 1609 | return true; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1613 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1614 | } |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1615 | |
| 1616 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1617 | /// for equlity of their argument types. Caller has already checked that |
| 1618 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1619 | /// pointer types which only differ in their protocol qualifiers are equal. |
| 1620 | bool Sema::FunctionArgTypesAreEqual(FunctionProtoType* OldType, |
| 1621 | FunctionProtoType* NewType){ |
| 1622 | if (!getLangOptions().ObjC1) |
| 1623 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1624 | NewType->arg_type_begin()); |
| 1625 | |
| 1626 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1627 | N = NewType->arg_type_begin(), |
| 1628 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1629 | QualType ToType = (*O); |
| 1630 | QualType FromType = (*N); |
| 1631 | if (ToType != FromType) { |
| 1632 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1633 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1634 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1635 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1636 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1637 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1638 | continue; |
| 1639 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1640 | else if (const ObjCObjectPointerType *PTTo = |
| 1641 | ToType->getAs<ObjCObjectPointerType>()) { |
| 1642 | if (const ObjCObjectPointerType *PTFr = |
| 1643 | FromType->getAs<ObjCObjectPointerType>()) |
| 1644 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1645 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1646 | } |
| 1647 | return false; |
| 1648 | } |
| 1649 | } |
| 1650 | return true; |
| 1651 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1652 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1653 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1654 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1655 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1656 | /// conversions for which IsPointerConversion has already returned |
| 1657 | /// true. It returns true and produces a diagnostic if there was an |
| 1658 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1659 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1660 | CastExpr::CastKind &Kind, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1661 | CXXBaseSpecifierArray& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1662 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1663 | QualType FromType = From->getType(); |
| 1664 | |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1665 | if (CXXBoolLiteralExpr* LitBool |
| 1666 | = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens())) |
| 1667 | if (LitBool->getValue() == false) |
| 1668 | Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false) |
| 1669 | << ToType; |
| 1670 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1671 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1672 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1673 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1674 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1676 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1677 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1678 | // We must have a derived-to-base conversion. Check an |
| 1679 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1680 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1681 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1682 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1683 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1684 | return true; |
| 1685 | |
| 1686 | // The conversion was successful. |
| 1687 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1691 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1693 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1694 | // Objective-C++ conversions are always okay. |
| 1695 | // FIXME: We should have a different class of conversions for the |
| 1696 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1697 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1698 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1699 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1700 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1701 | return false; |
| 1702 | } |
| 1703 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1704 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1705 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1706 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1707 | /// If so, returns true and places the converted type (that might differ from |
| 1708 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1709 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1710 | QualType ToType, |
| 1711 | bool InOverloadResolution, |
| 1712 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1713 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1714 | if (!ToTypePtr) |
| 1715 | return false; |
| 1716 | |
| 1717 | // 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] | 1718 | if (From->isNullPointerConstant(Context, |
| 1719 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1720 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1721 | ConvertedType = ToType; |
| 1722 | return true; |
| 1723 | } |
| 1724 | |
| 1725 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1726 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1727 | if (!FromTypePtr) |
| 1728 | return false; |
| 1729 | |
| 1730 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1731 | // where D is derived from B (C++ 4.11p2). |
| 1732 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1733 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1734 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1735 | |
| 1736 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1737 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1738 | ToClass.getTypePtr()); |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | return false; |
| 1743 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1744 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1745 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1746 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1747 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1748 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1749 | /// true and produces a diagnostic if there was an error, or returns false |
| 1750 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1752 | CastExpr::CastKind &Kind, |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1753 | CXXBaseSpecifierArray &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1754 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1755 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1756 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1757 | if (!FromPtrType) { |
| 1758 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1759 | assert(From->isNullPointerConstant(Context, |
| 1760 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1761 | "Expr must be null pointer constant!"); |
| 1762 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1763 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1764 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1765 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1766 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1767 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1768 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1769 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1770 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1771 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1772 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1773 | // FIXME: What about dependent types? |
| 1774 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1775 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1776 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1777 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1778 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1779 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1780 | assert(DerivationOkay && |
| 1781 | "Should not have been called if derivation isn't OK."); |
| 1782 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1783 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1784 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1785 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1786 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1787 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1788 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1789 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1790 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1792 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1793 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1794 | << FromClass << ToClass << QualType(VBase, 0) |
| 1795 | << From->getSourceRange(); |
| 1796 | return true; |
| 1797 | } |
| 1798 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1799 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1800 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1801 | Paths.front(), |
| 1802 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1803 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1804 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1805 | BuildBasePathArray(Paths, BasePath); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1806 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1807 | return false; |
| 1808 | } |
| 1809 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1810 | /// IsQualificationConversion - Determines whether the conversion from |
| 1811 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1812 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | bool |
| 1814 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1815 | FromType = Context.getCanonicalType(FromType); |
| 1816 | ToType = Context.getCanonicalType(ToType); |
| 1817 | |
| 1818 | // If FromType and ToType are the same type, this is not a |
| 1819 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1820 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1821 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1823 | // (C++ 4.4p4): |
| 1824 | // A conversion can add cv-qualifiers at levels other than the first |
| 1825 | // in multi-level pointers, subject to the following rules: [...] |
| 1826 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1827 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1828 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1829 | // Within each iteration of the loop, we check the qualifiers to |
| 1830 | // determine if this still looks like a qualification |
| 1831 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1832 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1833 | // until there are no more pointers or pointers-to-members left to |
| 1834 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1835 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1836 | |
| 1837 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1838 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1839 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1840 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1842 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1843 | // every cv for 0 < k < j. |
| 1844 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1845 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1846 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1848 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1849 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1851 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1853 | |
| 1854 | // We are left with FromType and ToType being the pointee types |
| 1855 | // after unwrapping the original FromType and ToType the same number |
| 1856 | // of types. If we unwrapped any pointers, and if FromType and |
| 1857 | // ToType have the same unqualified type (since we checked |
| 1858 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1859 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1862 | /// Determines whether there is a user-defined conversion sequence |
| 1863 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1864 | /// ToType. If such a conversion exists, User will contain the |
| 1865 | /// user-defined conversion sequence that performs such a conversion |
| 1866 | /// and this routine will return true. Otherwise, this routine returns |
| 1867 | /// false and User is unspecified. |
| 1868 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1869 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1870 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1871 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1872 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1873 | UserDefinedConversionSequence& User, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1874 | OverloadCandidateSet& CandidateSet, |
| 1875 | bool AllowExplicit) { |
| 1876 | // Whether we will only visit constructors. |
| 1877 | bool ConstructorsOnly = false; |
| 1878 | |
| 1879 | // If the type we are conversion to is a class type, enumerate its |
| 1880 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1881 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1882 | // C++ [over.match.ctor]p1: |
| 1883 | // When objects of class type are direct-initialized (8.5), or |
| 1884 | // copy-initialized from an expression of the same or a |
| 1885 | // derived class type (8.5), overload resolution selects the |
| 1886 | // constructor. [...] For copy-initialization, the candidate |
| 1887 | // functions are all the converting constructors (12.3.1) of |
| 1888 | // that class. The argument list is the expression-list within |
| 1889 | // the parentheses of the initializer. |
| 1890 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1891 | (From->getType()->getAs<RecordType>() && |
| 1892 | IsDerivedFrom(From->getType(), ToType))) |
| 1893 | ConstructorsOnly = true; |
| 1894 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1895 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1896 | // We're not going to find any constructors. |
| 1897 | } else if (CXXRecordDecl *ToRecordDecl |
| 1898 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1899 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 1900 | for (llvm::tie(Con, ConEnd) = LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1901 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1902 | NamedDecl *D = *Con; |
| 1903 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1904 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1905 | // Find the constructor (which may be a template). |
| 1906 | CXXConstructorDecl *Constructor = 0; |
| 1907 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1908 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1909 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1911 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1912 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1913 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1914 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1915 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1916 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1917 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1918 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1919 | /*ExplicitArgs*/ 0, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1920 | &From, 1, CandidateSet, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1921 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1922 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1923 | // Allow one user-defined conversion when user specifies a |
| 1924 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1925 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1926 | &From, 1, CandidateSet, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1927 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1928 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1929 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1933 | // Enumerate conversion functions, if we're allowed to. |
| 1934 | if (ConstructorsOnly) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1936 | PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1937 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1939 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1941 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1942 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1943 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1944 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1945 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1946 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1947 | DeclAccessPair FoundDecl = I.getPair(); |
| 1948 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1949 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1950 | if (isa<UsingShadowDecl>(D)) |
| 1951 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1952 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1953 | CXXConversionDecl *Conv; |
| 1954 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1955 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1956 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1957 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1958 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1959 | |
| 1960 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1961 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1962 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1963 | ActingContext, From, ToType, |
| 1964 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1965 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1966 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1967 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1971 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1972 | |
| 1973 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1974 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1975 | case OR_Success: |
| 1976 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1978 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1979 | // C++ [over.ics.user]p1: |
| 1980 | // If the user-defined conversion is specified by a |
| 1981 | // constructor (12.3.1), the initial standard conversion |
| 1982 | // sequence converts the source type to the type required by |
| 1983 | // the argument of the constructor. |
| 1984 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1985 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1986 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1987 | User.EllipsisConversion = true; |
| 1988 | else { |
| 1989 | User.Before = Best->Conversions[0].Standard; |
| 1990 | User.EllipsisConversion = false; |
| 1991 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1992 | User.ConversionFunction = Constructor; |
| 1993 | User.After.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1994 | User.After.setFromType( |
| 1995 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1996 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1997 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1998 | } else if (CXXConversionDecl *Conversion |
| 1999 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 2000 | // C++ [over.ics.user]p1: |
| 2001 | // |
| 2002 | // [...] If the user-defined conversion is specified by a |
| 2003 | // conversion function (12.3.2), the initial standard |
| 2004 | // conversion sequence converts the source type to the |
| 2005 | // implicit object parameter of the conversion function. |
| 2006 | User.Before = Best->Conversions[0].Standard; |
| 2007 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2008 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
| 2010 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2011 | // The second standard conversion sequence converts the |
| 2012 | // result of the user-defined conversion to the target type |
| 2013 | // for the sequence. Since an implicit conversion sequence |
| 2014 | // is an initialization, the special rules for |
| 2015 | // initialization by user-defined conversion apply when |
| 2016 | // selecting the best user-defined conversion for a |
| 2017 | // user-defined conversion sequence (see 13.3.3 and |
| 2018 | // 13.3.3.1). |
| 2019 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2020 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2021 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2022 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2023 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2026 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2027 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2028 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2029 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2030 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2031 | |
| 2032 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2033 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2036 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2037 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2038 | |
| 2039 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2040 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2041 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2042 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2043 | OverloadingResult OvResult = |
| 2044 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2045 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2046 | if (OvResult == OR_Ambiguous) |
| 2047 | Diag(From->getSourceRange().getBegin(), |
| 2048 | diag::err_typecheck_ambiguous_condition) |
| 2049 | << From->getType() << ToType << From->getSourceRange(); |
| 2050 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2051 | Diag(From->getSourceRange().getBegin(), |
| 2052 | diag::err_typecheck_nonviable_condition) |
| 2053 | << From->getType() << ToType << From->getSourceRange(); |
| 2054 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2055 | return false; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 2056 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2057 | return true; |
| 2058 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2060 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2061 | /// conversion sequences to determine whether one is better than the |
| 2062 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2064 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 2065 | const ImplicitConversionSequence& ICS2) |
| 2066 | { |
| 2067 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2068 | // conversion sequences (as defined in 13.3.3.1) |
| 2069 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2070 | // conversion sequence than a user-defined conversion sequence or |
| 2071 | // an ellipsis conversion sequence, and |
| 2072 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2073 | // conversion sequence than an ellipsis conversion sequence |
| 2074 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2076 | // C++0x [over.best.ics]p10: |
| 2077 | // For the purpose of ranking implicit conversion sequences as |
| 2078 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2079 | // treated as a user-defined sequence that is indistinguishable |
| 2080 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2081 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2082 | return ImplicitConversionSequence::Better; |
| 2083 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2084 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2085 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2086 | // The following checks require both conversion sequences to be of |
| 2087 | // the same kind. |
| 2088 | if (ICS1.getKind() != ICS2.getKind()) |
| 2089 | return ImplicitConversionSequence::Indistinguishable; |
| 2090 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2091 | // Two implicit conversion sequences of the same form are |
| 2092 | // indistinguishable conversion sequences unless one of the |
| 2093 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2094 | if (ICS1.isStandard()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2095 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2096 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2097 | // User-defined conversion sequence U1 is a better conversion |
| 2098 | // sequence than another user-defined conversion sequence U2 if |
| 2099 | // they contain the same user-defined conversion function or |
| 2100 | // constructor and if the second standard conversion sequence of |
| 2101 | // U1 is better than the second standard conversion sequence of |
| 2102 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2104 | ICS2.UserDefined.ConversionFunction) |
| 2105 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 2106 | ICS2.UserDefined.After); |
| 2107 | } |
| 2108 | |
| 2109 | return ImplicitConversionSequence::Indistinguishable; |
| 2110 | } |
| 2111 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2112 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2113 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2114 | Qualifiers Quals; |
| 2115 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
| 2116 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
| 2117 | } |
| 2118 | |
| 2119 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2120 | } |
| 2121 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2122 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2123 | // determine if one is a proper subset of the other. |
| 2124 | static ImplicitConversionSequence::CompareKind |
| 2125 | compareStandardConversionSubsets(ASTContext &Context, |
| 2126 | const StandardConversionSequence& SCS1, |
| 2127 | const StandardConversionSequence& SCS2) { |
| 2128 | ImplicitConversionSequence::CompareKind Result |
| 2129 | = ImplicitConversionSequence::Indistinguishable; |
| 2130 | |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2131 | // the identity conversion sequence is considered to be a subsequence of |
| 2132 | // any non-identity conversion sequence |
| 2133 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2134 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2135 | return ImplicitConversionSequence::Better; |
| 2136 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2137 | return ImplicitConversionSequence::Worse; |
| 2138 | } |
| 2139 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2140 | if (SCS1.Second != SCS2.Second) { |
| 2141 | if (SCS1.Second == ICK_Identity) |
| 2142 | Result = ImplicitConversionSequence::Better; |
| 2143 | else if (SCS2.Second == ICK_Identity) |
| 2144 | Result = ImplicitConversionSequence::Worse; |
| 2145 | else |
| 2146 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2147 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2148 | return ImplicitConversionSequence::Indistinguishable; |
| 2149 | |
| 2150 | if (SCS1.Third == SCS2.Third) { |
| 2151 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2152 | : ImplicitConversionSequence::Indistinguishable; |
| 2153 | } |
| 2154 | |
| 2155 | if (SCS1.Third == ICK_Identity) |
| 2156 | return Result == ImplicitConversionSequence::Worse |
| 2157 | ? ImplicitConversionSequence::Indistinguishable |
| 2158 | : ImplicitConversionSequence::Better; |
| 2159 | |
| 2160 | if (SCS2.Third == ICK_Identity) |
| 2161 | return Result == ImplicitConversionSequence::Better |
| 2162 | ? ImplicitConversionSequence::Indistinguishable |
| 2163 | : ImplicitConversionSequence::Worse; |
| 2164 | |
| 2165 | return ImplicitConversionSequence::Indistinguishable; |
| 2166 | } |
| 2167 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2168 | /// CompareStandardConversionSequences - Compare two standard |
| 2169 | /// conversion sequences to determine whether one is better than the |
| 2170 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2172 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 2173 | const StandardConversionSequence& SCS2) |
| 2174 | { |
| 2175 | // Standard conversion sequence S1 is a better conversion sequence |
| 2176 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2177 | |
| 2178 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2179 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2180 | // excluding any Lvalue Transformation; the identity conversion |
| 2181 | // sequence is considered to be a subsequence of any |
| 2182 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2183 | if (ImplicitConversionSequence::CompareKind CK |
| 2184 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 2185 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2186 | |
| 2187 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2188 | // defined below), or, if not that, |
| 2189 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2190 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2191 | if (Rank1 < Rank2) |
| 2192 | return ImplicitConversionSequence::Better; |
| 2193 | else if (Rank2 < Rank1) |
| 2194 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2196 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2197 | // are indistinguishable unless one of the following rules |
| 2198 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2200 | // A conversion that is not a conversion of a pointer, or |
| 2201 | // pointer to member, to bool is better than another conversion |
| 2202 | // that is such a conversion. |
| 2203 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2204 | return SCS2.isPointerConversionToBool() |
| 2205 | ? ImplicitConversionSequence::Better |
| 2206 | : ImplicitConversionSequence::Worse; |
| 2207 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2208 | // C++ [over.ics.rank]p4b2: |
| 2209 | // |
| 2210 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2211 | // conversion of B* to A* is better than conversion of B* to |
| 2212 | // void*, and conversion of A* to void* is better than conversion |
| 2213 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2215 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2217 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2218 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2219 | // Exactly one of the conversion sequences is a conversion to |
| 2220 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2221 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2222 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2223 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2224 | // Neither conversion sequence converts to a void pointer; compare |
| 2225 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2226 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 2227 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 2228 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2229 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2230 | // Both conversion sequences are conversions to void |
| 2231 | // pointers. Compare the source types to determine if there's an |
| 2232 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2233 | QualType FromType1 = SCS1.getFromType(); |
| 2234 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2235 | |
| 2236 | // Adjust the types we're converting from via the array-to-pointer |
| 2237 | // conversion, if we need to. |
| 2238 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2239 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2240 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2241 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2242 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2243 | QualType FromPointee1 |
| 2244 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2245 | QualType FromPointee2 |
| 2246 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2248 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2249 | return ImplicitConversionSequence::Better; |
| 2250 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2251 | return ImplicitConversionSequence::Worse; |
| 2252 | |
| 2253 | // Objective-C++: If one interface is more specific than the |
| 2254 | // other, it is the better one. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2255 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2256 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2257 | if (FromIface1 && FromIface1) { |
| 2258 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2259 | return ImplicitConversionSequence::Better; |
| 2260 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2261 | return ImplicitConversionSequence::Worse; |
| 2262 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2263 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2266 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2268 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2269 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2271 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2272 | // C++0x [over.ics.rank]p3b4: |
| 2273 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2274 | // implicit object parameter of a non-static member function declared |
| 2275 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 2276 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2277 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 2278 | // or if the member function in this case has a ref qualifier. |
| 2279 | // (Of course, we don't have ref qualifiers yet.) |
| 2280 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 2281 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 2282 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2283 | |
| 2284 | // C++ [over.ics.rank]p3b4: |
| 2285 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2286 | // which the references refer are the same type except for |
| 2287 | // top-level cv-qualifiers, and the type to which the reference |
| 2288 | // initialized by S2 refers is more cv-qualified than the type |
| 2289 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2290 | QualType T1 = SCS1.getToType(2); |
| 2291 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2292 | T1 = Context.getCanonicalType(T1); |
| 2293 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2294 | Qualifiers T1Quals, T2Quals; |
| 2295 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2296 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2297 | if (UnqualT1 == UnqualT2) { |
| 2298 | // If the type is an array type, promote the element qualifiers to the type |
| 2299 | // for comparison. |
| 2300 | if (isa<ArrayType>(T1) && T1Quals) |
| 2301 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2302 | if (isa<ArrayType>(T2) && T2Quals) |
| 2303 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2304 | if (T2.isMoreQualifiedThan(T1)) |
| 2305 | return ImplicitConversionSequence::Better; |
| 2306 | else if (T1.isMoreQualifiedThan(T2)) |
| 2307 | return ImplicitConversionSequence::Worse; |
| 2308 | } |
| 2309 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2310 | |
| 2311 | return ImplicitConversionSequence::Indistinguishable; |
| 2312 | } |
| 2313 | |
| 2314 | /// CompareQualificationConversions - Compares two standard conversion |
| 2315 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2317 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2318 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2320 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2321 | // -- S1 and S2 differ only in their qualification conversion and |
| 2322 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2323 | // cv-qualification signature of type T1 is a proper subset of |
| 2324 | // the cv-qualification signature of type T2, and S1 is not the |
| 2325 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2326 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2327 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2328 | return ImplicitConversionSequence::Indistinguishable; |
| 2329 | |
| 2330 | // FIXME: the example in the standard doesn't use a qualification |
| 2331 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2332 | QualType T1 = SCS1.getToType(2); |
| 2333 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2334 | T1 = Context.getCanonicalType(T1); |
| 2335 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2336 | Qualifiers T1Quals, T2Quals; |
| 2337 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2338 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2339 | |
| 2340 | // If the types are the same, we won't learn anything by unwrapped |
| 2341 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2342 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2343 | return ImplicitConversionSequence::Indistinguishable; |
| 2344 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2345 | // If the type is an array type, promote the element qualifiers to the type |
| 2346 | // for comparison. |
| 2347 | if (isa<ArrayType>(T1) && T1Quals) |
| 2348 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2349 | if (isa<ArrayType>(T2) && T2Quals) |
| 2350 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2351 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2353 | = ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2354 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2355 | // Within each iteration of the loop, we check the qualifiers to |
| 2356 | // determine if this still looks like a qualification |
| 2357 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2358 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2359 | // until there are no more pointers or pointers-to-members left |
| 2360 | // to unwrap. This essentially mimics what |
| 2361 | // IsQualificationConversion does, but here we're checking for a |
| 2362 | // strict subset of qualifiers. |
| 2363 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2364 | // The qualifiers are the same, so this doesn't tell us anything |
| 2365 | // about how the sequences rank. |
| 2366 | ; |
| 2367 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2368 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2369 | if (Result == ImplicitConversionSequence::Worse) |
| 2370 | // Neither has qualifiers that are a subset of the other's |
| 2371 | // qualifiers. |
| 2372 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2374 | Result = ImplicitConversionSequence::Better; |
| 2375 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2376 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2377 | if (Result == ImplicitConversionSequence::Better) |
| 2378 | // Neither has qualifiers that are a subset of the other's |
| 2379 | // qualifiers. |
| 2380 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2382 | Result = ImplicitConversionSequence::Worse; |
| 2383 | } else { |
| 2384 | // Qualifiers are disjoint. |
| 2385 | return ImplicitConversionSequence::Indistinguishable; |
| 2386 | } |
| 2387 | |
| 2388 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2389 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2390 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2393 | // Check that the winning standard conversion sequence isn't using |
| 2394 | // the deprecated string literal array to pointer conversion. |
| 2395 | switch (Result) { |
| 2396 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2397 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2398 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2399 | break; |
| 2400 | |
| 2401 | case ImplicitConversionSequence::Indistinguishable: |
| 2402 | break; |
| 2403 | |
| 2404 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2405 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2406 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2407 | break; |
| 2408 | } |
| 2409 | |
| 2410 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2413 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2414 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2415 | /// various kinds of derived-to-base conversions (C++ |
| 2416 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2417 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2418 | ImplicitConversionSequence::CompareKind |
| 2419 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2420 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2421 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2422 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2423 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2424 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2425 | |
| 2426 | // Adjust the types we're converting from via the array-to-pointer |
| 2427 | // conversion, if we need to. |
| 2428 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2429 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2430 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2431 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2432 | |
| 2433 | // Canonicalize all of the types. |
| 2434 | FromType1 = Context.getCanonicalType(FromType1); |
| 2435 | ToType1 = Context.getCanonicalType(ToType1); |
| 2436 | FromType2 = Context.getCanonicalType(FromType2); |
| 2437 | ToType2 = Context.getCanonicalType(ToType2); |
| 2438 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2439 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2440 | // |
| 2441 | // If class B is derived directly or indirectly from class A and |
| 2442 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2443 | // |
| 2444 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2445 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2446 | |
| 2447 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2449 | SCS2.Second == ICK_Pointer_Conversion && |
| 2450 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2451 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2452 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2454 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2456 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2457 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2458 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2459 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2460 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2461 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2462 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2463 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
| 2464 | const ObjCObjectType* ToIface1 = ToPointee1->getAs<ObjCObjectType>(); |
| 2465 | const ObjCObjectType* ToIface2 = ToPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2466 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2467 | // -- 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] | 2468 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2469 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2470 | return ImplicitConversionSequence::Better; |
| 2471 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2472 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2473 | |
| 2474 | if (ToIface1 && ToIface2) { |
| 2475 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2476 | return ImplicitConversionSequence::Better; |
| 2477 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2478 | return ImplicitConversionSequence::Worse; |
| 2479 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2480 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2481 | |
| 2482 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2483 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2484 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2485 | return ImplicitConversionSequence::Better; |
| 2486 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2487 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2488 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2489 | if (FromIface1 && FromIface2) { |
| 2490 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2491 | return ImplicitConversionSequence::Better; |
| 2492 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2493 | return ImplicitConversionSequence::Worse; |
| 2494 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2495 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2498 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2499 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2500 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2501 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2502 | const MemberPointerType * FromMemPointer1 = |
| 2503 | FromType1->getAs<MemberPointerType>(); |
| 2504 | const MemberPointerType * ToMemPointer1 = |
| 2505 | ToType1->getAs<MemberPointerType>(); |
| 2506 | const MemberPointerType * FromMemPointer2 = |
| 2507 | FromType2->getAs<MemberPointerType>(); |
| 2508 | const MemberPointerType * ToMemPointer2 = |
| 2509 | ToType2->getAs<MemberPointerType>(); |
| 2510 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2511 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2512 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2513 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2514 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2515 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2516 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2517 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2518 | // 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] | 2519 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2520 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2521 | return ImplicitConversionSequence::Worse; |
| 2522 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2523 | return ImplicitConversionSequence::Better; |
| 2524 | } |
| 2525 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2526 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2527 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2528 | return ImplicitConversionSequence::Better; |
| 2529 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2530 | return ImplicitConversionSequence::Worse; |
| 2531 | } |
| 2532 | } |
| 2533 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2534 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2535 | // -- 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] | 2536 | // -- binding of an expression of type C to a reference of type |
| 2537 | // B& is better than binding an expression of type C to a |
| 2538 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2539 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2540 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2541 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2542 | return ImplicitConversionSequence::Better; |
| 2543 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2544 | return ImplicitConversionSequence::Worse; |
| 2545 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2547 | // -- 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] | 2548 | // -- binding of an expression of type B to a reference of type |
| 2549 | // A& is better than binding an expression of type C to a |
| 2550 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2551 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2552 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2553 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2554 | return ImplicitConversionSequence::Better; |
| 2555 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2556 | return ImplicitConversionSequence::Worse; |
| 2557 | } |
| 2558 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2559 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2560 | return ImplicitConversionSequence::Indistinguishable; |
| 2561 | } |
| 2562 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2563 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2564 | /// determine whether they are reference-related, |
| 2565 | /// reference-compatible, reference-compatible with added |
| 2566 | /// qualification, or incompatible, for use in C++ initialization by |
| 2567 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2568 | /// type, and the first type (T1) is the pointee type of the reference |
| 2569 | /// type being initialized. |
| 2570 | Sema::ReferenceCompareResult |
| 2571 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2572 | QualType OrigT1, QualType OrigT2, |
| 2573 | bool& DerivedToBase) { |
| 2574 | assert(!OrigT1->isReferenceType() && |
| 2575 | "T1 must be the pointee type of the reference type"); |
| 2576 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2577 | |
| 2578 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2579 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2580 | Qualifiers T1Quals, T2Quals; |
| 2581 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2582 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2583 | |
| 2584 | // C++ [dcl.init.ref]p4: |
| 2585 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2586 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2587 | // T1 is a base class of T2. |
| 2588 | if (UnqualT1 == UnqualT2) |
| 2589 | DerivedToBase = false; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2590 | else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2591 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2592 | DerivedToBase = true; |
| 2593 | else |
| 2594 | return Ref_Incompatible; |
| 2595 | |
| 2596 | // At this point, we know that T1 and T2 are reference-related (at |
| 2597 | // least). |
| 2598 | |
| 2599 | // If the type is an array type, promote the element qualifiers to the type |
| 2600 | // for comparison. |
| 2601 | if (isa<ArrayType>(T1) && T1Quals) |
| 2602 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2603 | if (isa<ArrayType>(T2) && T2Quals) |
| 2604 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2605 | |
| 2606 | // C++ [dcl.init.ref]p4: |
| 2607 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2608 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2609 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2610 | // overload resolution, cases for which cv1 is greater |
| 2611 | // cv-qualification than cv2 are identified as |
| 2612 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2613 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2614 | return Ref_Compatible; |
| 2615 | else if (T1.isMoreQualifiedThan(T2)) |
| 2616 | return Ref_Compatible_With_Added_Qualification; |
| 2617 | else |
| 2618 | return Ref_Related; |
| 2619 | } |
| 2620 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2621 | /// \brief Look for a user-defined conversion to an lvalue reference-compatible |
| 2622 | /// with DeclType. Return true if something definite is found. |
| 2623 | static bool |
| 2624 | FindConversionToLValue(Sema &S, ImplicitConversionSequence &ICS, |
| 2625 | QualType DeclType, SourceLocation DeclLoc, |
| 2626 | Expr *Init, QualType T2, bool AllowExplicit) { |
| 2627 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 2628 | CXXRecordDecl *T2RecordDecl |
| 2629 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2630 | |
| 2631 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2632 | const UnresolvedSetImpl *Conversions |
| 2633 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2634 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2635 | E = Conversions->end(); I != E; ++I) { |
| 2636 | NamedDecl *D = *I; |
| 2637 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2638 | if (isa<UsingShadowDecl>(D)) |
| 2639 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2640 | |
| 2641 | FunctionTemplateDecl *ConvTemplate |
| 2642 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2643 | CXXConversionDecl *Conv; |
| 2644 | if (ConvTemplate) |
| 2645 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2646 | else |
| 2647 | Conv = cast<CXXConversionDecl>(D); |
| 2648 | |
| 2649 | // If the conversion function doesn't return a reference type, |
| 2650 | // it can't be considered for this conversion. An rvalue reference |
| 2651 | // is only acceptable if its referencee is a function type. |
| 2652 | const ReferenceType *RefType = |
| 2653 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 2654 | if (RefType && (RefType->isLValueReferenceType() || |
| 2655 | RefType->getPointeeType()->isFunctionType()) && |
| 2656 | (AllowExplicit || !Conv->isExplicit())) { |
| 2657 | if (ConvTemplate) |
| 2658 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2659 | Init, DeclType, CandidateSet); |
| 2660 | else |
| 2661 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2662 | DeclType, CandidateSet); |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | OverloadCandidateSet::iterator Best; |
| 2667 | switch (S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2668 | case OR_Success: |
| 2669 | // C++ [over.ics.ref]p1: |
| 2670 | // |
| 2671 | // [...] If the parameter binds directly to the result of |
| 2672 | // applying a conversion function to the argument |
| 2673 | // expression, the implicit conversion sequence is a |
| 2674 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2675 | // second standard conversion sequence either an identity |
| 2676 | // conversion or, if the conversion function returns an |
| 2677 | // entity of a type that is a derived class of the parameter |
| 2678 | // type, a derived-to-base Conversion. |
| 2679 | if (!Best->FinalConversion.DirectBinding) |
| 2680 | return false; |
| 2681 | |
| 2682 | ICS.setUserDefined(); |
| 2683 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2684 | ICS.UserDefined.After = Best->FinalConversion; |
| 2685 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2686 | ICS.UserDefined.EllipsisConversion = false; |
| 2687 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2688 | ICS.UserDefined.After.DirectBinding && |
| 2689 | "Expected a direct reference binding!"); |
| 2690 | return true; |
| 2691 | |
| 2692 | case OR_Ambiguous: |
| 2693 | ICS.setAmbiguous(); |
| 2694 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2695 | Cand != CandidateSet.end(); ++Cand) |
| 2696 | if (Cand->Viable) |
| 2697 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2698 | return true; |
| 2699 | |
| 2700 | case OR_No_Viable_Function: |
| 2701 | case OR_Deleted: |
| 2702 | // There was no suitable conversion, or we found a deleted |
| 2703 | // conversion; continue with other checks. |
| 2704 | return false; |
| 2705 | } |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 2706 | |
| 2707 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2710 | /// \brief Compute an implicit conversion sequence for reference |
| 2711 | /// initialization. |
| 2712 | static ImplicitConversionSequence |
| 2713 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2714 | SourceLocation DeclLoc, |
| 2715 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2716 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2717 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2718 | |
| 2719 | // Most paths end in a failed conversion. |
| 2720 | ImplicitConversionSequence ICS; |
| 2721 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2722 | |
| 2723 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2724 | QualType T2 = Init->getType(); |
| 2725 | |
| 2726 | // If the initializer is the address of an overloaded function, try |
| 2727 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2728 | // type of the resulting function. |
| 2729 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2730 | DeclAccessPair Found; |
| 2731 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2732 | false, Found)) |
| 2733 | T2 = Fn->getType(); |
| 2734 | } |
| 2735 | |
| 2736 | // Compute some basic properties of the types and the initializer. |
| 2737 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2738 | bool DerivedToBase = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2739 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2740 | Sema::ReferenceCompareResult RefRelationship |
| 2741 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
| 2742 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2743 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2744 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2745 | // A reference to type "cv1 T1" is initialized by an expression |
| 2746 | // of type "cv2 T2" as follows: |
| 2747 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2748 | // -- If reference is an lvalue reference and the initializer expression |
| 2749 | // The next bullet point (T1 is a function) is pretty much equivalent to this |
| 2750 | // one, so it's handled here. |
| 2751 | if (!isRValRef || T1->isFunctionType()) { |
| 2752 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2753 | // reference-compatible with "cv2 T2," or |
| 2754 | // |
| 2755 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2756 | if (InitCategory.isLValue() && |
| 2757 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2758 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2759 | // When a parameter of reference type binds directly (8.5.3) |
| 2760 | // to an argument expression, the implicit conversion sequence |
| 2761 | // is the identity conversion, unless the argument expression |
| 2762 | // has a type that is a derived class of the parameter type, |
| 2763 | // in which case the implicit conversion sequence is a |
| 2764 | // derived-to-base Conversion (13.3.3.1). |
| 2765 | ICS.setStandard(); |
| 2766 | ICS.Standard.First = ICK_Identity; |
| 2767 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2768 | ICS.Standard.Third = ICK_Identity; |
| 2769 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2770 | ICS.Standard.setToType(0, T2); |
| 2771 | ICS.Standard.setToType(1, T1); |
| 2772 | ICS.Standard.setToType(2, T1); |
| 2773 | ICS.Standard.ReferenceBinding = true; |
| 2774 | ICS.Standard.DirectBinding = true; |
| 2775 | ICS.Standard.RRefBinding = isRValRef; |
| 2776 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2777 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2778 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2779 | // derived-to-base conversions is suppressed when we're |
| 2780 | // computing the implicit conversion sequence (C++ |
| 2781 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2782 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2783 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2784 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2785 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2786 | // not reference-related to T2, and can be implicitly |
| 2787 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2788 | // is reference-compatible with "cv3 T3" 92) (this |
| 2789 | // conversion is selected by enumerating the applicable |
| 2790 | // conversion functions (13.3.1.6) and choosing the best |
| 2791 | // one through overload resolution (13.3)), |
| 2792 | if (!SuppressUserConversions && T2->isRecordType() && |
| 2793 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2794 | RefRelationship == Sema::Ref_Incompatible) { |
| 2795 | if (FindConversionToLValue(S, ICS, DeclType, DeclLoc, |
| 2796 | Init, T2, AllowExplicit)) |
| 2797 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2798 | } |
| 2799 | } |
| 2800 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2801 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 2802 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2803 | // shall be an rvalue reference and the initializer expression shall be |
| 2804 | // an rvalue or have a function type. |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2805 | // |
| 2806 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2807 | // point, which is that, due to p2 (which short-circuits reference |
| 2808 | // binding by only attempting a simple conversion for non-direct |
| 2809 | // bindings) and p3's strange wording, we allow a const volatile |
| 2810 | // reference to bind to an rvalue. Hence the check for the presence |
| 2811 | // of "const" rather than checking for "const" being the only |
| 2812 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2813 | // This is also the point where rvalue references and lvalue inits no longer |
| 2814 | // go together. |
| 2815 | if ((!isRValRef && !T1.isConstQualified()) || |
| 2816 | (isRValRef && InitCategory.isLValue())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2817 | return ICS; |
| 2818 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2819 | // -- If T1 is a function type, then |
| 2820 | // -- if T2 is the same type as T1, the reference is bound to the |
| 2821 | // initializer expression lvalue; |
| 2822 | // -- if T2 is a class type and the initializer expression can be |
| 2823 | // implicitly converted to an lvalue of type T1 [...], the |
| 2824 | // reference is bound to the function lvalue that is the result |
| 2825 | // of the conversion; |
| 2826 | // This is the same as for the lvalue case above, so it was handled there. |
| 2827 | // -- otherwise, the program is ill-formed. |
| 2828 | // This is the one difference to the lvalue case. |
| 2829 | if (T1->isFunctionType()) |
| 2830 | return ICS; |
| 2831 | |
| 2832 | // -- Otherwise, if T2 is a class type and |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2833 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2834 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2835 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2836 | // -- T1 is not reference-related to T2 and the initializer |
| 2837 | // expression can be implicitly converted to an rvalue |
| 2838 | // of type "cv3 T3" (this conversion is selected by |
| 2839 | // enumerating the applicable conversion functions |
| 2840 | // (13.3.1.6) and choosing the best one through overload |
| 2841 | // resolution (13.3)), |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2842 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2843 | // then the reference is bound to the initializer |
| 2844 | // expression rvalue in the first case and to the object |
| 2845 | // that is the result of the conversion in the second case |
| 2846 | // (or, in either case, to the appropriate base class |
| 2847 | // subobject of the object). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2848 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2849 | // We're only checking the first case here, which is a direct |
| 2850 | // binding in C++0x but not in C++03. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2851 | if (InitCategory.isRValue() && T2->isRecordType() && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2852 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2853 | ICS.setStandard(); |
| 2854 | ICS.Standard.First = ICK_Identity; |
| 2855 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2856 | ICS.Standard.Third = ICK_Identity; |
| 2857 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2858 | ICS.Standard.setToType(0, T2); |
| 2859 | ICS.Standard.setToType(1, T1); |
| 2860 | ICS.Standard.setToType(2, T1); |
| 2861 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2862 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2863 | ICS.Standard.RRefBinding = isRValRef; |
| 2864 | ICS.Standard.CopyConstructor = 0; |
| 2865 | return ICS; |
| 2866 | } |
| 2867 | |
| 2868 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2869 | // initialized from the initializer expression using the |
| 2870 | // rules for a non-reference copy initialization (8.5). The |
| 2871 | // reference is then bound to the temporary. If T1 is |
| 2872 | // reference-related to T2, cv1 must be the same |
| 2873 | // cv-qualification as, or greater cv-qualification than, |
| 2874 | // cv2; otherwise, the program is ill-formed. |
| 2875 | if (RefRelationship == Sema::Ref_Related) { |
| 2876 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2877 | // we would be reference-compatible or reference-compatible with |
| 2878 | // added qualification. But that wasn't the case, so the reference |
| 2879 | // initialization fails. |
| 2880 | return ICS; |
| 2881 | } |
| 2882 | |
| 2883 | // If at least one of the types is a class type, the types are not |
| 2884 | // related, and we aren't allowed any user conversions, the |
| 2885 | // reference binding fails. This case is important for breaking |
| 2886 | // recursion, since TryImplicitConversion below will attempt to |
| 2887 | // create a temporary through the use of a copy constructor. |
| 2888 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 2889 | (T1->isRecordType() || T2->isRecordType())) |
| 2890 | return ICS; |
| 2891 | |
| 2892 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2893 | // When a parameter of reference type is not bound directly to |
| 2894 | // an argument expression, the conversion sequence is the one |
| 2895 | // required to convert the argument expression to the |
| 2896 | // underlying type of the reference according to |
| 2897 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 2898 | // to copy-initializing a temporary of the underlying type with |
| 2899 | // the argument expression. Any difference in top-level |
| 2900 | // cv-qualification is subsumed by the initialization itself |
| 2901 | // and does not constitute a conversion. |
| 2902 | ICS = S.TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 2903 | /*AllowExplicit=*/false, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2904 | /*InOverloadResolution=*/false); |
| 2905 | |
| 2906 | // Of course, that's still a reference binding. |
| 2907 | if (ICS.isStandard()) { |
| 2908 | ICS.Standard.ReferenceBinding = true; |
| 2909 | ICS.Standard.RRefBinding = isRValRef; |
| 2910 | } else if (ICS.isUserDefined()) { |
| 2911 | ICS.UserDefined.After.ReferenceBinding = true; |
| 2912 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 2913 | } |
| 2914 | return ICS; |
| 2915 | } |
| 2916 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2917 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2918 | /// ToType from the expression From. Return the implicit conversion |
| 2919 | /// sequence required to pass this argument, which may be a bad |
| 2920 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2921 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 2922 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2923 | static ImplicitConversionSequence |
| 2924 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | b7f9e6a | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 2925 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2926 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2927 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2928 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2929 | /*FIXME:*/From->getLocStart(), |
| 2930 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2931 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2932 | |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2933 | return S.TryImplicitConversion(From, ToType, |
| 2934 | SuppressUserConversions, |
| 2935 | /*AllowExplicit=*/false, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2936 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2939 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2940 | /// parameter of the given member function (@c Method) from the |
| 2941 | /// expression @p From. |
| 2942 | ImplicitConversionSequence |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2943 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2944 | CXXMethodDecl *Method, |
| 2945 | CXXRecordDecl *ActingContext) { |
| 2946 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2947 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2948 | // const volatile object. |
| 2949 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2950 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2951 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2952 | |
| 2953 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2954 | // to exit early. |
| 2955 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2956 | |
| 2957 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2958 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2959 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2960 | FromType = PT->getPointeeType(); |
| 2961 | |
| 2962 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2963 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2964 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2965 | // where X is the class of which the function is a member |
| 2966 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2967 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2969 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2970 | // reference binding here, that allows class rvalues to bind to |
| 2971 | // non-constant references. |
| 2972 | |
| 2973 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2974 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2975 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2976 | if (ImplicitParamType.getCVRQualifiers() |
| 2977 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2978 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2979 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2980 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2981 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2982 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2983 | |
| 2984 | // Check that we have either the same type or a derived type. It |
| 2985 | // affects the conversion rank. |
| 2986 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2987 | ImplicitConversionKind SecondKind; |
| 2988 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2989 | SecondKind = ICK_Identity; |
| 2990 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2991 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2992 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2993 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2994 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2995 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2996 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2997 | |
| 2998 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2999 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3000 | ICS.Standard.setAsIdentityConversion(); |
| 3001 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3002 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3003 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3004 | ICS.Standard.ReferenceBinding = true; |
| 3005 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 3006 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3007 | return ICS; |
| 3008 | } |
| 3009 | |
| 3010 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3011 | /// the implicit object parameter for the given Method with the given |
| 3012 | /// expression. |
| 3013 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3014 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 3015 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3016 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3017 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3018 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3020 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3021 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3022 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3023 | FromRecordType = PT->getPointeeType(); |
| 3024 | DestType = Method->getThisType(Context); |
| 3025 | } else { |
| 3026 | FromRecordType = From->getType(); |
| 3027 | DestType = ImplicitParamRecordType; |
| 3028 | } |
| 3029 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3030 | // Note that we always use the true parent context when performing |
| 3031 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | ImplicitConversionSequence ICS |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3033 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 3034 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3035 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3036 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3037 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3038 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3040 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3041 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3043 | if (!Context.hasSameType(From->getType(), DestType)) |
Anders Carlsson | f1b48b7 | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3044 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
| 3045 | /*isLvalue=*/!From->getType()->isPointerType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3046 | return false; |
| 3047 | } |
| 3048 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3049 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3050 | /// expression From to bool (C++0x [conv]p3). |
| 3051 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3052 | // FIXME: This is pretty broken. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3054 | // FIXME: Are these flags correct? |
| 3055 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3057 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3061 | /// of the expression From to bool (C++0x [conv]p3). |
| 3062 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 3063 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3064 | if (!ICS.isBad()) |
| 3065 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3066 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3067 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3068 | return Diag(From->getSourceRange().getBegin(), |
| 3069 | diag::err_typecheck_bool_condition) |
| 3070 | << From->getType() << From->getSourceRange(); |
| 3071 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3072 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3073 | |
| 3074 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3075 | /// expression From to 'id'. |
| 3076 | ImplicitConversionSequence Sema::TryContextuallyConvertToObjCId(Expr *From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3077 | QualType Ty = Context.getObjCIdType(); |
| 3078 | return TryImplicitConversion(From, Ty, |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3079 | // FIXME: Are these flags correct? |
| 3080 | /*SuppressUserConversions=*/false, |
| 3081 | /*AllowExplicit=*/true, |
| 3082 | /*InOverloadResolution=*/false); |
| 3083 | } |
| 3084 | |
| 3085 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3086 | /// of the expression From to 'id'. |
| 3087 | bool Sema::PerformContextuallyConvertToObjCId(Expr *&From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3088 | QualType Ty = Context.getObjCIdType(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3089 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(From); |
| 3090 | if (!ICS.isBad()) |
| 3091 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| 3092 | return true; |
| 3093 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3094 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3095 | /// \brief Attempt to convert the given expression to an integral or |
| 3096 | /// enumeration type. |
| 3097 | /// |
| 3098 | /// This routine will attempt to convert an expression of class type to an |
| 3099 | /// integral or enumeration type, if that class type only has a single |
| 3100 | /// conversion to an integral or enumeration type. |
| 3101 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3102 | /// \param Loc The source location of the construct that requires the |
| 3103 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3104 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3105 | /// \param FromE The expression we're converting from. |
| 3106 | /// |
| 3107 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3108 | /// have integral or enumeration type. |
| 3109 | /// |
| 3110 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3111 | /// incomplete class type. |
| 3112 | /// |
| 3113 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3114 | /// explicit conversion function (because no implicit conversion functions |
| 3115 | /// were available). This is a recovery mode. |
| 3116 | /// |
| 3117 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3118 | /// showing which conversion was picked. |
| 3119 | /// |
| 3120 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3121 | /// conversion function that could convert to integral or enumeration type. |
| 3122 | /// |
| 3123 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
| 3124 | /// usable conversion function. |
| 3125 | /// |
| 3126 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3127 | /// function, which may be an extension in this case. |
| 3128 | /// |
| 3129 | /// \returns The expression, converted to an integral or enumeration type if |
| 3130 | /// successful. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3131 | Sema::OwningExprResult |
| 3132 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, ExprArg FromE, |
| 3133 | const PartialDiagnostic &NotIntDiag, |
| 3134 | const PartialDiagnostic &IncompleteDiag, |
| 3135 | const PartialDiagnostic &ExplicitConvDiag, |
| 3136 | const PartialDiagnostic &ExplicitConvNote, |
| 3137 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3138 | const PartialDiagnostic &AmbigNote, |
| 3139 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3140 | Expr *From = static_cast<Expr *>(FromE.get()); |
| 3141 | |
| 3142 | // We can't perform any more checking for type-dependent expressions. |
| 3143 | if (From->isTypeDependent()) |
| 3144 | return move(FromE); |
| 3145 | |
| 3146 | // If the expression already has integral or enumeration type, we're golden. |
| 3147 | QualType T = From->getType(); |
| 3148 | if (T->isIntegralOrEnumerationType()) |
| 3149 | return move(FromE); |
| 3150 | |
| 3151 | // FIXME: Check for missing '()' if T is a function type? |
| 3152 | |
| 3153 | // If we don't have a class type in C++, there's no way we can get an |
| 3154 | // expression of integral or enumeration type. |
| 3155 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3156 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3157 | Diag(Loc, NotIntDiag) |
| 3158 | << T << From->getSourceRange(); |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3159 | return move(FromE); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | // We must have a complete class type. |
| 3163 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3164 | return move(FromE); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3165 | |
| 3166 | // Look for a conversion to an integral or enumeration type. |
| 3167 | UnresolvedSet<4> ViableConversions; |
| 3168 | UnresolvedSet<4> ExplicitConversions; |
| 3169 | const UnresolvedSetImpl *Conversions |
| 3170 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
| 3171 | |
| 3172 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3173 | E = Conversions->end(); |
| 3174 | I != E; |
| 3175 | ++I) { |
| 3176 | if (CXXConversionDecl *Conversion |
| 3177 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3178 | if (Conversion->getConversionType().getNonReferenceType() |
| 3179 | ->isIntegralOrEnumerationType()) { |
| 3180 | if (Conversion->isExplicit()) |
| 3181 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3182 | else |
| 3183 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | switch (ViableConversions.size()) { |
| 3188 | case 0: |
| 3189 | if (ExplicitConversions.size() == 1) { |
| 3190 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3191 | CXXConversionDecl *Conversion |
| 3192 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3193 | |
| 3194 | // The user probably meant to invoke the given explicit |
| 3195 | // conversion; use it. |
| 3196 | QualType ConvTy |
| 3197 | = Conversion->getConversionType().getNonReferenceType(); |
| 3198 | std::string TypeStr; |
| 3199 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
| 3200 | |
| 3201 | Diag(Loc, ExplicitConvDiag) |
| 3202 | << T << ConvTy |
| 3203 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3204 | "static_cast<" + TypeStr + ">(") |
| 3205 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3206 | ")"); |
| 3207 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3208 | << ConvTy->isEnumeralType() << ConvTy; |
| 3209 | |
| 3210 | // If we aren't in a SFINAE context, build a call to the |
| 3211 | // explicit conversion function. |
| 3212 | if (isSFINAEContext()) |
| 3213 | return ExprError(); |
| 3214 | |
| 3215 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 3216 | From = BuildCXXMemberCallExpr(FromE.takeAs<Expr>(), Found, Conversion); |
| 3217 | FromE = Owned(From); |
| 3218 | } |
| 3219 | |
| 3220 | // We'll complain below about a non-integral condition type. |
| 3221 | break; |
| 3222 | |
| 3223 | case 1: { |
| 3224 | // Apply this conversion. |
| 3225 | DeclAccessPair Found = ViableConversions[0]; |
| 3226 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3227 | |
| 3228 | CXXConversionDecl *Conversion |
| 3229 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3230 | QualType ConvTy |
| 3231 | = Conversion->getConversionType().getNonReferenceType(); |
| 3232 | if (ConvDiag.getDiagID()) { |
| 3233 | if (isSFINAEContext()) |
| 3234 | return ExprError(); |
| 3235 | |
| 3236 | Diag(Loc, ConvDiag) |
| 3237 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3238 | } |
| 3239 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3240 | From = BuildCXXMemberCallExpr(FromE.takeAs<Expr>(), Found, |
| 3241 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
| 3242 | FromE = Owned(From); |
| 3243 | break; |
| 3244 | } |
| 3245 | |
| 3246 | default: |
| 3247 | Diag(Loc, AmbigDiag) |
| 3248 | << T << From->getSourceRange(); |
| 3249 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3250 | CXXConversionDecl *Conv |
| 3251 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3252 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3253 | Diag(Conv->getLocation(), AmbigNote) |
| 3254 | << ConvTy->isEnumeralType() << ConvTy; |
| 3255 | } |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3256 | return move(FromE); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3259 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3260 | Diag(Loc, NotIntDiag) |
| 3261 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3262 | |
| 3263 | return move(FromE); |
| 3264 | } |
| 3265 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3266 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3267 | /// candidate functions, using the given function call arguments. If |
| 3268 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3269 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3270 | /// |
| 3271 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3272 | /// based on an incomplete set of function arguments. This feature is used by |
| 3273 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | void |
| 3275 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3276 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3277 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3278 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3279 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3280 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3282 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3283 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3285 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3287 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3288 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3289 | // If we get here, it's because we're calling a member function |
| 3290 | // that is named without a member access expression (e.g., |
| 3291 | // "this->f") that was either written explicitly or created |
| 3292 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3293 | // function, e.g., X::f(). We use an empty type for the implied |
| 3294 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3295 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3296 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3297 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3298 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3299 | return; |
| 3300 | } |
| 3301 | // We treat a constructor like a non-member function, since its object |
| 3302 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3305 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3306 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3308 | // Overload resolution is always an unevaluated context. |
| 3309 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3310 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3311 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3312 | // C++ [class.copy]p3: |
| 3313 | // A member function template is never instantiated to perform the copy |
| 3314 | // of a class object to an object of its class type. |
| 3315 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 3316 | if (NumArgs == 1 && |
| 3317 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3318 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3319 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3320 | return; |
| 3321 | } |
| 3322 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3323 | // Add this candidate |
| 3324 | CandidateSet.push_back(OverloadCandidate()); |
| 3325 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3326 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3327 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3328 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3329 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3330 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3331 | |
| 3332 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3333 | |
| 3334 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3335 | // parameters is viable only if it has an ellipsis in its parameter |
| 3336 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3337 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 3338 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3339 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3340 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3341 | return; |
| 3342 | } |
| 3343 | |
| 3344 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3345 | // is viable only if the (m+1)st parameter has a default argument |
| 3346 | // (8.3.6). For the purposes of overload resolution, the |
| 3347 | // parameter list is truncated on the right, so that there are |
| 3348 | // exactly m parameters. |
| 3349 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3350 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3351 | // Not enough arguments. |
| 3352 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3353 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3354 | return; |
| 3355 | } |
| 3356 | |
| 3357 | // Determine the implicit conversion sequences for each of the |
| 3358 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3359 | Candidate.Conversions.resize(NumArgs); |
| 3360 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3361 | if (ArgIdx < NumArgsInProto) { |
| 3362 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3363 | // exist for each argument an implicit conversion sequence |
| 3364 | // (13.3.3.1) that converts that argument to the corresponding |
| 3365 | // parameter of F. |
| 3366 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3368 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3369 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3370 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3371 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3372 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3373 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3374 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3375 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3376 | } else { |
| 3377 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3378 | // argument for which there is no corresponding parameter is |
| 3379 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3380 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | } |
| 3384 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3385 | /// \brief Add all of the function declarations in the given function set to |
| 3386 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3387 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3388 | Expr **Args, unsigned NumArgs, |
| 3389 | OverloadCandidateSet& CandidateSet, |
| 3390 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3391 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3392 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3393 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3394 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3395 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3396 | cast<CXXMethodDecl>(FD)->getParent(), |
| 3397 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3398 | CandidateSet, SuppressUserConversions); |
| 3399 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3400 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3401 | SuppressUserConversions); |
| 3402 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3403 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3404 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3405 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3406 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3407 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3408 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3409 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3410 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3411 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3412 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3413 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3414 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3415 | Args, NumArgs, CandidateSet, |
| 3416 | SuppressUserConversions); |
| 3417 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3418 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3421 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3422 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3423 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3424 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3425 | Expr **Args, unsigned NumArgs, |
| 3426 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3427 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3428 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3429 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3430 | |
| 3431 | if (isa<UsingShadowDecl>(Decl)) |
| 3432 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 3433 | |
| 3434 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3435 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3436 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3437 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3438 | /*ExplicitArgs*/ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3439 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3440 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3441 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3442 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3443 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3444 | ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3445 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3446 | } |
| 3447 | } |
| 3448 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3449 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3450 | /// of candidate functions, using the given function call arguments |
| 3451 | /// and the object argument (@c Object). For example, in a call |
| 3452 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 3453 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 3454 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3455 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3457 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3458 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 3459 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3460 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3461 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3463 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3464 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3465 | assert(!isa<CXXConstructorDecl>(Method) && |
| 3466 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3468 | if (!CandidateSet.isNewCandidate(Method)) |
| 3469 | return; |
| 3470 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3471 | // Overload resolution is always an unevaluated context. |
| 3472 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3473 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3474 | // Add this candidate |
| 3475 | CandidateSet.push_back(OverloadCandidate()); |
| 3476 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3477 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3478 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3479 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3480 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3481 | |
| 3482 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3483 | |
| 3484 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3485 | // parameters is viable only if it has an ellipsis in its parameter |
| 3486 | // list (8.3.5). |
| 3487 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3488 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3489 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3490 | return; |
| 3491 | } |
| 3492 | |
| 3493 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3494 | // is viable only if the (m+1)st parameter has a default argument |
| 3495 | // (8.3.6). For the purposes of overload resolution, the |
| 3496 | // parameter list is truncated on the right, so that there are |
| 3497 | // exactly m parameters. |
| 3498 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 3499 | if (NumArgs < MinRequiredArgs) { |
| 3500 | // Not enough arguments. |
| 3501 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3502 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3503 | return; |
| 3504 | } |
| 3505 | |
| 3506 | Candidate.Viable = true; |
| 3507 | Candidate.Conversions.resize(NumArgs + 1); |
| 3508 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3509 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3510 | // The implicit object argument is ignored. |
| 3511 | Candidate.IgnoreObjectArgument = true; |
| 3512 | else { |
| 3513 | // Determine the implicit conversion sequence for the object |
| 3514 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3515 | Candidate.Conversions[0] |
| 3516 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3517 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3518 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3519 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3520 | return; |
| 3521 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | // Determine the implicit conversion sequences for each of the |
| 3525 | // arguments. |
| 3526 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3527 | if (ArgIdx < NumArgsInProto) { |
| 3528 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3529 | // exist for each argument an implicit conversion sequence |
| 3530 | // (13.3.3.1) that converts that argument to the corresponding |
| 3531 | // parameter of F. |
| 3532 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3534 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3535 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3536 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3537 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3538 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3539 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3540 | break; |
| 3541 | } |
| 3542 | } else { |
| 3543 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3544 | // argument for which there is no corresponding parameter is |
| 3545 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3546 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3547 | } |
| 3548 | } |
| 3549 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3551 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 3552 | /// set, using template argument deduction to produce an appropriate member |
| 3553 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3555 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3556 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3557 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3558 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3559 | QualType ObjectType, |
| 3560 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3561 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3562 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3563 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3564 | return; |
| 3565 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3566 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3568 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3569 | // 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] | 3570 | // candidate functions in the usual way.113) A given name can refer to one |
| 3571 | // or more function templates and also to a set of overloaded non-template |
| 3572 | // functions. In such a case, the candidate functions generated from each |
| 3573 | // function template are combined with the set of non-template candidate |
| 3574 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3575 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3576 | FunctionDecl *Specialization = 0; |
| 3577 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3578 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3579 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3580 | CandidateSet.push_back(OverloadCandidate()); |
| 3581 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3582 | Candidate.FoundDecl = FoundDecl; |
| 3583 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 3584 | Candidate.Viable = false; |
| 3585 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3586 | Candidate.IsSurrogate = false; |
| 3587 | Candidate.IgnoreObjectArgument = false; |
| 3588 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3589 | Info); |
| 3590 | return; |
| 3591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3593 | // Add the function template specialization produced by template argument |
| 3594 | // deduction as a candidate. |
| 3595 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3597 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3598 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3599 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3600 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3603 | /// \brief Add a C++ function template specialization as a candidate |
| 3604 | /// in the candidate set, using template argument deduction to produce |
| 3605 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3607 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3608 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3609 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3610 | Expr **Args, unsigned NumArgs, |
| 3611 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3612 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3613 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3614 | return; |
| 3615 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3616 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3618 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | // 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] | 3620 | // candidate functions in the usual way.113) A given name can refer to one |
| 3621 | // or more function templates and also to a set of overloaded non-template |
| 3622 | // functions. In such a case, the candidate functions generated from each |
| 3623 | // function template are combined with the set of non-template candidate |
| 3624 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3625 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3626 | FunctionDecl *Specialization = 0; |
| 3627 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3628 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3629 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3630 | CandidateSet.push_back(OverloadCandidate()); |
| 3631 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3632 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3633 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3634 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3635 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3636 | Candidate.IsSurrogate = false; |
| 3637 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3638 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3639 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3640 | return; |
| 3641 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3642 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3643 | // Add the function template specialization produced by template argument |
| 3644 | // deduction as a candidate. |
| 3645 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3646 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3647 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3650 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3652 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | /// 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] | 3654 | /// (which may or may not be the same type as the type that the |
| 3655 | /// conversion function produces). |
| 3656 | void |
| 3657 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3658 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3659 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3660 | Expr *From, QualType ToType, |
| 3661 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3662 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3663 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3664 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3665 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3666 | return; |
| 3667 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3668 | // Overload resolution is always an unevaluated context. |
| 3669 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3670 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3671 | // Add this candidate |
| 3672 | CandidateSet.push_back(OverloadCandidate()); |
| 3673 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3674 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3675 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3676 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3677 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3678 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3679 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3680 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3681 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3682 | // Determine the implicit conversion sequence for the implicit |
| 3683 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3684 | Candidate.Viable = true; |
| 3685 | Candidate.Conversions.resize(1); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3686 | Candidate.Conversions[0] |
| 3687 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 3688 | ActingContext); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3689 | // Conversion functions to a different type in the base class is visible in |
| 3690 | // the derived class. So, a derived to base conversion should not participate |
| 3691 | // in overload resolution. |
| 3692 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 3693 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3694 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3695 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3696 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3697 | return; |
| 3698 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3699 | |
| 3700 | // We won't go through a user-define type conversion function to convert a |
| 3701 | // derived to base as such conversions are given Conversion Rank. They only |
| 3702 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3703 | QualType FromCanon |
| 3704 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3705 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3706 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3707 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3708 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3709 | return; |
| 3710 | } |
| 3711 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3712 | // To determine what the conversion from the result of calling the |
| 3713 | // conversion function to the type we're eventually trying to |
| 3714 | // convert to (ToType), we need to synthesize a call to the |
| 3715 | // conversion function and attempt copy initialization from it. This |
| 3716 | // makes sure that we get the right semantics with respect to |
| 3717 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3718 | // call on the stack and we don't need its arguments to be |
| 3719 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3721 | From->getLocStart()); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3722 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3723 | CastExpr::CK_FunctionToPointerDecay, |
Anders Carlsson | f1b48b7 | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3724 | &ConversionRef, CXXBaseSpecifierArray(), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | |
| 3726 | // 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] | 3727 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3728 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3730 | Conversion->getConversionType().getNonLValueExprType(Context), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3731 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3733 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3734 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3735 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3737 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3738 | case ImplicitConversionSequence::StandardConversion: |
| 3739 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3740 | |
| 3741 | // C++ [over.ics.user]p3: |
| 3742 | // If the user-defined conversion is specified by a specialization of a |
| 3743 | // conversion function template, the second standard conversion sequence |
| 3744 | // shall have exact match rank. |
| 3745 | if (Conversion->getPrimaryTemplate() && |
| 3746 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3747 | Candidate.Viable = false; |
| 3748 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3749 | } |
| 3750 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3751 | break; |
| 3752 | |
| 3753 | case ImplicitConversionSequence::BadConversion: |
| 3754 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3755 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3756 | break; |
| 3757 | |
| 3758 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3760 | "Can only end up with a standard conversion sequence or failure"); |
| 3761 | } |
| 3762 | } |
| 3763 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3764 | /// \brief Adds a conversion function template specialization |
| 3765 | /// candidate to the overload set, using template argument deduction |
| 3766 | /// to deduce the template arguments of the conversion function |
| 3767 | /// template from the type that we are converting to (C++ |
| 3768 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3770 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3771 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3772 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3773 | Expr *From, QualType ToType, |
| 3774 | OverloadCandidateSet &CandidateSet) { |
| 3775 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3776 | "Only conversion function templates permitted here"); |
| 3777 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3778 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3779 | return; |
| 3780 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3781 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3782 | CXXConversionDecl *Specialization = 0; |
| 3783 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3785 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3786 | CandidateSet.push_back(OverloadCandidate()); |
| 3787 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3788 | Candidate.FoundDecl = FoundDecl; |
| 3789 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3790 | Candidate.Viable = false; |
| 3791 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3792 | Candidate.IsSurrogate = false; |
| 3793 | Candidate.IgnoreObjectArgument = false; |
| 3794 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3795 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3796 | return; |
| 3797 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3799 | // Add the conversion function template specialization produced by |
| 3800 | // template argument deduction as a candidate. |
| 3801 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3802 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3803 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3806 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3807 | /// converts the given @c Object to a function pointer via the |
| 3808 | /// conversion function @c Conversion, and then attempts to call it |
| 3809 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3810 | /// the type of function that we'll eventually be calling. |
| 3811 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3812 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3813 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3814 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3815 | QualType ObjectType, |
| 3816 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3817 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3818 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3819 | return; |
| 3820 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3821 | // Overload resolution is always an unevaluated context. |
| 3822 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3823 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3824 | CandidateSet.push_back(OverloadCandidate()); |
| 3825 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3826 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3827 | Candidate.Function = 0; |
| 3828 | Candidate.Surrogate = Conversion; |
| 3829 | Candidate.Viable = true; |
| 3830 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3831 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3832 | Candidate.Conversions.resize(NumArgs + 1); |
| 3833 | |
| 3834 | // Determine the implicit conversion sequence for the implicit |
| 3835 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | ImplicitConversionSequence ObjectInit |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3837 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3838 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3839 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3840 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3841 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3842 | return; |
| 3843 | } |
| 3844 | |
| 3845 | // The first conversion is actually a user-defined conversion whose |
| 3846 | // first conversion is ObjectInit's standard conversion (which is |
| 3847 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3848 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3849 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3850 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3851 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3853 | = Candidate.Conversions[0].UserDefined.Before; |
| 3854 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3855 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3857 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3858 | |
| 3859 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3860 | // parameters is viable only if it has an ellipsis in its parameter |
| 3861 | // list (8.3.5). |
| 3862 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3863 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3864 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3865 | return; |
| 3866 | } |
| 3867 | |
| 3868 | // Function types don't have any default arguments, so just check if |
| 3869 | // we have enough arguments. |
| 3870 | if (NumArgs < NumArgsInProto) { |
| 3871 | // Not enough arguments. |
| 3872 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3873 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3874 | return; |
| 3875 | } |
| 3876 | |
| 3877 | // Determine the implicit conversion sequences for each of the |
| 3878 | // arguments. |
| 3879 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3880 | if (ArgIdx < NumArgsInProto) { |
| 3881 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3882 | // exist for each argument an implicit conversion sequence |
| 3883 | // (13.3.3.1) that converts that argument to the corresponding |
| 3884 | // parameter of F. |
| 3885 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3887 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3888 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3889 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3890 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3891 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3892 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3893 | break; |
| 3894 | } |
| 3895 | } else { |
| 3896 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3897 | // argument for which there is no corresponding parameter is |
| 3898 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3899 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | } |
| 3903 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3904 | /// \brief Add overload candidates for overloaded operators that are |
| 3905 | /// member functions. |
| 3906 | /// |
| 3907 | /// Add the overloaded operator candidates that are member functions |
| 3908 | /// for the operator Op that was used in an operator expression such |
| 3909 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3910 | /// CandidateSet will store the added overload candidates. (C++ |
| 3911 | /// [over.match.oper]). |
| 3912 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3913 | SourceLocation OpLoc, |
| 3914 | Expr **Args, unsigned NumArgs, |
| 3915 | OverloadCandidateSet& CandidateSet, |
| 3916 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3917 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3918 | |
| 3919 | // C++ [over.match.oper]p3: |
| 3920 | // For a unary operator @ with an operand of a type whose |
| 3921 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3922 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3923 | // a right operand of a type whose cv-unqualified version is T2, |
| 3924 | // three sets of candidate functions, designated member |
| 3925 | // candidates, non-member candidates and built-in candidates, are |
| 3926 | // constructed as follows: |
| 3927 | QualType T1 = Args[0]->getType(); |
| 3928 | QualType T2; |
| 3929 | if (NumArgs > 1) |
| 3930 | T2 = Args[1]->getType(); |
| 3931 | |
| 3932 | // -- If T1 is a class type, the set of member candidates is the |
| 3933 | // result of the qualified lookup of T1::operator@ |
| 3934 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3935 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3936 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3937 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3938 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3939 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3941 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3942 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3943 | Operators.suppressDiagnostics(); |
| 3944 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3946 | OperEnd = Operators.end(); |
| 3947 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3948 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3949 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3950 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3951 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3952 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3955 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3956 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3957 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3958 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3959 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3960 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3961 | /// (at the beginning of the argument list) that will be contextually |
| 3962 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3963 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3964 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3965 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3966 | bool IsAssignmentOperator, |
| 3967 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3968 | // Overload resolution is always an unevaluated context. |
| 3969 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3970 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3971 | // Add this candidate |
| 3972 | CandidateSet.push_back(OverloadCandidate()); |
| 3973 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3974 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3975 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3976 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3977 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3978 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3979 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3980 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3981 | |
| 3982 | // Determine the implicit conversion sequences for each of the |
| 3983 | // arguments. |
| 3984 | Candidate.Viable = true; |
| 3985 | Candidate.Conversions.resize(NumArgs); |
| 3986 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3987 | // C++ [over.match.oper]p4: |
| 3988 | // For the built-in assignment operators, conversions of the |
| 3989 | // left operand are restricted as follows: |
| 3990 | // -- no temporaries are introduced to hold the left operand, and |
| 3991 | // -- no user-defined conversions are applied to the left |
| 3992 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3994 | // |
| 3995 | // We block these conversions by turning off user-defined |
| 3996 | // conversions, since that is the only way that initialization of |
| 3997 | // a reference to a non-class type can occur from something that |
| 3998 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3999 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4001 | "Contextual conversion to bool requires bool type"); |
| 4002 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 4003 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4005 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4006 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4007 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4008 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4009 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4010 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4011 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4012 | break; |
| 4013 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4018 | /// candidate operator functions for built-in operators (C++ |
| 4019 | /// [over.built]). The types are separated into pointer types and |
| 4020 | /// enumeration types. |
| 4021 | class BuiltinCandidateTypeSet { |
| 4022 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4023 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4024 | |
| 4025 | /// PointerTypes - The set of pointer types that will be used in the |
| 4026 | /// built-in candidates. |
| 4027 | TypeSet PointerTypes; |
| 4028 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4029 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4030 | /// used in the built-in candidates. |
| 4031 | TypeSet MemberPointerTypes; |
| 4032 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4033 | /// EnumerationTypes - The set of enumeration types that will be |
| 4034 | /// used in the built-in candidates. |
| 4035 | TypeSet EnumerationTypes; |
| 4036 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4037 | /// \brief The set of vector types that will be used in the built-in |
| 4038 | /// candidates. |
| 4039 | TypeSet VectorTypes; |
| 4040 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4041 | /// Sema - The semantic analysis instance where we are building the |
| 4042 | /// candidate type set. |
| 4043 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4045 | /// Context - The AST context in which we will build the type sets. |
| 4046 | ASTContext &Context; |
| 4047 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4048 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4049 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4050 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4051 | |
| 4052 | public: |
| 4053 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4054 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4055 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4057 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4058 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4059 | void AddTypesConvertedFrom(QualType Ty, |
| 4060 | SourceLocation Loc, |
| 4061 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4062 | bool AllowExplicitConversions, |
| 4063 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4064 | |
| 4065 | /// pointer_begin - First pointer type found; |
| 4066 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4067 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4068 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4069 | iterator pointer_end() { return PointerTypes.end(); } |
| 4070 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4071 | /// member_pointer_begin - First member pointer type found; |
| 4072 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4073 | |
| 4074 | /// member_pointer_end - Past the last member pointer type found; |
| 4075 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4076 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4077 | /// enumeration_begin - First enumeration type found; |
| 4078 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4079 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4080 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4081 | iterator enumeration_end() { return EnumerationTypes.end(); } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4082 | |
| 4083 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4084 | iterator vector_end() { return VectorTypes.end(); } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4085 | }; |
| 4086 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4087 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4088 | /// the set of pointer types along with any more-qualified variants of |
| 4089 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4090 | /// will add "int const *", "int const volatile *", "int const |
| 4091 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4092 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4093 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4094 | /// |
| 4095 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4096 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4097 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4098 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4099 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4100 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4101 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4102 | return false; |
| 4103 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4104 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 4105 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4106 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4107 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4108 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4109 | // (the qualifier would sink to the element type), and for another, the |
| 4110 | // only overload situation where it matters is subscript or pointer +- int, |
| 4111 | // and those shouldn't have qualifier variants anyway. |
| 4112 | if (PointeeTy->isArrayType()) |
| 4113 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4114 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4115 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4116 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4117 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4118 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 4119 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4120 | // Iterate through all strict supersets of BaseCVR. |
| 4121 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4122 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4123 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4124 | // in the types. |
| 4125 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4126 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4127 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 4128 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | return true; |
| 4132 | } |
| 4133 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4134 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4135 | /// to the set of pointer types along with any more-qualified variants of |
| 4136 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4137 | /// will add "int const *", "int const volatile *", "int const |
| 4138 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4139 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4140 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4141 | /// |
| 4142 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4143 | bool |
| 4144 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4145 | QualType Ty) { |
| 4146 | // Insert this type. |
| 4147 | if (!MemberPointerTypes.insert(Ty)) |
| 4148 | return false; |
| 4149 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4150 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4151 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4152 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4153 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4154 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4155 | // (the qualifier would sink to the element type), and for another, the |
| 4156 | // only overload situation where it matters is subscript or pointer +- int, |
| 4157 | // and those shouldn't have qualifier variants anyway. |
| 4158 | if (PointeeTy->isArrayType()) |
| 4159 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4160 | const Type *ClassTy = PointerTy->getClass(); |
| 4161 | |
| 4162 | // Iterate through all strict supersets of the pointee type's CVR |
| 4163 | // qualifiers. |
| 4164 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4165 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4166 | if ((CVR | BaseCVR) != CVR) continue; |
| 4167 | |
| 4168 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 4169 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | return true; |
| 4173 | } |
| 4174 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4175 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4176 | /// 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] | 4177 | /// primarily interested in pointer types and enumeration types. We also |
| 4178 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4179 | /// AllowUserConversions is true if we should look at the conversion |
| 4180 | /// functions of a class type, and AllowExplicitConversions if we |
| 4181 | /// should also include the explicit conversion functions of a class |
| 4182 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4184 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4185 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4186 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4187 | bool AllowExplicitConversions, |
| 4188 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4189 | // Only deal with canonical types. |
| 4190 | Ty = Context.getCanonicalType(Ty); |
| 4191 | |
| 4192 | // Look through reference types; they aren't part of the type of an |
| 4193 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4194 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4195 | Ty = RefTy->getPointeeType(); |
| 4196 | |
| 4197 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4198 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4199 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 4200 | // If we're dealing with an array type, decay to the pointer. |
| 4201 | if (Ty->isArrayType()) |
| 4202 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4203 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4204 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4205 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 4206 | |
| 4207 | // Insert our type, and its more-qualified variants, into the set |
| 4208 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4209 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4210 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4211 | } else if (Ty->isMemberPointerType()) { |
| 4212 | // Member pointers are far easier, since the pointee can't be converted. |
| 4213 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4214 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4215 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4216 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4217 | } else if (Ty->isVectorType()) { |
| 4218 | VectorTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4219 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4220 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4221 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4222 | // No conversion functions in incomplete types. |
| 4223 | return; |
| 4224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4226 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4227 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 4228 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4229 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4230 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4231 | NamedDecl *D = I.getDecl(); |
| 4232 | if (isa<UsingShadowDecl>(D)) |
| 4233 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4234 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4235 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4236 | // about which builtin types we can convert to. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4237 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4238 | continue; |
| 4239 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4240 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4241 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4242 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4243 | VisibleQuals); |
| 4244 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | } |
| 4249 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4250 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4251 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4252 | /// given type to the candidate set. |
| 4253 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4254 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4255 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4256 | unsigned NumArgs, |
| 4257 | OverloadCandidateSet &CandidateSet) { |
| 4258 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4260 | // T& operator=(T&, T) |
| 4261 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4262 | ParamTypes[1] = T; |
| 4263 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4264 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4266 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4267 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4268 | ParamTypes[0] |
| 4269 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4270 | ParamTypes[1] = T; |
| 4271 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4275 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4276 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4277 | /// 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] | 4278 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4279 | Qualifiers VRQuals; |
| 4280 | const RecordType *TyRec; |
| 4281 | if (const MemberPointerType *RHSMPType = |
| 4282 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4283 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4284 | else |
| 4285 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4286 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4287 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4288 | VRQuals.addVolatile(); |
| 4289 | VRQuals.addRestrict(); |
| 4290 | return VRQuals; |
| 4291 | } |
| 4292 | |
| 4293 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4294 | if (!ClassDecl->hasDefinition()) |
| 4295 | return VRQuals; |
| 4296 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4297 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4298 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4299 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4300 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4301 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4302 | NamedDecl *D = I.getDecl(); |
| 4303 | if (isa<UsingShadowDecl>(D)) |
| 4304 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4305 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4306 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4307 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4308 | CanTy = ResTypeRef->getPointeeType(); |
| 4309 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4310 | // as see them. |
| 4311 | bool done = false; |
| 4312 | while (!done) { |
| 4313 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4314 | CanTy = ResTypePtr->getPointeeType(); |
| 4315 | else if (const MemberPointerType *ResTypeMPtr = |
| 4316 | CanTy->getAs<MemberPointerType>()) |
| 4317 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4318 | else |
| 4319 | done = true; |
| 4320 | if (CanTy.isVolatileQualified()) |
| 4321 | VRQuals.addVolatile(); |
| 4322 | if (CanTy.isRestrictQualified()) |
| 4323 | VRQuals.addRestrict(); |
| 4324 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4325 | return VRQuals; |
| 4326 | } |
| 4327 | } |
| 4328 | } |
| 4329 | return VRQuals; |
| 4330 | } |
| 4331 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4332 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 4333 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 4334 | /// on the operator @p Op and the arguments given. For example, if the |
| 4335 | /// operator is a binary '+', this routine might add "int |
| 4336 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4337 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4339 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4340 | Expr **Args, unsigned NumArgs, |
| 4341 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4342 | // The set of "promoted arithmetic types", which are the arithmetic |
| 4343 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 4344 | // that the first few of these types are the promoted integral |
| 4345 | // types; these types need to be first. |
| 4346 | // FIXME: What about complex? |
| 4347 | const unsigned FirstIntegralType = 0; |
| 4348 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4350 | LastPromotedIntegralType = 13; |
| 4351 | const unsigned FirstPromotedArithmeticType = 7, |
| 4352 | LastPromotedArithmeticType = 16; |
| 4353 | const unsigned NumArithmeticTypes = 16; |
| 4354 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4355 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 4356 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4357 | Context.SignedCharTy, Context.ShortTy, |
| 4358 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 4359 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 4360 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 4361 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 4362 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 4363 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 4364 | "Invalid first promoted integral type"); |
| 4365 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 4366 | == Context.UnsignedLongLongTy && |
| 4367 | "Invalid last promoted integral type"); |
| 4368 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 4369 | "Invalid first promoted arithmetic type"); |
| 4370 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 4371 | == Context.LongDoubleTy && |
| 4372 | "Invalid last promoted arithmetic type"); |
| 4373 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4374 | // Find all of the types that the arguments can convert to, but only |
| 4375 | // if the operator we're looking at has built-in operator candidates |
| 4376 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4377 | Qualifiers VisibleTypeConversionsQuals; |
| 4378 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4379 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4380 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 4381 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4382 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4383 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4384 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 4385 | OpLoc, |
| 4386 | true, |
| 4387 | (Op == OO_Exclaim || |
| 4388 | Op == OO_AmpAmp || |
| 4389 | Op == OO_PipePipe), |
| 4390 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4391 | |
| 4392 | bool isComparison = false; |
| 4393 | switch (Op) { |
| 4394 | case OO_None: |
| 4395 | case NUM_OVERLOADED_OPERATORS: |
| 4396 | assert(false && "Expected an overloaded operator"); |
| 4397 | break; |
| 4398 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4399 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4400 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4401 | goto UnaryStar; |
| 4402 | else |
| 4403 | goto BinaryStar; |
| 4404 | break; |
| 4405 | |
| 4406 | case OO_Plus: // '+' is either unary or binary |
| 4407 | if (NumArgs == 1) |
| 4408 | goto UnaryPlus; |
| 4409 | else |
| 4410 | goto BinaryPlus; |
| 4411 | break; |
| 4412 | |
| 4413 | case OO_Minus: // '-' is either unary or binary |
| 4414 | if (NumArgs == 1) |
| 4415 | goto UnaryMinus; |
| 4416 | else |
| 4417 | goto BinaryMinus; |
| 4418 | break; |
| 4419 | |
| 4420 | case OO_Amp: // '&' is either unary or binary |
| 4421 | if (NumArgs == 1) |
| 4422 | goto UnaryAmp; |
| 4423 | else |
| 4424 | goto BinaryAmp; |
| 4425 | |
| 4426 | case OO_PlusPlus: |
| 4427 | case OO_MinusMinus: |
| 4428 | // C++ [over.built]p3: |
| 4429 | // |
| 4430 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 4431 | // is either volatile or empty, there exist candidate operator |
| 4432 | // functions of the form |
| 4433 | // |
| 4434 | // VQ T& operator++(VQ T&); |
| 4435 | // T operator++(VQ T&, int); |
| 4436 | // |
| 4437 | // C++ [over.built]p4: |
| 4438 | // |
| 4439 | // For every pair (T, VQ), where T is an arithmetic type other |
| 4440 | // than bool, and VQ is either volatile or empty, there exist |
| 4441 | // candidate operator functions of the form |
| 4442 | // |
| 4443 | // VQ T& operator--(VQ T&); |
| 4444 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4446 | Arith < NumArithmeticTypes; ++Arith) { |
| 4447 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4449 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4450 | |
| 4451 | // Non-volatile version. |
| 4452 | if (NumArgs == 1) |
| 4453 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4454 | else |
| 4455 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4456 | // heuristic to reduce number of builtin candidates in the set. |
| 4457 | // Add volatile version only if there are conversions to a volatile type. |
| 4458 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4459 | // Volatile version |
| 4460 | ParamTypes[0] |
| 4461 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 4462 | if (NumArgs == 1) |
| 4463 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4464 | else |
| 4465 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 4466 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | // C++ [over.built]p5: |
| 4470 | // |
| 4471 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4472 | // cv-unqualified object type, and VQ is either volatile or |
| 4473 | // empty, there exist candidate operator functions of the form |
| 4474 | // |
| 4475 | // T*VQ& operator++(T*VQ&); |
| 4476 | // T*VQ& operator--(T*VQ&); |
| 4477 | // T* operator++(T*VQ&, int); |
| 4478 | // T* operator--(T*VQ&, int); |
| 4479 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4480 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4481 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4482 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4483 | continue; |
| 4484 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | QualType ParamTypes[2] = { |
| 4486 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4487 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4488 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4489 | // Without volatile |
| 4490 | if (NumArgs == 1) |
| 4491 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4492 | else |
| 4493 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4494 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4495 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4496 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4497 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4498 | ParamTypes[0] |
| 4499 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4500 | if (NumArgs == 1) |
| 4501 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4502 | else |
| 4503 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4504 | } |
| 4505 | } |
| 4506 | break; |
| 4507 | |
| 4508 | UnaryStar: |
| 4509 | // C++ [over.built]p6: |
| 4510 | // For every cv-qualified or cv-unqualified object type T, there |
| 4511 | // exist candidate operator functions of the form |
| 4512 | // |
| 4513 | // T& operator*(T*); |
| 4514 | // |
| 4515 | // C++ [over.built]p7: |
| 4516 | // For every function type T, there exist candidate operator |
| 4517 | // functions of the form |
| 4518 | // T& operator*(T*); |
| 4519 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4520 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4521 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4522 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4524 | &ParamTy, Args, 1, CandidateSet); |
| 4525 | } |
| 4526 | break; |
| 4527 | |
| 4528 | UnaryPlus: |
| 4529 | // C++ [over.built]p8: |
| 4530 | // For every type T, there exist candidate operator functions of |
| 4531 | // the form |
| 4532 | // |
| 4533 | // T* operator+(T*); |
| 4534 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4535 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4536 | QualType ParamTy = *Ptr; |
| 4537 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 4538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4540 | // Fall through |
| 4541 | |
| 4542 | UnaryMinus: |
| 4543 | // C++ [over.built]p9: |
| 4544 | // For every promoted arithmetic type T, there exist candidate |
| 4545 | // operator functions of the form |
| 4546 | // |
| 4547 | // T operator+(T); |
| 4548 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4549 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4550 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 4551 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 4552 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 4553 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4554 | |
| 4555 | // Extension: We also add these operators for vector types. |
| 4556 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4557 | VecEnd = CandidateTypes.vector_end(); |
| 4558 | Vec != VecEnd; ++Vec) { |
| 4559 | QualType VecTy = *Vec; |
| 4560 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4561 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4562 | break; |
| 4563 | |
| 4564 | case OO_Tilde: |
| 4565 | // C++ [over.built]p10: |
| 4566 | // For every promoted integral type T, there exist candidate |
| 4567 | // operator functions of the form |
| 4568 | // |
| 4569 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4570 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4571 | Int < LastPromotedIntegralType; ++Int) { |
| 4572 | QualType IntTy = ArithmeticTypes[Int]; |
| 4573 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 4574 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4575 | |
| 4576 | // Extension: We also add this operator for vector types. |
| 4577 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4578 | VecEnd = CandidateTypes.vector_end(); |
| 4579 | Vec != VecEnd; ++Vec) { |
| 4580 | QualType VecTy = *Vec; |
| 4581 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4582 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4583 | break; |
| 4584 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4585 | case OO_New: |
| 4586 | case OO_Delete: |
| 4587 | case OO_Array_New: |
| 4588 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4589 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4590 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4591 | break; |
| 4592 | |
| 4593 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4594 | UnaryAmp: |
| 4595 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4596 | // C++ [over.match.oper]p3: |
| 4597 | // -- For the operator ',', the unary operator '&', or the |
| 4598 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4599 | break; |
| 4600 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4601 | case OO_EqualEqual: |
| 4602 | case OO_ExclaimEqual: |
| 4603 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | // For every pointer to member type T, there exist candidate operator |
| 4605 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4606 | // |
| 4607 | // bool operator==(T,T); |
| 4608 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4609 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4610 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4611 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4612 | MemPtr != MemPtrEnd; |
| 4613 | ++MemPtr) { |
| 4614 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4615 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4616 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4618 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4620 | case OO_Less: |
| 4621 | case OO_Greater: |
| 4622 | case OO_LessEqual: |
| 4623 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4624 | // C++ [over.built]p15: |
| 4625 | // |
| 4626 | // For every pointer or enumeration type T, there exist |
| 4627 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4629 | // bool operator<(T, T); |
| 4630 | // bool operator>(T, T); |
| 4631 | // bool operator<=(T, T); |
| 4632 | // bool operator>=(T, T); |
| 4633 | // bool operator==(T, T); |
| 4634 | // bool operator!=(T, T); |
| 4635 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4636 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4637 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4638 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4639 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4640 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4641 | = CandidateTypes.enumeration_begin(); |
| 4642 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4643 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4644 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4645 | } |
| 4646 | |
| 4647 | // Fall through. |
| 4648 | isComparison = true; |
| 4649 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4650 | BinaryPlus: |
| 4651 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4652 | if (!isComparison) { |
| 4653 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4654 | |
| 4655 | // C++ [over.built]p13: |
| 4656 | // |
| 4657 | // For every cv-qualified or cv-unqualified object type T |
| 4658 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4660 | // T* operator+(T*, ptrdiff_t); |
| 4661 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4662 | // T* operator-(T*, ptrdiff_t); |
| 4663 | // T* operator+(ptrdiff_t, T*); |
| 4664 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4665 | // |
| 4666 | // C++ [over.built]p14: |
| 4667 | // |
| 4668 | // For every T, where T is a pointer to object type, there |
| 4669 | // exist candidate operator functions of the form |
| 4670 | // |
| 4671 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4673 | = CandidateTypes.pointer_begin(); |
| 4674 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4675 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4676 | |
| 4677 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4678 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4679 | |
| 4680 | if (Op == OO_Plus) { |
| 4681 | // T* operator+(ptrdiff_t, T*); |
| 4682 | ParamTypes[0] = ParamTypes[1]; |
| 4683 | ParamTypes[1] = *Ptr; |
| 4684 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4685 | } else { |
| 4686 | // ptrdiff_t operator-(T, T); |
| 4687 | ParamTypes[1] = *Ptr; |
| 4688 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4689 | Args, 2, CandidateSet); |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | // Fall through |
| 4694 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4695 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4696 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4697 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4698 | // C++ [over.built]p12: |
| 4699 | // |
| 4700 | // For every pair of promoted arithmetic types L and R, there |
| 4701 | // exist candidate operator functions of the form |
| 4702 | // |
| 4703 | // LR operator*(L, R); |
| 4704 | // LR operator/(L, R); |
| 4705 | // LR operator+(L, R); |
| 4706 | // LR operator-(L, R); |
| 4707 | // bool operator<(L, R); |
| 4708 | // bool operator>(L, R); |
| 4709 | // bool operator<=(L, R); |
| 4710 | // bool operator>=(L, R); |
| 4711 | // bool operator==(L, R); |
| 4712 | // bool operator!=(L, R); |
| 4713 | // |
| 4714 | // where LR is the result of the usual arithmetic conversions |
| 4715 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4716 | // |
| 4717 | // C++ [over.built]p24: |
| 4718 | // |
| 4719 | // For every pair of promoted arithmetic types L and R, there exist |
| 4720 | // candidate operator functions of the form |
| 4721 | // |
| 4722 | // LR operator?(bool, L, R); |
| 4723 | // |
| 4724 | // where LR is the result of the usual arithmetic conversions |
| 4725 | // between types L and R. |
| 4726 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4728 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4730 | Right < LastPromotedArithmeticType; ++Right) { |
| 4731 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4732 | QualType Result |
| 4733 | = isComparison |
| 4734 | ? Context.BoolTy |
| 4735 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4736 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4737 | } |
| 4738 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4739 | |
| 4740 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 4741 | // conditional operator for vector types. |
| 4742 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4743 | Vec1End = CandidateTypes.vector_end(); |
| 4744 | Vec1 != Vec1End; ++Vec1) |
| 4745 | for (BuiltinCandidateTypeSet::iterator |
| 4746 | Vec2 = CandidateTypes.vector_begin(), |
| 4747 | Vec2End = CandidateTypes.vector_end(); |
| 4748 | Vec2 != Vec2End; ++Vec2) { |
| 4749 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 4750 | QualType Result; |
| 4751 | if (isComparison) |
| 4752 | Result = Context.BoolTy; |
| 4753 | else { |
| 4754 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 4755 | Result = *Vec1; |
| 4756 | else |
| 4757 | Result = *Vec2; |
| 4758 | } |
| 4759 | |
| 4760 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4761 | } |
| 4762 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4763 | break; |
| 4764 | |
| 4765 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4766 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4767 | case OO_Caret: |
| 4768 | case OO_Pipe: |
| 4769 | case OO_LessLess: |
| 4770 | case OO_GreaterGreater: |
| 4771 | // C++ [over.built]p17: |
| 4772 | // |
| 4773 | // For every pair of promoted integral types L and R, there |
| 4774 | // exist candidate operator functions of the form |
| 4775 | // |
| 4776 | // LR operator%(L, R); |
| 4777 | // LR operator&(L, R); |
| 4778 | // LR operator^(L, R); |
| 4779 | // LR operator|(L, R); |
| 4780 | // L operator<<(L, R); |
| 4781 | // L operator>>(L, R); |
| 4782 | // |
| 4783 | // where LR is the result of the usual arithmetic conversions |
| 4784 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4786 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4788 | Right < LastPromotedIntegralType; ++Right) { |
| 4789 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4790 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4791 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4792 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4793 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4794 | } |
| 4795 | } |
| 4796 | break; |
| 4797 | |
| 4798 | case OO_Equal: |
| 4799 | // C++ [over.built]p20: |
| 4800 | // |
| 4801 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4802 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4803 | // empty, there exist candidate operator functions of the form |
| 4804 | // |
| 4805 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4806 | for (BuiltinCandidateTypeSet::iterator |
| 4807 | Enum = CandidateTypes.enumeration_begin(), |
| 4808 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4809 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4811 | CandidateSet); |
| 4812 | for (BuiltinCandidateTypeSet::iterator |
| 4813 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4814 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4815 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4817 | CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4818 | |
| 4819 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4820 | |
| 4821 | case OO_PlusEqual: |
| 4822 | case OO_MinusEqual: |
| 4823 | // C++ [over.built]p19: |
| 4824 | // |
| 4825 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4826 | // volatile or empty, there exist candidate operator functions |
| 4827 | // of the form |
| 4828 | // |
| 4829 | // T*VQ& operator=(T*VQ&, T*); |
| 4830 | // |
| 4831 | // C++ [over.built]p21: |
| 4832 | // |
| 4833 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4834 | // cv-unqualified object type and VQ is either volatile or |
| 4835 | // empty, there exist candidate operator functions of the form |
| 4836 | // |
| 4837 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4838 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4839 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4840 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4841 | QualType ParamTypes[2]; |
| 4842 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4843 | |
| 4844 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4845 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4846 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4847 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4848 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4849 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4850 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4851 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4852 | ParamTypes[0] |
| 4853 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4854 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4855 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4856 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4857 | } |
| 4858 | // Fall through. |
| 4859 | |
| 4860 | case OO_StarEqual: |
| 4861 | case OO_SlashEqual: |
| 4862 | // C++ [over.built]p18: |
| 4863 | // |
| 4864 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 4865 | // VQ is either volatile or empty, and R is a promoted |
| 4866 | // arithmetic type, there exist candidate operator functions of |
| 4867 | // the form |
| 4868 | // |
| 4869 | // VQ L& operator=(VQ L&, R); |
| 4870 | // VQ L& operator*=(VQ L&, R); |
| 4871 | // VQ L& operator/=(VQ L&, R); |
| 4872 | // VQ L& operator+=(VQ L&, R); |
| 4873 | // VQ L& operator-=(VQ L&, R); |
| 4874 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4875 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4876 | Right < LastPromotedArithmeticType; ++Right) { |
| 4877 | QualType ParamTypes[2]; |
| 4878 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4879 | |
| 4880 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4881 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4882 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4883 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4884 | |
| 4885 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4886 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4887 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 4888 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4889 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4890 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4891 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4892 | } |
| 4893 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4894 | |
| 4895 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 4896 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4897 | Vec1End = CandidateTypes.vector_end(); |
| 4898 | Vec1 != Vec1End; ++Vec1) |
| 4899 | for (BuiltinCandidateTypeSet::iterator |
| 4900 | Vec2 = CandidateTypes.vector_begin(), |
| 4901 | Vec2End = CandidateTypes.vector_end(); |
| 4902 | Vec2 != Vec2End; ++Vec2) { |
| 4903 | QualType ParamTypes[2]; |
| 4904 | ParamTypes[1] = *Vec2; |
| 4905 | // Add this built-in operator as a candidate (VQ is empty). |
| 4906 | ParamTypes[0] = Context.getLValueReferenceType(*Vec1); |
| 4907 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4908 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4909 | |
| 4910 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4911 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4912 | ParamTypes[0] = Context.getVolatileType(*Vec1); |
| 4913 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4914 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4915 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4916 | } |
| 4917 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4918 | break; |
| 4919 | |
| 4920 | case OO_PercentEqual: |
| 4921 | case OO_LessLessEqual: |
| 4922 | case OO_GreaterGreaterEqual: |
| 4923 | case OO_AmpEqual: |
| 4924 | case OO_CaretEqual: |
| 4925 | case OO_PipeEqual: |
| 4926 | // C++ [over.built]p22: |
| 4927 | // |
| 4928 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 4929 | // is either volatile or empty, and R is a promoted integral |
| 4930 | // type, there exist candidate operator functions of the form |
| 4931 | // |
| 4932 | // VQ L& operator%=(VQ L&, R); |
| 4933 | // VQ L& operator<<=(VQ L&, R); |
| 4934 | // VQ L& operator>>=(VQ L&, R); |
| 4935 | // VQ L& operator&=(VQ L&, R); |
| 4936 | // VQ L& operator^=(VQ L&, R); |
| 4937 | // VQ L& operator|=(VQ L&, R); |
| 4938 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4940 | Right < LastPromotedIntegralType; ++Right) { |
| 4941 | QualType ParamTypes[2]; |
| 4942 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4943 | |
| 4944 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4945 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4946 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4947 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4948 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4949 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4950 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4951 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4952 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4953 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4954 | } |
| 4955 | } |
| 4956 | break; |
| 4957 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4958 | case OO_Exclaim: { |
| 4959 | // C++ [over.operator]p23: |
| 4960 | // |
| 4961 | // There also exist candidate operator functions of the form |
| 4962 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4964 | // bool operator&&(bool, bool); [BELOW] |
| 4965 | // bool operator||(bool, bool); [BELOW] |
| 4966 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4967 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4968 | /*IsAssignmentOperator=*/false, |
| 4969 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4970 | break; |
| 4971 | } |
| 4972 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4973 | case OO_AmpAmp: |
| 4974 | case OO_PipePipe: { |
| 4975 | // C++ [over.operator]p23: |
| 4976 | // |
| 4977 | // There also exist candidate operator functions of the form |
| 4978 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4979 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4980 | // bool operator&&(bool, bool); |
| 4981 | // bool operator||(bool, bool); |
| 4982 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4983 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4984 | /*IsAssignmentOperator=*/false, |
| 4985 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4986 | break; |
| 4987 | } |
| 4988 | |
| 4989 | case OO_Subscript: |
| 4990 | // C++ [over.built]p13: |
| 4991 | // |
| 4992 | // For every cv-qualified or cv-unqualified object type T there |
| 4993 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4994 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4995 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4996 | // T& operator[](T*, ptrdiff_t); |
| 4997 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4998 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4999 | // T& operator[](ptrdiff_t, T*); |
| 5000 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 5001 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5002 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5003 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5004 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5005 | |
| 5006 | // T& operator[](T*, ptrdiff_t) |
| 5007 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5008 | |
| 5009 | // T& operator[](ptrdiff_t, T*); |
| 5010 | ParamTypes[0] = ParamTypes[1]; |
| 5011 | ParamTypes[1] = *Ptr; |
| 5012 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5013 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5014 | break; |
| 5015 | |
| 5016 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5017 | // C++ [over.built]p11: |
| 5018 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5019 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5020 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5021 | // there exist candidate operator functions of the form |
| 5022 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5023 | // where CV12 is the union of CV1 and CV2. |
| 5024 | { |
| 5025 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5026 | CandidateTypes.pointer_begin(); |
| 5027 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5028 | QualType C1Ty = (*Ptr); |
| 5029 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5030 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5031 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5032 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5033 | if (!isa<RecordType>(C1)) |
| 5034 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5035 | // heuristic to reduce number of builtin candidates in the set. |
| 5036 | // Add volatile/restrict version only if there are conversions to a |
| 5037 | // volatile/restrict type. |
| 5038 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5039 | continue; |
| 5040 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5041 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5042 | } |
| 5043 | for (BuiltinCandidateTypeSet::iterator |
| 5044 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 5045 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 5046 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5047 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5048 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 5049 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5050 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 5051 | break; |
| 5052 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5053 | // build CV12 T& |
| 5054 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5055 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5056 | T.isVolatileQualified()) |
| 5057 | continue; |
| 5058 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5059 | T.isRestrictQualified()) |
| 5060 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5061 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5062 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 5063 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5064 | } |
| 5065 | } |
| 5066 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5067 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5068 | |
| 5069 | case OO_Conditional: |
| 5070 | // Note that we don't consider the first argument, since it has been |
| 5071 | // contextually converted to bool long ago. The candidates below are |
| 5072 | // therefore added as binary. |
| 5073 | // |
| 5074 | // C++ [over.built]p24: |
| 5075 | // For every type T, where T is a pointer or pointer-to-member type, |
| 5076 | // there exist candidate operator functions of the form |
| 5077 | // |
| 5078 | // T operator?(bool, T, T); |
| 5079 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5080 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 5081 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 5082 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5083 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5084 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5085 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5086 | CandidateTypes.member_pointer_begin(), |
| 5087 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 5088 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5089 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5090 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5091 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5092 | } |
| 5093 | } |
| 5094 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5095 | /// \brief Add function candidates found via argument-dependent lookup |
| 5096 | /// to the set of overloading candidates. |
| 5097 | /// |
| 5098 | /// This routine performs argument-dependent name lookup based on the |
| 5099 | /// given function name (which may also be an operator name) and adds |
| 5100 | /// all of the overload candidates found by ADL to the overload |
| 5101 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5102 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5103 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5104 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5105 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5106 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5107 | OverloadCandidateSet& CandidateSet, |
| 5108 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5109 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5110 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 5111 | // FIXME: This approach for uniquing ADL results (and removing |
| 5112 | // redundant candidates from the set) relies on pointer-equality, |
| 5113 | // which means we need to key off the canonical decl. However, |
| 5114 | // always going back to the canonical decl might not get us the |
| 5115 | // right set of default arguments. What default arguments are |
| 5116 | // we supposed to consider on ADL candidates, anyway? |
| 5117 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5118 | // FIXME: Pass in the explicit template arguments? |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5119 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5120 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5121 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5122 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5123 | CandEnd = CandidateSet.end(); |
| 5124 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5125 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5126 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5127 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5128 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5129 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5130 | |
| 5131 | // For each of the ADL candidates we found, add it to the overload |
| 5132 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5133 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5134 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5135 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5136 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5137 | continue; |
| 5138 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5139 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5140 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5141 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5142 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5143 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 5144 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5145 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5148 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 5149 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5150 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5151 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5152 | const OverloadCandidate& Cand2, |
| 5153 | SourceLocation Loc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5154 | // Define viable functions to be better candidates than non-viable |
| 5155 | // functions. |
| 5156 | if (!Cand2.Viable) |
| 5157 | return Cand1.Viable; |
| 5158 | else if (!Cand1.Viable) |
| 5159 | return false; |
| 5160 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5161 | // C++ [over.match.best]p1: |
| 5162 | // |
| 5163 | // -- if F is a static member function, ICS1(F) is defined such |
| 5164 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 5165 | // any function G, and, symmetrically, ICS1(G) is neither |
| 5166 | // better nor worse than ICS1(F). |
| 5167 | unsigned StartArg = 0; |
| 5168 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 5169 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5170 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5171 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5172 | // A viable function F1 is defined to be a better function than another |
| 5173 | // 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] | 5174 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5175 | unsigned NumArgs = Cand1.Conversions.size(); |
| 5176 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 5177 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5178 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5179 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 5180 | Cand2.Conversions[ArgIdx])) { |
| 5181 | case ImplicitConversionSequence::Better: |
| 5182 | // Cand1 has a better conversion sequence. |
| 5183 | HasBetterConversion = true; |
| 5184 | break; |
| 5185 | |
| 5186 | case ImplicitConversionSequence::Worse: |
| 5187 | // Cand1 can't be better than Cand2. |
| 5188 | return false; |
| 5189 | |
| 5190 | case ImplicitConversionSequence::Indistinguishable: |
| 5191 | // Do nothing. |
| 5192 | break; |
| 5193 | } |
| 5194 | } |
| 5195 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5196 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5197 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5198 | if (HasBetterConversion) |
| 5199 | return true; |
| 5200 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5201 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5202 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 5203 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5204 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 5205 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5206 | |
| 5207 | // -- F1 and F2 are function template specializations, and the function |
| 5208 | // template for F1 is more specialized than the template for F2 |
| 5209 | // 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] | 5210 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 5211 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 5212 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5213 | if (FunctionTemplateDecl *BetterTemplate |
| 5214 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 5215 | Cand2.Function->getPrimaryTemplate(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5216 | Loc, |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 5217 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 5218 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5219 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5220 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5221 | // -- the context is an initialization by user-defined conversion |
| 5222 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 5223 | // from the return type of F1 to the destination type (i.e., |
| 5224 | // the type of the entity being initialized) is a better |
| 5225 | // conversion sequence than the standard conversion sequence |
| 5226 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5227 | if (Cand1.Function && Cand2.Function && |
| 5228 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5229 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 5230 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 5231 | Cand2.FinalConversion)) { |
| 5232 | case ImplicitConversionSequence::Better: |
| 5233 | // Cand1 has a better conversion sequence. |
| 5234 | return true; |
| 5235 | |
| 5236 | case ImplicitConversionSequence::Worse: |
| 5237 | // Cand1 can't be better than Cand2. |
| 5238 | return false; |
| 5239 | |
| 5240 | case ImplicitConversionSequence::Indistinguishable: |
| 5241 | // Do nothing |
| 5242 | break; |
| 5243 | } |
| 5244 | } |
| 5245 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5246 | return false; |
| 5247 | } |
| 5248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5249 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5250 | /// within an overload candidate set. |
| 5251 | /// |
| 5252 | /// \param CandidateSet the set of candidate functions. |
| 5253 | /// |
| 5254 | /// \param Loc the location of the function name (or operator symbol) for |
| 5255 | /// which overload resolution occurs. |
| 5256 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5258 | /// function, Best points to the candidate function found. |
| 5259 | /// |
| 5260 | /// \returns The result of overload resolution. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5261 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 5262 | SourceLocation Loc, |
| 5263 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5264 | // Find the best viable function. |
| 5265 | Best = CandidateSet.end(); |
| 5266 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 5267 | Cand != CandidateSet.end(); ++Cand) { |
| 5268 | if (Cand->Viable) { |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5269 | if (Best == CandidateSet.end() || |
| 5270 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5271 | Best = Cand; |
| 5272 | } |
| 5273 | } |
| 5274 | |
| 5275 | // If we didn't find any viable functions, abort. |
| 5276 | if (Best == CandidateSet.end()) |
| 5277 | return OR_No_Viable_Function; |
| 5278 | |
| 5279 | // Make sure that this function is better than every other viable |
| 5280 | // function. If not, we have an ambiguity. |
| 5281 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 5282 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5283 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5284 | Cand != Best && |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5285 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5286 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5287 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5288 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5289 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5290 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5291 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5292 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5293 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5294 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5295 | return OR_Deleted; |
| 5296 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5297 | // C++ [basic.def.odr]p2: |
| 5298 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5299 | // when referred to from a potentially-evaluated expression. [Note: this |
| 5300 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5301 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 5302 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 5303 | if (Best->Function) |
| 5304 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5305 | return OR_Success; |
| 5306 | } |
| 5307 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5308 | namespace { |
| 5309 | |
| 5310 | enum OverloadCandidateKind { |
| 5311 | oc_function, |
| 5312 | oc_method, |
| 5313 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5314 | oc_function_template, |
| 5315 | oc_method_template, |
| 5316 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5317 | oc_implicit_default_constructor, |
| 5318 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5319 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5320 | }; |
| 5321 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5322 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 5323 | FunctionDecl *Fn, |
| 5324 | std::string &Description) { |
| 5325 | bool isTemplate = false; |
| 5326 | |
| 5327 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 5328 | isTemplate = true; |
| 5329 | Description = S.getTemplateArgumentBindingsText( |
| 5330 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 5331 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5332 | |
| 5333 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5334 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5335 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5336 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5337 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 5338 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5342 | // This actually gets spelled 'candidate function' for now, but |
| 5343 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5344 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5345 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5346 | |
| 5347 | assert(Meth->isCopyAssignment() |
| 5348 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5349 | return oc_implicit_copy_assignment; |
| 5350 | } |
| 5351 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5352 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | } // end anonymous namespace |
| 5356 | |
| 5357 | // Notes the location of an overload candidate. |
| 5358 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5359 | std::string FnDesc; |
| 5360 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 5361 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 5362 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5365 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 5366 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 5367 | /// target types of the conversion. |
| 5368 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 5369 | SourceLocation CaretLoc, |
| 5370 | const PartialDiagnostic &PDiag) { |
| 5371 | Diag(CaretLoc, PDiag) |
| 5372 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 5373 | for (AmbiguousConversionSequence::const_iterator |
| 5374 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 5375 | NoteOverloadCandidate(*I); |
| 5376 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5379 | namespace { |
| 5380 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5381 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 5382 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 5383 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5384 | assert(Cand->Function && "for now, candidate must be a function"); |
| 5385 | FunctionDecl *Fn = Cand->Function; |
| 5386 | |
| 5387 | // There's a conversion slot for the object argument if this is a |
| 5388 | // non-constructor method. Note that 'I' corresponds the |
| 5389 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5390 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5391 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5392 | if (I == 0) |
| 5393 | isObjectArgument = true; |
| 5394 | else |
| 5395 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5398 | std::string FnDesc; |
| 5399 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 5400 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5401 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 5402 | QualType FromTy = Conv.Bad.getFromType(); |
| 5403 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5404 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5405 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5406 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5407 | Expr *E = FromExpr->IgnoreParens(); |
| 5408 | if (isa<UnaryOperator>(E)) |
| 5409 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5410 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5411 | |
| 5412 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 5413 | << (unsigned) FnKind << FnDesc |
| 5414 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5415 | << ToTy << Name << I+1; |
| 5416 | return; |
| 5417 | } |
| 5418 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5419 | // Do some hand-waving analysis to see if the non-viability is due |
| 5420 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5421 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 5422 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 5423 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 5424 | CToTy = RT->getPointeeType(); |
| 5425 | else { |
| 5426 | // TODO: detect and diagnose the full richness of const mismatches. |
| 5427 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 5428 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 5429 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 5430 | } |
| 5431 | |
| 5432 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 5433 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 5434 | // It is dumb that we have to do this here. |
| 5435 | while (isa<ArrayType>(CFromTy)) |
| 5436 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5437 | while (isa<ArrayType>(CToTy)) |
| 5438 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5439 | |
| 5440 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 5441 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 5442 | |
| 5443 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 5444 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 5445 | << (unsigned) FnKind << FnDesc |
| 5446 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5447 | << FromTy |
| 5448 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 5449 | << (unsigned) isObjectArgument << I+1; |
| 5450 | return; |
| 5451 | } |
| 5452 | |
| 5453 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5454 | assert(CVR && "unexpected qualifiers mismatch"); |
| 5455 | |
| 5456 | if (isObjectArgument) { |
| 5457 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 5458 | << (unsigned) FnKind << FnDesc |
| 5459 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5460 | << FromTy << (CVR - 1); |
| 5461 | } else { |
| 5462 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 5463 | << (unsigned) FnKind << FnDesc |
| 5464 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5465 | << FromTy << (CVR - 1) << I+1; |
| 5466 | } |
| 5467 | return; |
| 5468 | } |
| 5469 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5470 | // Diagnose references or pointers to incomplete types differently, |
| 5471 | // since it's far from impossible that the incompleteness triggered |
| 5472 | // the failure. |
| 5473 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 5474 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 5475 | TempFromTy = PTy->getPointeeType(); |
| 5476 | if (TempFromTy->isIncompleteType()) { |
| 5477 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 5478 | << (unsigned) FnKind << FnDesc |
| 5479 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5480 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 5481 | return; |
| 5482 | } |
| 5483 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5484 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5485 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5486 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 5487 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 5488 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5489 | FromPtrTy->getPointeeType()) && |
| 5490 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 5491 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
| 5492 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
| 5493 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5494 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5495 | } |
| 5496 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 5497 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 5498 | if (const ObjCObjectPointerType *ToPtrTy |
| 5499 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 5500 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 5501 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 5502 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5503 | FromPtrTy->getPointeeType()) && |
| 5504 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5505 | BaseToDerivedConversion = 2; |
| 5506 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 5507 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 5508 | !FromTy->isIncompleteType() && |
| 5509 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 5510 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 5511 | BaseToDerivedConversion = 3; |
| 5512 | } |
| 5513 | |
| 5514 | if (BaseToDerivedConversion) { |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5515 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5516 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5517 | << (unsigned) FnKind << FnDesc |
| 5518 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5519 | << (BaseToDerivedConversion - 1) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5520 | << FromTy << ToTy << I+1; |
| 5521 | return; |
| 5522 | } |
| 5523 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5524 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5525 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 5526 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5527 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 5528 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 5532 | unsigned NumFormalArgs) { |
| 5533 | // TODO: treat calls to a missing default constructor as a special case |
| 5534 | |
| 5535 | FunctionDecl *Fn = Cand->Function; |
| 5536 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 5537 | |
| 5538 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 5539 | |
| 5540 | // at least / at most / exactly |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5541 | // FIXME: variadic templates "at most" should account for parameter packs |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5542 | unsigned mode, modeCount; |
| 5543 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5544 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 5545 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5546 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5547 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 5548 | mode = 0; // "at least" |
| 5549 | else |
| 5550 | mode = 2; // "exactly" |
| 5551 | modeCount = MinParams; |
| 5552 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5553 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 5554 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5555 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5556 | if (MinParams != FnTy->getNumArgs()) |
| 5557 | mode = 1; // "at most" |
| 5558 | else |
| 5559 | mode = 2; // "exactly" |
| 5560 | modeCount = FnTy->getNumArgs(); |
| 5561 | } |
| 5562 | |
| 5563 | std::string Description; |
| 5564 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 5565 | |
| 5566 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5567 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 5568 | << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5571 | /// Diagnose a failed template-argument deduction. |
| 5572 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 5573 | Expr **Args, unsigned NumArgs) { |
| 5574 | FunctionDecl *Fn = Cand->Function; // pattern |
| 5575 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5576 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5577 | NamedDecl *ParamD; |
| 5578 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 5579 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 5580 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5581 | switch (Cand->DeductionFailure.Result) { |
| 5582 | case Sema::TDK_Success: |
| 5583 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 5584 | |
| 5585 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5586 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 5587 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 5588 | << ParamD->getDeclName(); |
| 5589 | return; |
| 5590 | } |
| 5591 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5592 | case Sema::TDK_Inconsistent: |
| 5593 | case Sema::TDK_InconsistentQuals: { |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5594 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5595 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5596 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5597 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5598 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5599 | which = 1; |
| 5600 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5601 | which = 2; |
| 5602 | } |
| 5603 | |
| 5604 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
| 5605 | << which << ParamD->getDeclName() |
| 5606 | << *Cand->DeductionFailure.getFirstArg() |
| 5607 | << *Cand->DeductionFailure.getSecondArg(); |
| 5608 | return; |
| 5609 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5610 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5611 | case Sema::TDK_InvalidExplicitArguments: |
| 5612 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| 5613 | if (ParamD->getDeclName()) |
| 5614 | S.Diag(Fn->getLocation(), |
| 5615 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 5616 | << ParamD->getDeclName(); |
| 5617 | else { |
| 5618 | int index = 0; |
| 5619 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 5620 | index = TTP->getIndex(); |
| 5621 | else if (NonTypeTemplateParmDecl *NTTP |
| 5622 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 5623 | index = NTTP->getIndex(); |
| 5624 | else |
| 5625 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| 5626 | S.Diag(Fn->getLocation(), |
| 5627 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 5628 | << (index + 1); |
| 5629 | } |
| 5630 | return; |
| 5631 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5632 | case Sema::TDK_TooManyArguments: |
| 5633 | case Sema::TDK_TooFewArguments: |
| 5634 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 5635 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 5636 | |
| 5637 | case Sema::TDK_InstantiationDepth: |
| 5638 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
| 5639 | return; |
| 5640 | |
| 5641 | case Sema::TDK_SubstitutionFailure: { |
| 5642 | std::string ArgString; |
| 5643 | if (TemplateArgumentList *Args |
| 5644 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 5645 | ArgString = S.getTemplateArgumentBindingsText( |
| 5646 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 5647 | *Args); |
| 5648 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 5649 | << ArgString; |
| 5650 | return; |
| 5651 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5652 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5653 | // TODO: diagnose these individually, then kill off |
| 5654 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5655 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5656 | case Sema::TDK_FailedOverloadResolution: |
| 5657 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 5658 | return; |
| 5659 | } |
| 5660 | } |
| 5661 | |
| 5662 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 5663 | /// already generated a primary error at the call site. |
| 5664 | /// |
| 5665 | /// It really does need to be a single diagnostic with its caret |
| 5666 | /// pointed at the candidate declaration. Yes, this creates some |
| 5667 | /// major challenges of technical writing. Yes, this makes pointing |
| 5668 | /// out problems with specific arguments quite awkward. It's still |
| 5669 | /// better than generating twenty screens of text for every failed |
| 5670 | /// overload. |
| 5671 | /// |
| 5672 | /// It would be great to be able to express per-candidate problems |
| 5673 | /// more richly for those diagnostic clients that cared, but we'd |
| 5674 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5675 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 5676 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5677 | FunctionDecl *Fn = Cand->Function; |
| 5678 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5679 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5680 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5681 | std::string FnDesc; |
| 5682 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5683 | |
| 5684 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5685 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5686 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5687 | } |
| 5688 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5689 | // We don't really have anything else to say about viable candidates. |
| 5690 | if (Cand->Viable) { |
| 5691 | S.NoteOverloadCandidate(Fn); |
| 5692 | return; |
| 5693 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5694 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5695 | switch (Cand->FailureKind) { |
| 5696 | case ovl_fail_too_many_arguments: |
| 5697 | case ovl_fail_too_few_arguments: |
| 5698 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5699 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5700 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5701 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 5702 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5703 | case ovl_fail_trivial_conversion: |
| 5704 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5705 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5706 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5707 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5708 | case ovl_fail_bad_conversion: { |
| 5709 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 5710 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5711 | if (Cand->Conversions[I].isBad()) |
| 5712 | return DiagnoseBadConversion(S, Cand, I); |
| 5713 | |
| 5714 | // FIXME: this currently happens when we're called from SemaInit |
| 5715 | // when user-conversion overload fails. Figure out how to handle |
| 5716 | // those conditions and diagnose them well. |
| 5717 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5718 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5719 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 5723 | // Desugar the type of the surrogate down to a function type, |
| 5724 | // retaining as many typedefs as possible while still showing |
| 5725 | // the function type (and, therefore, its parameter types). |
| 5726 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 5727 | bool isLValueReference = false; |
| 5728 | bool isRValueReference = false; |
| 5729 | bool isPointer = false; |
| 5730 | if (const LValueReferenceType *FnTypeRef = |
| 5731 | FnType->getAs<LValueReferenceType>()) { |
| 5732 | FnType = FnTypeRef->getPointeeType(); |
| 5733 | isLValueReference = true; |
| 5734 | } else if (const RValueReferenceType *FnTypeRef = |
| 5735 | FnType->getAs<RValueReferenceType>()) { |
| 5736 | FnType = FnTypeRef->getPointeeType(); |
| 5737 | isRValueReference = true; |
| 5738 | } |
| 5739 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5740 | FnType = FnTypePtr->getPointeeType(); |
| 5741 | isPointer = true; |
| 5742 | } |
| 5743 | // Desugar down to a function type. |
| 5744 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5745 | // Reconstruct the pointer/reference as appropriate. |
| 5746 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5747 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5748 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5749 | |
| 5750 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5751 | << FnType; |
| 5752 | } |
| 5753 | |
| 5754 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5755 | const char *Opc, |
| 5756 | SourceLocation OpLoc, |
| 5757 | OverloadCandidate *Cand) { |
| 5758 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5759 | std::string TypeStr("operator"); |
| 5760 | TypeStr += Opc; |
| 5761 | TypeStr += "("; |
| 5762 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5763 | if (Cand->Conversions.size() == 1) { |
| 5764 | TypeStr += ")"; |
| 5765 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5766 | } else { |
| 5767 | TypeStr += ", "; |
| 5768 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5769 | TypeStr += ")"; |
| 5770 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5771 | } |
| 5772 | } |
| 5773 | |
| 5774 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5775 | OverloadCandidate *Cand) { |
| 5776 | unsigned NoOperands = Cand->Conversions.size(); |
| 5777 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5778 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5779 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5780 | if (!ICS.isAmbiguous()) continue; |
| 5781 | |
| 5782 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5783 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5784 | } |
| 5785 | } |
| 5786 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5787 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5788 | if (Cand->Function) |
| 5789 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5790 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5791 | return Cand->Surrogate->getLocation(); |
| 5792 | return SourceLocation(); |
| 5793 | } |
| 5794 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5795 | struct CompareOverloadCandidatesForDisplay { |
| 5796 | Sema &S; |
| 5797 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5798 | |
| 5799 | bool operator()(const OverloadCandidate *L, |
| 5800 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5801 | // Fast-path this check. |
| 5802 | if (L == R) return false; |
| 5803 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5804 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5805 | if (L->Viable) { |
| 5806 | if (!R->Viable) return true; |
| 5807 | |
| 5808 | // TODO: introduce a tri-valued comparison for overload |
| 5809 | // candidates. Would be more worthwhile if we had a sort |
| 5810 | // that could exploit it. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5811 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 5812 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5813 | } else if (R->Viable) |
| 5814 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5815 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5816 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5817 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5818 | // Criteria by which we can sort non-viable candidates: |
| 5819 | if (!L->Viable) { |
| 5820 | // 1. Arity mismatches come after other candidates. |
| 5821 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5822 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5823 | return false; |
| 5824 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5825 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5826 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5827 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5828 | // 2. Bad conversions come first and are ordered by the number |
| 5829 | // of bad conversions and quality of good conversions. |
| 5830 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5831 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5832 | return true; |
| 5833 | |
| 5834 | // If there's any ordering between the defined conversions... |
| 5835 | // FIXME: this might not be transitive. |
| 5836 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5837 | |
| 5838 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5839 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5840 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5841 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 5842 | R->Conversions[I])) { |
| 5843 | case ImplicitConversionSequence::Better: |
| 5844 | leftBetter++; |
| 5845 | break; |
| 5846 | |
| 5847 | case ImplicitConversionSequence::Worse: |
| 5848 | leftBetter--; |
| 5849 | break; |
| 5850 | |
| 5851 | case ImplicitConversionSequence::Indistinguishable: |
| 5852 | break; |
| 5853 | } |
| 5854 | } |
| 5855 | if (leftBetter > 0) return true; |
| 5856 | if (leftBetter < 0) return false; |
| 5857 | |
| 5858 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 5859 | return false; |
| 5860 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5861 | // TODO: others? |
| 5862 | } |
| 5863 | |
| 5864 | // Sort everything else by location. |
| 5865 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 5866 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 5867 | |
| 5868 | // Put candidates without locations (e.g. builtins) at the end. |
| 5869 | if (LLoc.isInvalid()) return false; |
| 5870 | if (RLoc.isInvalid()) return true; |
| 5871 | |
| 5872 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5873 | } |
| 5874 | }; |
| 5875 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5876 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 5877 | /// computes up to the first |
| 5878 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 5879 | Expr **Args, unsigned NumArgs) { |
| 5880 | assert(!Cand->Viable); |
| 5881 | |
| 5882 | // Don't do anything on failures other than bad conversion. |
| 5883 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 5884 | |
| 5885 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5886 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5887 | unsigned ConvCount = Cand->Conversions.size(); |
| 5888 | while (true) { |
| 5889 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 5890 | ConvIdx++; |
| 5891 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 5892 | break; |
| 5893 | } |
| 5894 | |
| 5895 | if (ConvIdx == ConvCount) |
| 5896 | return; |
| 5897 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5898 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 5899 | "remaining conversion is initialized?"); |
| 5900 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 5901 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5902 | // operation somehow. |
| 5903 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5904 | |
| 5905 | const FunctionProtoType* Proto; |
| 5906 | unsigned ArgIdx = ConvIdx; |
| 5907 | |
| 5908 | if (Cand->IsSurrogate) { |
| 5909 | QualType ConvType |
| 5910 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 5911 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5912 | ConvType = ConvPtrType->getPointeeType(); |
| 5913 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 5914 | ArgIdx--; |
| 5915 | } else if (Cand->Function) { |
| 5916 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 5917 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 5918 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 5919 | ArgIdx--; |
| 5920 | } else { |
| 5921 | // Builtin binary operator with a bad first conversion. |
| 5922 | assert(ConvCount <= 3); |
| 5923 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 5924 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5925 | = TryCopyInitialization(S, Args[ConvIdx], |
| 5926 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 5927 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5928 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5929 | return; |
| 5930 | } |
| 5931 | |
| 5932 | // Fill in the rest of the conversions. |
| 5933 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5934 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 5935 | if (ArgIdx < NumArgsInProto) |
| 5936 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5937 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 5938 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5939 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5940 | else |
| 5941 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 5942 | } |
| 5943 | } |
| 5944 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5945 | } // end anonymous namespace |
| 5946 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5947 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 5948 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5949 | /// set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5950 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5951 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5952 | OverloadCandidateDisplayKind OCD, |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5953 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5954 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 5955 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5956 | // Sort the candidates by viability and position. Sorting directly would |
| 5957 | // be prohibitive, so we make a set of pointers and sort those. |
| 5958 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 5959 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 5960 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5961 | LastCand = CandidateSet.end(); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5962 | Cand != LastCand; ++Cand) { |
| 5963 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5964 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5965 | else if (OCD == OCD_AllCandidates) { |
| 5966 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5967 | if (Cand->Function || Cand->IsSurrogate) |
| 5968 | Cands.push_back(Cand); |
| 5969 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 5970 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5971 | } |
| 5972 | } |
| 5973 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5974 | std::sort(Cands.begin(), Cands.end(), |
| 5975 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5976 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5977 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5978 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5979 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5980 | const Diagnostic::OverloadsShown ShowOverloads = Diags.getShowOverloads(); |
| 5981 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5982 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 5983 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 5984 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5985 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 5986 | // the user with. FIXME: This limit should depend on details of the |
| 5987 | // candidate list. |
| 5988 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 5989 | break; |
| 5990 | } |
| 5991 | ++CandsShown; |
| 5992 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5993 | if (Cand->Function) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5994 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5995 | else if (Cand->IsSurrogate) |
| 5996 | NoteSurrogateCandidate(*this, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5997 | else { |
| 5998 | assert(Cand->Viable && |
| 5999 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6000 | // Generally we only see ambiguities including viable builtin |
| 6001 | // operators if overload resolution got screwed up by an |
| 6002 | // ambiguous user-defined conversion. |
| 6003 | // |
| 6004 | // FIXME: It's quite possible for different conversions to see |
| 6005 | // different ambiguities, though. |
| 6006 | if (!ReportedAmbiguousConversions) { |
| 6007 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 6008 | ReportedAmbiguousConversions = true; |
| 6009 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6010 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6011 | // If this is a viable builtin, print it. |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6012 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6013 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6014 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6015 | |
| 6016 | if (I != E) |
Jeffrey Yasskin | 258de30 | 2010-06-11 06:58:43 +0000 | [diff] [blame] | 6017 | Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6020 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6021 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6022 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6023 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6024 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6027 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 6028 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 6029 | /// expression with overloaded function type and @p ToType is the type |
| 6030 | /// we're trying to resolve to. For example: |
| 6031 | /// |
| 6032 | /// @code |
| 6033 | /// int f(double); |
| 6034 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6035 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6036 | /// int (*pfd)(double) = f; // selects f(double) |
| 6037 | /// @endcode |
| 6038 | /// |
| 6039 | /// This routine returns the resulting FunctionDecl if it could be |
| 6040 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 6041 | /// routine will emit diagnostics if there is an error. |
| 6042 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6043 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6044 | bool Complain, |
| 6045 | DeclAccessPair &FoundResult) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6046 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6047 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6048 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6049 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6050 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 6051 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6052 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6053 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6054 | FunctionType = MemTypePtr->getPointeeType(); |
| 6055 | IsMember = true; |
| 6056 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6057 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6058 | // C++ [over.over]p1: |
| 6059 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6060 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6061 | // C++ [over.over]p1: |
| 6062 | // [...] The overloaded function name can be preceded by the & |
| 6063 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6064 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 6065 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 6066 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 6067 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 6068 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6069 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6070 | |
| 6071 | // We expect a pointer or reference to function, or a function pointer. |
| 6072 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 6073 | if (!FunctionType->isFunctionType()) { |
| 6074 | if (Complain) |
| 6075 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 6076 | << OvlExpr->getName() << ToType; |
| 6077 | |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
| 6081 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6082 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6083 | // Look through all of the overloaded functions, searching for one |
| 6084 | // whose type matches exactly. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6085 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6086 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 6087 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6088 | bool FoundNonTemplateFunction = false; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6089 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6090 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6091 | // Look through any using declarations to find the underlying function. |
| 6092 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 6093 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6094 | // C++ [over.over]p3: |
| 6095 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 6096 | // targets of type "pointer-to-function" or "reference-to-function." |
| 6097 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6098 | // type "pointer-to-member-function." |
| 6099 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6100 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6101 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6102 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6103 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6104 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6105 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6106 | // static when converting to member pointer. |
| 6107 | if (Method->isStatic() == IsMember) |
| 6108 | continue; |
| 6109 | } else if (IsMember) |
| 6110 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6111 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6112 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6113 | // If the name is a function template, template argument deduction is |
| 6114 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6115 | // resulting template argument list is used to generate a single |
| 6116 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6117 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6118 | // 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] | 6119 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6120 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6121 | if (TemplateDeductionResult Result |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6122 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6123 | FunctionType, Specialization, Info)) { |
| 6124 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6125 | (void)Result; |
| 6126 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6127 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 6128 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6129 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6130 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6131 | Matches.push_back(std::make_pair(I.getPair(), |
| 6132 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6133 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6134 | |
| 6135 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6136 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6137 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6138 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6139 | // Skip non-static functions when converting to pointer, and static |
| 6140 | // when converting to member pointer. |
| 6141 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6142 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6143 | |
| 6144 | // If we have explicit template arguments, skip non-templates. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6145 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6146 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6147 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6148 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6149 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6150 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 6151 | QualType ResultTy; |
| 6152 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 6153 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 6154 | ResultTy)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6155 | Matches.push_back(std::make_pair(I.getPair(), |
| 6156 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6157 | FoundNonTemplateFunction = true; |
| 6158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6160 | } |
| 6161 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6162 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6163 | if (Matches.empty()) { |
| 6164 | if (Complain) { |
| 6165 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 6166 | << OvlExpr->getName() << FunctionType; |
| 6167 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6168 | E = OvlExpr->decls_end(); |
| 6169 | I != E; ++I) |
| 6170 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 6171 | NoteOverloadCandidate(F); |
| 6172 | } |
| 6173 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6174 | return 0; |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6175 | } else if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6176 | FunctionDecl *Result = Matches[0].second; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6177 | FoundResult = Matches[0].first; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6178 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6179 | if (Complain) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6180 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6181 | return Result; |
| 6182 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6183 | |
| 6184 | // C++ [over.over]p4: |
| 6185 | // If more than one function is selected, [...] |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6186 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6187 | // [...] and any given function template specialization F1 is |
| 6188 | // eliminated if the set contains a second function template |
| 6189 | // specialization whose function template is more specialized |
| 6190 | // than the function template of F1 according to the partial |
| 6191 | // ordering rules of 14.5.5.2. |
| 6192 | |
| 6193 | // The algorithm specified above is quadratic. We instead use a |
| 6194 | // two-pass algorithm (similar to the one used to identify the |
| 6195 | // best viable function in an overload set) that identifies the |
| 6196 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6197 | |
| 6198 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 6199 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6200 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6201 | |
| 6202 | UnresolvedSetIterator Result = |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6203 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6204 | TPOC_Other, From->getLocStart(), |
| 6205 | PDiag(), |
| 6206 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6207 | << Matches[0].second->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6208 | PDiag(diag::note_ovl_candidate) |
| 6209 | << (unsigned) oc_function_template); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6210 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6211 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6212 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6213 | if (Complain) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6214 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6215 | DiagnoseUseOfDecl(FoundResult, OvlExpr->getNameLoc()); |
| 6216 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6217 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6219 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6220 | // [...] any function template specializations in the set are |
| 6221 | // eliminated if the set also contains a non-template function, [...] |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6222 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6223 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6224 | ++I; |
| 6225 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6226 | Matches[I] = Matches[--N]; |
| 6227 | Matches.set_size(N); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6228 | } |
| 6229 | } |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6230 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6231 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6232 | // selected function. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6233 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6234 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6235 | FoundResult = Matches[0].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6236 | if (Complain) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6237 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6238 | DiagnoseUseOfDecl(Matches[0].first, OvlExpr->getNameLoc()); |
| 6239 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6240 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6242 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6243 | // FIXME: We should probably return the same thing that BestViableFunction |
| 6244 | // returns (even if we issue the diagnostics here). |
| 6245 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6246 | << Matches[0].second->getDeclName(); |
| 6247 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6248 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6249 | return 0; |
| 6250 | } |
| 6251 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6252 | /// \brief Given an expression that refers to an overloaded function, try to |
| 6253 | /// resolve that overloaded function expression down to a single function. |
| 6254 | /// |
| 6255 | /// This routine can only resolve template-ids that refer to a single function |
| 6256 | /// template, where that template-id refers to a single template whose template |
| 6257 | /// arguments are either provided by the template-id or have defaults, |
| 6258 | /// as described in C++0x [temp.arg.explicit]p3. |
| 6259 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 6260 | // C++ [over.over]p1: |
| 6261 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6262 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6263 | // C++ [over.over]p1: |
| 6264 | // [...] The overloaded function name can be preceded by the & |
| 6265 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6266 | |
| 6267 | if (From->getType() != Context.OverloadTy) |
| 6268 | return 0; |
| 6269 | |
| 6270 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6271 | |
| 6272 | // If we didn't actually find any template-ids, we're done. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6273 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6274 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6275 | |
| 6276 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 6277 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6278 | |
| 6279 | // Look through all of the overloaded functions, searching for one |
| 6280 | // whose type matches exactly. |
| 6281 | FunctionDecl *Matched = 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6282 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6283 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6284 | // C++0x [temp.arg.explicit]p3: |
| 6285 | // [...] In contexts where deduction is done and fails, or in contexts |
| 6286 | // where deduction is not done, if a template argument list is |
| 6287 | // specified and it, along with any default template arguments, |
| 6288 | // identifies a single function template specialization, then the |
| 6289 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 6290 | FunctionTemplateDecl *FunctionTemplate |
| 6291 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6292 | |
| 6293 | // C++ [over.over]p2: |
| 6294 | // If the name is a function template, template argument deduction is |
| 6295 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6296 | // resulting template argument list is used to generate a single |
| 6297 | // function template specialization, which is added to the set of |
| 6298 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6299 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6300 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6301 | if (TemplateDeductionResult Result |
| 6302 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 6303 | Specialization, Info)) { |
| 6304 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6305 | (void)Result; |
| 6306 | continue; |
| 6307 | } |
| 6308 | |
| 6309 | // Multiple matches; we can't resolve to a single declaration. |
| 6310 | if (Matched) |
| 6311 | return 0; |
| 6312 | |
| 6313 | Matched = Specialization; |
| 6314 | } |
| 6315 | |
| 6316 | return Matched; |
| 6317 | } |
| 6318 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6319 | /// \brief Add a single candidate to the overload set. |
| 6320 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6321 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6322 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6323 | Expr **Args, unsigned NumArgs, |
| 6324 | OverloadCandidateSet &CandidateSet, |
| 6325 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6326 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6327 | if (isa<UsingShadowDecl>(Callee)) |
| 6328 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 6329 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6330 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6331 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6332 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6333 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6334 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | if (FunctionTemplateDecl *FuncTemplate |
| 6338 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6339 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 6340 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6341 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6342 | return; |
| 6343 | } |
| 6344 | |
| 6345 | assert(false && "unhandled case in overloaded call candidate"); |
| 6346 | |
| 6347 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 6351 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6352 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6353 | Expr **Args, unsigned NumArgs, |
| 6354 | OverloadCandidateSet &CandidateSet, |
| 6355 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6356 | |
| 6357 | #ifndef NDEBUG |
| 6358 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 6359 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6360 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6361 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 6362 | // and let Y be the lookup set produced by argument dependent |
| 6363 | // lookup (defined as follows). If X contains |
| 6364 | // |
| 6365 | // -- a declaration of a class member, or |
| 6366 | // |
| 6367 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6368 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6369 | // |
| 6370 | // -- a declaration that is neither a function or a function |
| 6371 | // template |
| 6372 | // |
| 6373 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6374 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6375 | if (ULE->requiresADL()) { |
| 6376 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6377 | E = ULE->decls_end(); I != E; ++I) { |
| 6378 | assert(!(*I)->getDeclContext()->isRecord()); |
| 6379 | assert(isa<UsingShadowDecl>(*I) || |
| 6380 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 6381 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6382 | } |
| 6383 | } |
| 6384 | #endif |
| 6385 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6386 | // It would be nice to avoid this copy. |
| 6387 | TemplateArgumentListInfo TABuffer; |
| 6388 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6389 | if (ULE->hasExplicitTemplateArgs()) { |
| 6390 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6391 | ExplicitTemplateArgs = &TABuffer; |
| 6392 | } |
| 6393 | |
| 6394 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6395 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6396 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6397 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6398 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6399 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6400 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6401 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 6402 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6403 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6404 | CandidateSet, |
| 6405 | PartialOverloading); |
| 6406 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6407 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6408 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 6409 | Expr **Args, unsigned NumArgs) { |
| 6410 | Fn->Destroy(SemaRef.Context); |
| 6411 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 6412 | Args[Arg]->Destroy(SemaRef.Context); |
| 6413 | return SemaRef.ExprError(); |
| 6414 | } |
| 6415 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6416 | /// Attempts to recover from a call where no functions were found. |
| 6417 | /// |
| 6418 | /// Returns true if new candidates were found. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6419 | static Sema::OwningExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6420 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6421 | UnresolvedLookupExpr *ULE, |
| 6422 | SourceLocation LParenLoc, |
| 6423 | Expr **Args, unsigned NumArgs, |
| 6424 | SourceLocation *CommaLocs, |
| 6425 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6426 | |
| 6427 | CXXScopeSpec SS; |
| 6428 | if (ULE->getQualifier()) { |
| 6429 | SS.setScopeRep(ULE->getQualifier()); |
| 6430 | SS.setRange(ULE->getQualifierRange()); |
| 6431 | } |
| 6432 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6433 | TemplateArgumentListInfo TABuffer; |
| 6434 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6435 | if (ULE->hasExplicitTemplateArgs()) { |
| 6436 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6437 | ExplicitTemplateArgs = &TABuffer; |
| 6438 | } |
| 6439 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6440 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 6441 | Sema::LookupOrdinaryName); |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 6442 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6443 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6444 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6445 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 6446 | |
| 6447 | // Build an implicit member call if appropriate. Just drop the |
| 6448 | // casts and such from the call, we don't really care. |
| 6449 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 6450 | if ((*R.begin())->isCXXClassMember()) |
| 6451 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 6452 | else if (ExplicitTemplateArgs) |
| 6453 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 6454 | else |
| 6455 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 6456 | |
| 6457 | if (NewFn.isInvalid()) |
| 6458 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 6459 | |
| 6460 | Fn->Destroy(SemaRef.Context); |
| 6461 | |
| 6462 | // This shouldn't cause an infinite loop because we're giving it |
| 6463 | // an expression with non-empty lookup results, which should never |
| 6464 | // end up here. |
| 6465 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 6466 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 6467 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6468 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 6469 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6470 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6471 | /// (which eventually refers to the declaration Func) and the call |
| 6472 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 6473 | /// to a specific function. If overload resolution succeeds, returns |
| 6474 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 6475 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6476 | /// arguments and Fn, and returns NULL. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6477 | Sema::OwningExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6478 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6479 | SourceLocation LParenLoc, |
| 6480 | Expr **Args, unsigned NumArgs, |
| 6481 | SourceLocation *CommaLocs, |
| 6482 | SourceLocation RParenLoc) { |
| 6483 | #ifndef NDEBUG |
| 6484 | if (ULE->requiresADL()) { |
| 6485 | // To do ADL, we must have found an unqualified name. |
| 6486 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 6487 | |
| 6488 | // We don't perform ADL for implicit declarations of builtins. |
| 6489 | // Verify that this was correctly set up. |
| 6490 | FunctionDecl *F; |
| 6491 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 6492 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 6493 | F->getBuiltinID() && F->isImplicit()) |
| 6494 | assert(0 && "performing ADL for builtin"); |
| 6495 | |
| 6496 | // We don't perform ADL in C. |
| 6497 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 6498 | } |
| 6499 | #endif |
| 6500 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6501 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 6502 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6503 | // Add the functions denoted by the callee to the set of candidate |
| 6504 | // functions, including those from argument-dependent lookup. |
| 6505 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6506 | |
| 6507 | // If we found nothing, try to recover. |
| 6508 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 6509 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6510 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6511 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6512 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6513 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6514 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6515 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6516 | case OR_Success: { |
| 6517 | FunctionDecl *FDecl = Best->Function; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6518 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6519 | DiagnoseUseOfDecl(Best->FoundDecl, ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6520 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6521 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 6522 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6523 | |
| 6524 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 6525 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6526 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6527 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6528 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6529 | break; |
| 6530 | |
| 6531 | case OR_Ambiguous: |
| 6532 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6533 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6534 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6535 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6536 | |
| 6537 | case OR_Deleted: |
| 6538 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 6539 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6540 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6541 | << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6542 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6543 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
| 6546 | // Overload resolution failed. Destroy all of the subexpressions and |
| 6547 | // return NULL. |
| 6548 | Fn->Destroy(Context); |
| 6549 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 6550 | Args[Arg]->Destroy(Context); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6551 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6554 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6555 | return Functions.size() > 1 || |
| 6556 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 6557 | } |
| 6558 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6559 | /// \brief Create a unary operation that may resolve to an overloaded |
| 6560 | /// operator. |
| 6561 | /// |
| 6562 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 6563 | /// |
| 6564 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 6565 | /// operator. |
| 6566 | /// |
| 6567 | /// \param Functions The set of non-member functions that will be |
| 6568 | /// considered by overload resolution. The caller needs to build this |
| 6569 | /// set based on the context using, e.g., |
| 6570 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6571 | /// set should not contain any member functions; those will be added |
| 6572 | /// by CreateOverloadedUnaryOp(). |
| 6573 | /// |
| 6574 | /// \param input The input argument. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6575 | Sema::OwningExprResult |
| 6576 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 6577 | const UnresolvedSetImpl &Fns, |
| 6578 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6579 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 6580 | Expr *Input = (Expr *)input.get(); |
| 6581 | |
| 6582 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 6583 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 6584 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6585 | |
| 6586 | Expr *Args[2] = { Input, 0 }; |
| 6587 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6588 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6589 | // For post-increment and post-decrement, add the implicit '0' as |
| 6590 | // the second argument, so that we know this is a post-increment or |
| 6591 | // post-decrement. |
| 6592 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 6593 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6594 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6595 | SourceLocation()); |
| 6596 | NumArgs = 2; |
| 6597 | } |
| 6598 | |
| 6599 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 6600 | if (Fns.empty()) |
| 6601 | return Owned(new (Context) UnaryOperator(input.takeAs<Expr>(), |
| 6602 | Opc, |
| 6603 | Context.DependentTy, |
| 6604 | OpLoc)); |
| 6605 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6606 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6607 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6608 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6609 | 0, SourceRange(), OpName, OpLoc, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6610 | /*ADL*/ true, IsOverloaded(Fns), |
| 6611 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6612 | input.release(); |
| 6613 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 6614 | &Args[0], NumArgs, |
| 6615 | Context.DependentTy, |
| 6616 | OpLoc)); |
| 6617 | } |
| 6618 | |
| 6619 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6620 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6621 | |
| 6622 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6623 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6624 | |
| 6625 | // Add operator candidates that are member functions. |
| 6626 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 6627 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6628 | // Add candidates from ADL. |
| 6629 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 6630 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6631 | /*ExplicitTemplateArgs*/ 0, |
| 6632 | CandidateSet); |
| 6633 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6634 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6635 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6636 | |
| 6637 | // Perform overload resolution. |
| 6638 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6639 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6640 | case OR_Success: { |
| 6641 | // We found a built-in operator or an overloaded operator. |
| 6642 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6643 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6644 | if (FnDecl) { |
| 6645 | // We matched an overloaded operator. Build a call to that |
| 6646 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6647 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6648 | // Convert the arguments. |
| 6649 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6650 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6651 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6652 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 6653 | Best->FoundDecl, Method)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6654 | return ExprError(); |
| 6655 | } else { |
| 6656 | // Convert the arguments. |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6657 | OwningExprResult InputInit |
| 6658 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6659 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6660 | SourceLocation(), |
| 6661 | move(input)); |
| 6662 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6663 | return ExprError(); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6665 | input = move(InputInit); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6666 | Input = (Expr *)input.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6667 | } |
| 6668 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6669 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6670 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6671 | // Determine the result type |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 6672 | QualType ResultTy = FnDecl->getCallResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6673 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6674 | // Build the actual expression node. |
| 6675 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6676 | SourceLocation()); |
| 6677 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6678 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6679 | input.release(); |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6680 | Args[0] = Input; |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6681 | ExprOwningPtr<CallExpr> TheCall(this, |
| 6682 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6683 | Args, NumArgs, ResultTy, OpLoc)); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6684 | |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6685 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6686 | FnDecl)) |
| 6687 | return ExprError(); |
| 6688 | |
| 6689 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6690 | } else { |
| 6691 | // We matched a built-in operator. Convert the arguments, then |
| 6692 | // break out so that we will build the appropriate built-in |
| 6693 | // operator node. |
| 6694 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6695 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6696 | return ExprError(); |
| 6697 | |
| 6698 | break; |
| 6699 | } |
| 6700 | } |
| 6701 | |
| 6702 | case OR_No_Viable_Function: |
| 6703 | // No viable function; fall through to handling this as a |
| 6704 | // built-in operator, which will produce an error message for us. |
| 6705 | break; |
| 6706 | |
| 6707 | case OR_Ambiguous: |
| 6708 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6709 | << UnaryOperator::getOpcodeStr(Opc) |
| 6710 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6711 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6712 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6713 | return ExprError(); |
| 6714 | |
| 6715 | case OR_Deleted: |
| 6716 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6717 | << Best->Function->isDeleted() |
| 6718 | << UnaryOperator::getOpcodeStr(Opc) |
| 6719 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6720 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6721 | return ExprError(); |
| 6722 | } |
| 6723 | |
| 6724 | // Either we found no viable overloaded operator or we matched a |
| 6725 | // built-in operator. In either case, fall through to trying to |
| 6726 | // build a built-in operation. |
| 6727 | input.release(); |
| 6728 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 6729 | } |
| 6730 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6731 | /// \brief Create a binary operation that may resolve to an overloaded |
| 6732 | /// operator. |
| 6733 | /// |
| 6734 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 6735 | /// |
| 6736 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 6737 | /// operator. |
| 6738 | /// |
| 6739 | /// \param Functions The set of non-member functions that will be |
| 6740 | /// considered by overload resolution. The caller needs to build this |
| 6741 | /// set based on the context using, e.g., |
| 6742 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6743 | /// set should not contain any member functions; those will be added |
| 6744 | /// by CreateOverloadedBinOp(). |
| 6745 | /// |
| 6746 | /// \param LHS Left-hand argument. |
| 6747 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6748 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6749 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6750 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6751 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6752 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6753 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6754 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6755 | |
| 6756 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 6757 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 6758 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6759 | |
| 6760 | // If either side is type-dependent, create an appropriate dependent |
| 6761 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6762 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6763 | if (Fns.empty()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6764 | // If there are no functions to store, just build a dependent |
| 6765 | // BinaryOperator or CompoundAssignment. |
| 6766 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 6767 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6768 | Context.DependentTy, OpLoc)); |
| 6769 | |
| 6770 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6771 | Context.DependentTy, |
| 6772 | Context.DependentTy, |
| 6773 | Context.DependentTy, |
| 6774 | OpLoc)); |
| 6775 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6776 | |
| 6777 | // FIXME: save results of ADL from here? |
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, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6781 | 0, SourceRange(), OpName, OpLoc, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6782 | /*ADL*/ true, IsOverloaded(Fns), |
| 6783 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6784 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6785 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6786 | Context.DependentTy, |
| 6787 | OpLoc)); |
| 6788 | } |
| 6789 | |
| 6790 | // If this is the .* operator, which is not overloadable, just |
| 6791 | // create a built-in binary operator. |
| 6792 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6793 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6794 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6795 | // If this is the assignment operator, we only perform overload resolution |
| 6796 | // if the left-hand side is a class or enumeration type. This is actually |
| 6797 | // a hack. The standard requires that we do overload resolution between the |
| 6798 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6799 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6800 | // Note that we go the traditional code path for compound assignment forms. |
| 6801 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6802 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6803 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6804 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6805 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6806 | |
| 6807 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6808 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6809 | |
| 6810 | // Add operator candidates that are member functions. |
| 6811 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6812 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6813 | // Add candidates from ADL. |
| 6814 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6815 | Args, 2, |
| 6816 | /*ExplicitTemplateArgs*/ 0, |
| 6817 | CandidateSet); |
| 6818 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6819 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6820 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6821 | |
| 6822 | // Perform overload resolution. |
| 6823 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6824 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6825 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6826 | // We found a built-in operator or an overloaded operator. |
| 6827 | FunctionDecl *FnDecl = Best->Function; |
| 6828 | |
| 6829 | if (FnDecl) { |
| 6830 | // We matched an overloaded operator. Build a call to that |
| 6831 | // operator. |
| 6832 | |
| 6833 | // Convert the arguments. |
| 6834 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6835 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6836 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6837 | |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6838 | OwningExprResult Arg1 |
| 6839 | = PerformCopyInitialization( |
| 6840 | InitializedEntity::InitializeParameter( |
| 6841 | FnDecl->getParamDecl(0)), |
| 6842 | SourceLocation(), |
| 6843 | Owned(Args[1])); |
| 6844 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6845 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6846 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6847 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6848 | Best->FoundDecl, Method)) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6849 | return ExprError(); |
| 6850 | |
| 6851 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6852 | } else { |
| 6853 | // Convert the arguments. |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6854 | OwningExprResult Arg0 |
| 6855 | = PerformCopyInitialization( |
| 6856 | InitializedEntity::InitializeParameter( |
| 6857 | FnDecl->getParamDecl(0)), |
| 6858 | SourceLocation(), |
| 6859 | Owned(Args[0])); |
| 6860 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6861 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6862 | |
| 6863 | OwningExprResult Arg1 |
| 6864 | = PerformCopyInitialization( |
| 6865 | InitializedEntity::InitializeParameter( |
| 6866 | FnDecl->getParamDecl(1)), |
| 6867 | SourceLocation(), |
| 6868 | Owned(Args[1])); |
| 6869 | if (Arg1.isInvalid()) |
| 6870 | return ExprError(); |
| 6871 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 6872 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6875 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6876 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6877 | // Determine the result type |
| 6878 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 6879 | = FnDecl->getType()->getAs<FunctionType>() |
| 6880 | ->getCallResultType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6881 | |
| 6882 | // Build the actual expression node. |
| 6883 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 6884 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6885 | UsualUnaryConversions(FnExpr); |
| 6886 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6887 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6888 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 6889 | Args, 2, ResultTy, |
| 6890 | OpLoc)); |
| 6891 | |
| 6892 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6893 | FnDecl)) |
| 6894 | return ExprError(); |
| 6895 | |
| 6896 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6897 | } else { |
| 6898 | // We matched a built-in operator. Convert the arguments, then |
| 6899 | // break out so that we will build the appropriate built-in |
| 6900 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6901 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6902 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6903 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6904 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6905 | return ExprError(); |
| 6906 | |
| 6907 | break; |
| 6908 | } |
| 6909 | } |
| 6910 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6911 | case OR_No_Viable_Function: { |
| 6912 | // C++ [over.match.oper]p9: |
| 6913 | // If the operator is the operator , [...] and there are no |
| 6914 | // viable functions, then the operator is assumed to be the |
| 6915 | // built-in operator and interpreted according to clause 5. |
| 6916 | if (Opc == BinaryOperator::Comma) |
| 6917 | break; |
| 6918 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6919 | // For class as left operand for assignment or compound assigment operator |
| 6920 | // do not fall through to handling in built-in, but report that no overloaded |
| 6921 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6922 | OwningExprResult Result = ExprError(); |
| 6923 | if (Args[0]->getType()->isRecordType() && |
| 6924 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6925 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6926 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6927 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6928 | } else { |
| 6929 | // No viable function; try to create a built-in operation, which will |
| 6930 | // produce an error. Then, show the non-viable candidates. |
| 6931 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6932 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6933 | assert(Result.isInvalid() && |
| 6934 | "C++ binary operator overloading is missing candidates!"); |
| 6935 | if (Result.isInvalid()) |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6936 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6937 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6938 | return move(Result); |
| 6939 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6940 | |
| 6941 | case OR_Ambiguous: |
| 6942 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6943 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6944 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6945 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6946 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6947 | return ExprError(); |
| 6948 | |
| 6949 | case OR_Deleted: |
| 6950 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6951 | << Best->Function->isDeleted() |
| 6952 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6953 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6954 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6955 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6956 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6957 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6958 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6959 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6960 | } |
| 6961 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6962 | Action::OwningExprResult |
| 6963 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 6964 | SourceLocation RLoc, |
| 6965 | ExprArg Base, ExprArg Idx) { |
| 6966 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 6967 | static_cast<Expr*>(Idx.get()) }; |
| 6968 | DeclarationName OpName = |
| 6969 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 6970 | |
| 6971 | // If either side is type-dependent, create an appropriate dependent |
| 6972 | // expression. |
| 6973 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 6974 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6975 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6976 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6977 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6978 | 0, SourceRange(), OpName, LLoc, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6979 | /*ADL*/ true, /*Overloaded*/ false, |
| 6980 | UnresolvedSetIterator(), |
| 6981 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6982 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6983 | |
| 6984 | Base.release(); |
| 6985 | Idx.release(); |
| 6986 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 6987 | Args, 2, |
| 6988 | Context.DependentTy, |
| 6989 | RLoc)); |
| 6990 | } |
| 6991 | |
| 6992 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6993 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6994 | |
| 6995 | // Subscript can only be overloaded as a member function. |
| 6996 | |
| 6997 | // Add operator candidates that are member functions. |
| 6998 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6999 | |
| 7000 | // Add builtin operator candidates. |
| 7001 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7002 | |
| 7003 | // Perform overload resolution. |
| 7004 | OverloadCandidateSet::iterator Best; |
| 7005 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 7006 | case OR_Success: { |
| 7007 | // We found a built-in operator or an overloaded operator. |
| 7008 | FunctionDecl *FnDecl = Best->Function; |
| 7009 | |
| 7010 | if (FnDecl) { |
| 7011 | // We matched an overloaded operator. Build a call to that |
| 7012 | // operator. |
| 7013 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7014 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7015 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7016 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7017 | // Convert the arguments. |
| 7018 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7019 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7020 | Best->FoundDecl, Method)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7021 | return ExprError(); |
| 7022 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 7023 | // Convert the arguments. |
| 7024 | OwningExprResult InputInit |
| 7025 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7026 | FnDecl->getParamDecl(0)), |
| 7027 | SourceLocation(), |
| 7028 | Owned(Args[1])); |
| 7029 | if (InputInit.isInvalid()) |
| 7030 | return ExprError(); |
| 7031 | |
| 7032 | Args[1] = InputInit.takeAs<Expr>(); |
| 7033 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7034 | // Determine the result type |
| 7035 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7036 | = FnDecl->getType()->getAs<FunctionType>() |
| 7037 | ->getCallResultType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7038 | |
| 7039 | // Build the actual expression node. |
| 7040 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 7041 | LLoc); |
| 7042 | UsualUnaryConversions(FnExpr); |
| 7043 | |
| 7044 | Base.release(); |
| 7045 | Idx.release(); |
| 7046 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7047 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 7048 | FnExpr, Args, 2, |
| 7049 | ResultTy, RLoc)); |
| 7050 | |
| 7051 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 7052 | FnDecl)) |
| 7053 | return ExprError(); |
| 7054 | |
| 7055 | return MaybeBindToTemporary(TheCall.release()); |
| 7056 | } else { |
| 7057 | // We matched a built-in operator. Convert the arguments, then |
| 7058 | // break out so that we will build the appropriate built-in |
| 7059 | // operator node. |
| 7060 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7061 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7062 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7063 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7064 | return ExprError(); |
| 7065 | |
| 7066 | break; |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7071 | if (CandidateSet.empty()) |
| 7072 | Diag(LLoc, diag::err_ovl_no_oper) |
| 7073 | << Args[0]->getType() << /*subscript*/ 0 |
| 7074 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 7075 | else |
| 7076 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 7077 | << Args[0]->getType() |
| 7078 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7079 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7080 | "[]", LLoc); |
| 7081 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
| 7084 | case OR_Ambiguous: |
| 7085 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 7086 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7087 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7088 | "[]", LLoc); |
| 7089 | return ExprError(); |
| 7090 | |
| 7091 | case OR_Deleted: |
| 7092 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 7093 | << Best->Function->isDeleted() << "[]" |
| 7094 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7095 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7096 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7097 | return ExprError(); |
| 7098 | } |
| 7099 | |
| 7100 | // We matched a built-in operator; build it. |
| 7101 | Base.release(); |
| 7102 | Idx.release(); |
| 7103 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 7104 | Owned(Args[1]), RLoc); |
| 7105 | } |
| 7106 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7107 | /// BuildCallToMemberFunction - Build a call to a member |
| 7108 | /// function. MemExpr is the expression that refers to the member |
| 7109 | /// function (and includes the object parameter), Args/NumArgs are the |
| 7110 | /// arguments to the function call (not including the object |
| 7111 | /// parameter). The caller needs to validate that the member |
| 7112 | /// expression refers to a member function or an overloaded member |
| 7113 | /// function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7114 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7115 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 7116 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7117 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 7118 | SourceLocation RParenLoc) { |
| 7119 | // Dig out the member expression. This holds both the object |
| 7120 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7121 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 7122 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7123 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7124 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 7125 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7126 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7127 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 7128 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7129 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7130 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7131 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7132 | } else { |
| 7133 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7134 | Qualifier = UnresExpr->getQualifier(); |
| 7135 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7136 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7137 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7138 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7139 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7140 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7141 | // FIXME: avoid copy. |
| 7142 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7143 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 7144 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7145 | TemplateArgs = &TemplateArgsBuffer; |
| 7146 | } |
| 7147 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7148 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 7149 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 7150 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7151 | NamedDecl *Func = *I; |
| 7152 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 7153 | if (isa<UsingShadowDecl>(Func)) |
| 7154 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 7155 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7156 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7157 | // If explicit template arguments were provided, we can't call a |
| 7158 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7159 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7160 | continue; |
| 7161 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7162 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7163 | Args, NumArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7164 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7165 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7166 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7167 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7168 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7169 | CandidateSet, |
| 7170 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7171 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7173 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7174 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 7175 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7176 | OverloadCandidateSet::iterator Best; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7177 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7178 | case OR_Success: |
| 7179 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7180 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7181 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7182 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7183 | break; |
| 7184 | |
| 7185 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7186 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7187 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7188 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7189 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7190 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7191 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7192 | |
| 7193 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7194 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7195 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7196 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7197 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7198 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7199 | |
| 7200 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7201 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7202 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7203 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7204 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7205 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7206 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7207 | } |
| 7208 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7209 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7210 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7211 | // If overload resolution picked a static member, build a |
| 7212 | // non-member call based on that function. |
| 7213 | if (Method->isStatic()) { |
| 7214 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 7215 | Args, NumArgs, RParenLoc); |
| 7216 | } |
| 7217 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7218 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7219 | } |
| 7220 | |
| 7221 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7222 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7223 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7224 | NumArgs, |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7225 | Method->getCallResultType(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7226 | RParenLoc)); |
| 7227 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7228 | // Check for a valid return type. |
| 7229 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 7230 | TheCall.get(), Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7231 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7232 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7233 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7234 | // We only need to do this if there was actually an overload; otherwise |
| 7235 | // it was done at lookup. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7236 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7237 | if (!Method->isStatic() && |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7238 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 7239 | FoundDecl, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7240 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7241 | MemExpr->setBase(ObjectArg); |
| 7242 | |
| 7243 | // Convert the rest of the arguments |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 7244 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7245 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7246 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7247 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7248 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7249 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7250 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 7251 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7252 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7255 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 7256 | /// type (C++ [over.call.object]), which can end up invoking an |
| 7257 | /// overloaded function call operator (@c operator()) or performing a |
| 7258 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7259 | Sema::ExprResult |
| 7260 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 7261 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7262 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7263 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7264 | SourceLocation RParenLoc) { |
| 7265 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7266 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7267 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7268 | // C++ [over.call.object]p1: |
| 7269 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7270 | // 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] | 7271 | // candidate functions includes at least the function call |
| 7272 | // operators of T. The function call operators of T are obtained by |
| 7273 | // ordinary lookup of the name operator() in the context of |
| 7274 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7275 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7276 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7277 | |
| 7278 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7279 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7280 | << Object->getSourceRange())) |
| 7281 | return true; |
| 7282 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7283 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 7284 | LookupQualifiedName(R, Record->getDecl()); |
| 7285 | R.suppressDiagnostics(); |
| 7286 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7287 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7288 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7289 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7290 | Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 7291 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7292 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7293 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7294 | // C++ [over.call.object]p2: |
| 7295 | // In addition, for each conversion function declared in T of the |
| 7296 | // form |
| 7297 | // |
| 7298 | // operator conversion-type-id () cv-qualifier; |
| 7299 | // |
| 7300 | // where cv-qualifier is the same cv-qualification as, or a |
| 7301 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 7302 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 7303 | // R", or the type "reference to pointer to function of |
| 7304 | // (P1,...,Pn) returning R", or the type "reference to function |
| 7305 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7306 | // is also considered as a candidate function. Similarly, |
| 7307 | // surrogate call functions are added to the set of candidate |
| 7308 | // functions for each conversion function declared in an |
| 7309 | // accessible base class provided the function is not hidden |
| 7310 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7311 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 7312 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7313 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7314 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7315 | NamedDecl *D = *I; |
| 7316 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 7317 | if (isa<UsingShadowDecl>(D)) |
| 7318 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7319 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7320 | // Skip over templated conversion functions; they aren't |
| 7321 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7322 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7323 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7324 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7325 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7326 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7327 | // Strip the reference type (if any) and then the pointer type (if |
| 7328 | // any) to get down to what might be a function type. |
| 7329 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 7330 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7331 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7332 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7333 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7334 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7335 | Object->getType(), Args, NumArgs, |
| 7336 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7337 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7338 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7339 | // Perform overload resolution. |
| 7340 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7341 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7342 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7343 | // Overload resolution succeeded; we'll build the appropriate call |
| 7344 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7345 | break; |
| 7346 | |
| 7347 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7348 | if (CandidateSet.empty()) |
| 7349 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 7350 | << Object->getType() << /*call*/ 1 |
| 7351 | << Object->getSourceRange(); |
| 7352 | else |
| 7353 | Diag(Object->getSourceRange().getBegin(), |
| 7354 | diag::err_ovl_no_viable_object_call) |
| 7355 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7356 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7357 | break; |
| 7358 | |
| 7359 | case OR_Ambiguous: |
| 7360 | Diag(Object->getSourceRange().getBegin(), |
| 7361 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7362 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7363 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7364 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7365 | |
| 7366 | case OR_Deleted: |
| 7367 | Diag(Object->getSourceRange().getBegin(), |
| 7368 | diag::err_ovl_deleted_object_call) |
| 7369 | << Best->Function->isDeleted() |
| 7370 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7371 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7372 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7373 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7374 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7375 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7376 | // We had an error; delete all of the subexpressions and return |
| 7377 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7378 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7379 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7380 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7381 | return true; |
| 7382 | } |
| 7383 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7384 | if (Best->Function == 0) { |
| 7385 | // Since there is no function declaration, this is one of the |
| 7386 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7387 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7388 | = cast<CXXConversionDecl>( |
| 7389 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 7390 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7391 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7392 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7393 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7394 | // We selected one of the surrogate functions that converts the |
| 7395 | // object parameter to a function pointer. Perform the conversion |
| 7396 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7397 | |
| 7398 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7399 | // and then call it. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7400 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 7401 | Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7402 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7403 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7404 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
Douglas Gregor | aa0be17 | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7405 | CommaLocs, RParenLoc).result(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7406 | } |
| 7407 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7408 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7409 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7410 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7411 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 7412 | // that calls this method, using Object for the implicit object |
| 7413 | // parameter and passing along the remaining arguments. |
| 7414 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7415 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7416 | |
| 7417 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7418 | unsigned NumArgsToCheck = NumArgs; |
| 7419 | |
| 7420 | // Build the full argument list for the method call (the |
| 7421 | // implicit object parameter is placed at the beginning of the |
| 7422 | // list). |
| 7423 | Expr **MethodArgs; |
| 7424 | if (NumArgs < NumArgsInProto) { |
| 7425 | NumArgsToCheck = NumArgsInProto; |
| 7426 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 7427 | } else { |
| 7428 | MethodArgs = new Expr*[NumArgs + 1]; |
| 7429 | } |
| 7430 | MethodArgs[0] = Object; |
| 7431 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7432 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7433 | |
| 7434 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7435 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7436 | UsualUnaryConversions(NewFn); |
| 7437 | |
| 7438 | // Once we've built TheCall, all of the expressions are properly |
| 7439 | // owned. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7440 | QualType ResultTy = Method->getCallResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7441 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7442 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7443 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7444 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7445 | delete [] MethodArgs; |
| 7446 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 7447 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 7448 | Method)) |
| 7449 | return true; |
| 7450 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7451 | // We may have default arguments. If so, we need to allocate more |
| 7452 | // slots in the call for them. |
| 7453 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7454 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7455 | else if (NumArgs > NumArgsInProto) |
| 7456 | NumArgsToCheck = NumArgsInProto; |
| 7457 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7458 | bool IsError = false; |
| 7459 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7460 | // Initialize the implicit object parameter. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7461 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7462 | Best->FoundDecl, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7463 | TheCall->setArg(0, Object); |
| 7464 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7465 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7466 | // Check the argument types. |
| 7467 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7468 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7469 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7470 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7471 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7472 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7473 | |
| 7474 | OwningExprResult InputInit |
| 7475 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7476 | Method->getParamDecl(i)), |
| 7477 | SourceLocation(), Owned(Arg)); |
| 7478 | |
| 7479 | IsError |= InputInit.isInvalid(); |
| 7480 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7481 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 7482 | OwningExprResult DefArg |
| 7483 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 7484 | if (DefArg.isInvalid()) { |
| 7485 | IsError = true; |
| 7486 | break; |
| 7487 | } |
| 7488 | |
| 7489 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7490 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7491 | |
| 7492 | TheCall->setArg(i + 1, Arg); |
| 7493 | } |
| 7494 | |
| 7495 | // If this is a variadic call, handle args passed through "...". |
| 7496 | if (Proto->isVariadic()) { |
| 7497 | // Promote the arguments (C99 6.5.2.2p7). |
| 7498 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 7499 | Expr *Arg = Args[i]; |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 7500 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod, 0); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7501 | TheCall->setArg(i + 1, Arg); |
| 7502 | } |
| 7503 | } |
| 7504 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7505 | if (IsError) return true; |
| 7506 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7507 | if (CheckFunctionCall(Method, TheCall.get())) |
| 7508 | return true; |
| 7509 | |
Douglas Gregor | aa0be17 | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7510 | return MaybeBindToTemporary(TheCall.release()).result(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7511 | } |
| 7512 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7513 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7514 | /// (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] | 7515 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7516 | Sema::OwningExprResult |
| 7517 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 7518 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7519 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7520 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7521 | SourceLocation Loc = Base->getExprLoc(); |
| 7522 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7523 | // C++ [over.ref]p1: |
| 7524 | // |
| 7525 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 7526 | // for a class object x of type T if T::operator->() exists and if |
| 7527 | // the operator is selected as the best match function by the |
| 7528 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7529 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7530 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7531 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7532 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7533 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 7534 | PDiag(diag::err_typecheck_incomplete_tag) |
| 7535 | << Base->getSourceRange())) |
| 7536 | return ExprError(); |
| 7537 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7538 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7539 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 7540 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7541 | |
| 7542 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7543 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7544 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7545 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7546 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7547 | |
| 7548 | // Perform overload resolution. |
| 7549 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7550 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7551 | case OR_Success: |
| 7552 | // Overload resolution succeeded; we'll build the call below. |
| 7553 | break; |
| 7554 | |
| 7555 | case OR_No_Viable_Function: |
| 7556 | if (CandidateSet.empty()) |
| 7557 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7558 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7559 | else |
| 7560 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7561 | << "operator->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7562 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7563 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7564 | |
| 7565 | case OR_Ambiguous: |
| 7566 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7567 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7568 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7569 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7570 | |
| 7571 | case OR_Deleted: |
| 7572 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7573 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7574 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7575 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7576 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7577 | } |
| 7578 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7579 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7580 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7581 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7582 | // Convert the object parameter. |
| 7583 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7584 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 7585 | Best->FoundDecl, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7586 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 7587 | |
| 7588 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7589 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7590 | |
| 7591 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7592 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 7593 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7594 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7595 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7596 | QualType ResultTy = Method->getCallResultType(); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7597 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7598 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 7599 | &Base, 1, ResultTy, OpLoc)); |
| 7600 | |
| 7601 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 7602 | Method)) |
| 7603 | return ExprError(); |
| 7604 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7605 | } |
| 7606 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7607 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 7608 | /// a C++ overloaded function (possibly with some parentheses and |
| 7609 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 7610 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7611 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7612 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7613 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7614 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7615 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 7616 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7617 | if (SubExpr == PE->getSubExpr()) |
| 7618 | return PE->Retain(); |
| 7619 | |
| 7620 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 7621 | } |
| 7622 | |
| 7623 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7624 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 7625 | Found, Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7626 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7627 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7628 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7629 | if (SubExpr == ICE->getSubExpr()) |
| 7630 | return ICE->Retain(); |
| 7631 | |
| 7632 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 7633 | ICE->getCastKind(), |
Anders Carlsson | f1b48b7 | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 7634 | SubExpr, CXXBaseSpecifierArray(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7635 | ICE->isLvalueCast()); |
| 7636 | } |
| 7637 | |
| 7638 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7639 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7640 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7641 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7642 | if (Method->isStatic()) { |
| 7643 | // Do nothing: static member functions aren't any different |
| 7644 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7645 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7646 | // Fix the sub expression, which really has to be an |
| 7647 | // UnresolvedLookupExpr holding an overloaded member function |
| 7648 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7649 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7650 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7651 | if (SubExpr == UnOp->getSubExpr()) |
| 7652 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7653 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7654 | assert(isa<DeclRefExpr>(SubExpr) |
| 7655 | && "fixed to something other than a decl ref"); |
| 7656 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 7657 | && "fixed to a member ref with no nested name qualifier"); |
| 7658 | |
| 7659 | // We have taken the address of a pointer to member |
| 7660 | // function. Perform the computation here so that we get the |
| 7661 | // appropriate pointer to member type. |
| 7662 | QualType ClassType |
| 7663 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 7664 | QualType MemPtrType |
| 7665 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 7666 | |
| 7667 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7668 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7669 | } |
| 7670 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7671 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7672 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7673 | if (SubExpr == UnOp->getSubExpr()) |
| 7674 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7675 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7676 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7677 | Context.getPointerType(SubExpr->getType()), |
| 7678 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7679 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7680 | |
| 7681 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7682 | // FIXME: avoid copy. |
| 7683 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7684 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7685 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7686 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7687 | } |
| 7688 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7689 | return DeclRefExpr::Create(Context, |
| 7690 | ULE->getQualifier(), |
| 7691 | ULE->getQualifierRange(), |
| 7692 | Fn, |
| 7693 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7694 | Fn->getType(), |
| 7695 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7696 | } |
| 7697 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7698 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7699 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7700 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7701 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 7702 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7703 | TemplateArgs = &TemplateArgsBuffer; |
| 7704 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7705 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7706 | Expr *Base; |
| 7707 | |
| 7708 | // If we're filling in |
| 7709 | if (MemExpr->isImplicitAccess()) { |
| 7710 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 7711 | return DeclRefExpr::Create(Context, |
| 7712 | MemExpr->getQualifier(), |
| 7713 | MemExpr->getQualifierRange(), |
| 7714 | Fn, |
| 7715 | MemExpr->getMemberLoc(), |
| 7716 | Fn->getType(), |
| 7717 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 7718 | } else { |
| 7719 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 7720 | if (MemExpr->getQualifier()) |
| 7721 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 7722 | Base = new (Context) CXXThisExpr(Loc, |
| 7723 | MemExpr->getBaseType(), |
| 7724 | /*isImplicit=*/true); |
| 7725 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7726 | } else |
| 7727 | Base = MemExpr->getBase()->Retain(); |
| 7728 | |
| 7729 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7730 | MemExpr->isArrow(), |
| 7731 | MemExpr->getQualifier(), |
| 7732 | MemExpr->getQualifierRange(), |
| 7733 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7734 | Found, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7735 | MemExpr->getMemberLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7736 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7737 | Fn->getType()); |
| 7738 | } |
| 7739 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7740 | assert(false && "Invalid reference to overloaded function"); |
| 7741 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7742 | } |
| 7743 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7744 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7745 | DeclAccessPair Found, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7746 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7747 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7750 | } // end namespace clang |