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: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 318 | case Sema::TDK_Underqualified: { |
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: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 351 | case Sema::TDK_Underqualified: |
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: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 383 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 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: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 405 | case Sema::TDK_Underqualified: |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 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: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 432 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 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: |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 457 | case Sema::TDK_Underqualified: |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 458 | return &static_cast<DFIParamWithArguments*>(Data)->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 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 853 | |
| 854 | // We can perform the conversion between vector types in the following cases: |
| 855 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 856 | // 2)lax vector conversions are permitted and the vector types are of the |
| 857 | // same size |
| 858 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 859 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
| 860 | Context.getLangOptions().LaxVectorConversions && |
| 861 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType))) { |
| 862 | ICK = ICK_Vector_Conversion; |
| 863 | return true; |
| 864 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 865 | } |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 870 | /// IsStandardConversion - Determines whether there is a standard |
| 871 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 872 | /// expression From to the type ToType. Standard conversion sequences |
| 873 | /// only consider non-class types; for conversions that involve class |
| 874 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 875 | /// contain the standard conversion sequence required to perform this |
| 876 | /// conversion and this routine will return true. Otherwise, this |
| 877 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | bool |
| 879 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 880 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 882 | QualType FromType = From->getType(); |
| 883 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 884 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 885 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 886 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 887 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 888 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 889 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 890 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 891 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 893 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 894 | if (getLangOptions().CPlusPlus) |
| 895 | return false; |
| 896 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 900 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 901 | // array-to-pointer conversion, or function-to-pointer conversion |
| 902 | // (C++ 4p1). |
| 903 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 904 | if (FromType == Context.OverloadTy) { |
| 905 | DeclAccessPair AccessPair; |
| 906 | if (FunctionDecl *Fn |
| 907 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 908 | AccessPair)) { |
| 909 | // We were able to resolve the address of the overloaded function, |
| 910 | // so we can convert to the type of that function. |
| 911 | FromType = Fn->getType(); |
| 912 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 913 | if (!Method->isStatic()) { |
| 914 | Type *ClassType |
| 915 | = Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 916 | FromType = Context.getMemberPointerType(FromType, ClassType); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | // If the "from" expression takes the address of the overloaded |
| 921 | // function, update the type of the resulting expression accordingly. |
| 922 | if (FromType->getAs<FunctionType>()) |
| 923 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(From->IgnoreParens())) |
| 924 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 925 | FromType = Context.getPointerType(FromType); |
| 926 | |
| 927 | // Check that we've computed the proper type after overload resolution. |
| 928 | assert(Context.hasSameType(FromType, |
| 929 | FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
| 930 | } else { |
| 931 | return false; |
| 932 | } |
| 933 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 935 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 936 | // converted to an rvalue. |
| 937 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 939 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 940 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 941 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 942 | |
| 943 | // If T is a non-class type, the type of the rvalue is the |
| 944 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 945 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 946 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 947 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 948 | } else if (FromType->isArrayType()) { |
| 949 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 950 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 951 | |
| 952 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 953 | // bound of T" can be converted to an rvalue of type "pointer to |
| 954 | // T" (C++ 4.2p1). |
| 955 | FromType = Context.getArrayDecayedType(FromType); |
| 956 | |
| 957 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 958 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 959 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 960 | |
| 961 | // For the purpose of ranking in overload resolution |
| 962 | // (13.3.3.1.1), this conversion is considered an |
| 963 | // array-to-pointer conversion followed by a qualification |
| 964 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 965 | SCS.Second = ICK_Identity; |
| 966 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 967 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 968 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 969 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 970 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 971 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 972 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 973 | |
| 974 | // An lvalue of function type T can be converted to an rvalue of |
| 975 | // type "pointer to T." The result is a pointer to the |
| 976 | // function. (C++ 4.3p1). |
| 977 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 978 | } else { |
| 979 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 980 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 981 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 982 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 983 | |
| 984 | // The second conversion can be an integral promotion, floating |
| 985 | // point promotion, integral conversion, floating point conversion, |
| 986 | // floating-integral conversion, pointer conversion, |
| 987 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 988 | // For overloading in C, this can also be a "compatible-type" |
| 989 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 990 | bool IncompatibleObjC = false; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 991 | ImplicitConversionKind SecondICK = ICK_Identity; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 992 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 993 | // The unqualified versions of the types are the same: there's no |
| 994 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 995 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 996 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 998 | SCS.Second = ICK_Integral_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 (IsFloatingPointPromotion(FromType, ToType)) { |
| 1001 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1002 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1003 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1004 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 1005 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1006 | SCS.Second = ICK_Complex_Promotion; |
| 1007 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1008 | } else if (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1009 | ToType->isIntegralType(Context)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1010 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1011 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1012 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1013 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 1014 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1015 | SCS.Second = ICK_Complex_Conversion; |
| 1016 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1017 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 1018 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 1019 | // Complex-real conversions (C99 6.3.1.7) |
| 1020 | SCS.Second = ICK_Complex_Real; |
| 1021 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1022 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1023 | // Floating point conversions (C++ 4.8). |
| 1024 | SCS.Second = ICK_Floating_Conversion; |
| 1025 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1026 | } else if ((FromType->isRealFloatingType() && |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1027 | ToType->isIntegralType(Context) && !ToType->isBooleanType()) || |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1028 | (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1029 | ToType->isRealFloatingType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1030 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1031 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1032 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1033 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1034 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1035 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1036 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1037 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1038 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 1039 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1040 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1041 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1042 | } else if (ToType->isBooleanType() && |
| 1043 | (FromType->isArithmeticType() || |
| 1044 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 1045 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1046 | FromType->isBlockPointerType() || |
| 1047 | FromType->isMemberPointerType() || |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 1048 | FromType->isNullPtrType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1049 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1050 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1051 | FromType = Context.BoolTy; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1052 | } else if (IsVectorConversion(Context, FromType, ToType, SecondICK)) { |
| 1053 | SCS.Second = SecondICK; |
| 1054 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1056 | Context.typesAreCompatible(ToType, FromType)) { |
| 1057 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1058 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1059 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1060 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 1061 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1062 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1063 | } else { |
| 1064 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1065 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1066 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1067 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1069 | QualType CanonFrom; |
| 1070 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1071 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1072 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1073 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1074 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1075 | CanonFrom = Context.getCanonicalType(FromType); |
| 1076 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1077 | } else { |
| 1078 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1079 | SCS.Third = ICK_Identity; |
| 1080 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1082 | // [...] Any difference in top-level cv-qualification is |
| 1083 | // subsumed by the initialization itself and does not constitute |
| 1084 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1085 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1087 | if (CanonFrom.getLocalUnqualifiedType() |
| 1088 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1089 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1090 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1091 | FromType = ToType; |
| 1092 | CanonFrom = CanonTo; |
| 1093 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1094 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1095 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1096 | |
| 1097 | // If we have not converted the argument type to the parameter type, |
| 1098 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1099 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1100 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1102 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1106 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1107 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1108 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1110 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1111 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1112 | if (!To) { |
| 1113 | return false; |
| 1114 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1115 | |
| 1116 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1117 | // unsigned short int can be converted to an rvalue of type int if |
| 1118 | // int can represent all the values of the source type; otherwise, |
| 1119 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1120 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1121 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1122 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1123 | if (// We can promote any signed, promotable integer type to an int |
| 1124 | (FromType->isSignedIntegerType() || |
| 1125 | // We can promote any unsigned integer type whose size is |
| 1126 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1128 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1129 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1132 | return To->getKind() == BuiltinType::UInt; |
| 1133 | } |
| 1134 | |
| 1135 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 1136 | // can be converted to an rvalue of the first of the following types |
| 1137 | // that can represent all the values of its underlying type: int, |
| 1138 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1139 | |
| 1140 | // We pre-calculate the promotion type for enum types. |
| 1141 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 1142 | if (ToType->isIntegerType()) |
| 1143 | return Context.hasSameUnqualifiedType(ToType, |
| 1144 | FromEnumType->getDecl()->getPromotionType()); |
| 1145 | |
| 1146 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1147 | // Determine whether the type we're converting from is signed or |
| 1148 | // unsigned. |
| 1149 | bool FromIsSigned; |
| 1150 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1151 | |
| 1152 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1153 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1154 | |
| 1155 | // The types we'll try to promote to, in the appropriate |
| 1156 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | QualType PromoteTypes[6] = { |
| 1158 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1159 | Context.LongTy, Context.UnsignedLongTy , |
| 1160 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1161 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1162 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1163 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1164 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1166 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1167 | // We found the type that we can promote to. If this is the |
| 1168 | // type we wanted, we have a promotion. Otherwise, no |
| 1169 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1170 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1176 | // rvalue of type int if int can represent all the values of the |
| 1177 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1178 | // unsigned int can represent all the values of the bit-field. If |
| 1179 | // the bit-field is larger yet, no integral promotion applies to |
| 1180 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1181 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1182 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1183 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1184 | using llvm::APSInt; |
| 1185 | if (From) |
| 1186 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1187 | APSInt BitWidth; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1188 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1189 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1190 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1191 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1193 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1194 | if (BitWidth < ToSize || |
| 1195 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1196 | return To->getKind() == BuiltinType::Int; |
| 1197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1199 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1200 | // that fits into an unsigned int? |
| 1201 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1202 | return To->getKind() == BuiltinType::UInt; |
| 1203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1205 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1206 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1209 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1210 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1211 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1212 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1213 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1214 | |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1219 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1220 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1222 | /// An rvalue of type float can be converted to an rvalue of type |
| 1223 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1224 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1225 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1226 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1227 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1228 | return true; |
| 1229 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1230 | // C99 6.3.1.5p1: |
| 1231 | // When a float is promoted to double or long double, or a |
| 1232 | // double is promoted to long double [...]. |
| 1233 | if (!getLangOptions().CPlusPlus && |
| 1234 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1235 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1236 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1240 | return false; |
| 1241 | } |
| 1242 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1243 | /// \brief Determine if a conversion is a complex promotion. |
| 1244 | /// |
| 1245 | /// A complex promotion is defined as a complex -> complex conversion |
| 1246 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1247 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1248 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1249 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1250 | if (!FromComplex) |
| 1251 | return false; |
| 1252 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1253 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1254 | if (!ToComplex) |
| 1255 | return false; |
| 1256 | |
| 1257 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1258 | ToComplex->getElementType()) || |
| 1259 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1260 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1263 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1264 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1265 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1266 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1267 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | static QualType |
| 1269 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1270 | QualType ToPointee, QualType ToType, |
| 1271 | ASTContext &Context) { |
| 1272 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 1273 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1274 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1277 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1278 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1279 | if (!ToType.isNull()) |
Douglas Gregor | af7bea5 | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1280 | return ToType.getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1283 | // already. |
| 1284 | return Context.getPointerType(ToPointee); |
| 1285 | } |
| 1286 | |
| 1287 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1288 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1289 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1290 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1293 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1294 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1295 | /// not have the right set of qualifiers. |
| 1296 | static QualType |
| 1297 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1298 | QualType ToType, |
| 1299 | ASTContext &Context) { |
| 1300 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1301 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1302 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1303 | |
| 1304 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1305 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1306 | return ToType; |
| 1307 | |
| 1308 | // Just build a canonical type that has the right qualifiers. |
| 1309 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1310 | } |
| 1311 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1313 | bool InOverloadResolution, |
| 1314 | ASTContext &Context) { |
| 1315 | // Handle value-dependent integral null pointer constants correctly. |
| 1316 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1317 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1318 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1319 | return !InOverloadResolution; |
| 1320 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1321 | return Expr->isNullPointerConstant(Context, |
| 1322 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1323 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1326 | /// IsPointerConversion - Determines whether the conversion of the |
| 1327 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1328 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1329 | /// 4.10). If so, returns true and places the converted type (that |
| 1330 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1331 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1332 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1333 | /// This routine also supports conversions to and from block pointers |
| 1334 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1335 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1336 | /// appropriate overloading rules for Objective-C, we may want to |
| 1337 | /// split the Objective-C checks into a different routine; however, |
| 1338 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1339 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1340 | /// set if the conversion is an allowed Objective-C conversion that |
| 1341 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1342 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1343 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1344 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1346 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1347 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1348 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1349 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1351 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1352 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1353 | ConvertedType = ToType; |
| 1354 | return true; |
| 1355 | } |
| 1356 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1357 | // Blocks: Block pointers can be converted to void*. |
| 1358 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1359 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1360 | ConvertedType = ToType; |
| 1361 | return true; |
| 1362 | } |
| 1363 | // Blocks: A null pointer constant can be converted to a block |
| 1364 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1366 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1367 | ConvertedType = ToType; |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1371 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1372 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1374 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1375 | ConvertedType = ToType; |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1379 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1380 | if (!ToTypePtr) |
| 1381 | return false; |
| 1382 | |
| 1383 | // 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] | 1384 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1385 | ConvertedType = ToType; |
| 1386 | return true; |
| 1387 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1388 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1389 | // Beyond this point, both types need to be pointers |
| 1390 | // , including objective-c pointers. |
| 1391 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1392 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1393 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1394 | ToType, Context); |
| 1395 | return true; |
| 1396 | |
| 1397 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1398 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1399 | if (!FromTypePtr) |
| 1400 | return false; |
| 1401 | |
| 1402 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1404 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1405 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1406 | // 4.10p2). |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1407 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1408 | ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1410 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1411 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1412 | return true; |
| 1413 | } |
| 1414 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1415 | // When we're overloading in C, we allow a special kind of pointer |
| 1416 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1418 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1420 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1422 | return true; |
| 1423 | } |
| 1424 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1425 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1427 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1428 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1429 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1430 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1431 | // necessitates this conversion is ill-formed. The result of the |
| 1432 | // conversion is a pointer to the base class sub-object of the |
| 1433 | // derived class object. The null pointer value is converted to |
| 1434 | // the null pointer value of the destination type. |
| 1435 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1436 | // Note that we do not check for ambiguity or inaccessibility |
| 1437 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1438 | if (getLangOptions().CPlusPlus && |
| 1439 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1440 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1441 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1442 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1444 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1445 | ToType, Context); |
| 1446 | return true; |
| 1447 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | /// isObjCPointerConversion - Determines whether this is an |
| 1453 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1454 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1456 | QualType& ConvertedType, |
| 1457 | bool &IncompatibleObjC) { |
| 1458 | if (!getLangOptions().ObjC1) |
| 1459 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1460 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1461 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1462 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1464 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1465 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1466 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1467 | // 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] | 1468 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1469 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1470 | ConvertedType = ToType; |
| 1471 | return true; |
| 1472 | } |
| 1473 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1475 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1477 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1478 | ConvertedType = ToType; |
| 1479 | return true; |
| 1480 | } |
| 1481 | // Objective C++: We're able to convert from a pointer to an |
| 1482 | // interface to a pointer to a different interface. |
| 1483 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1484 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1485 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1486 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1487 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1488 | FromObjCPtr->getPointeeType())) |
| 1489 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1490 | ConvertedType = ToType; |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
| 1494 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1495 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1496 | // interfaces, which is permitted. However, we're going to |
| 1497 | // complain about it. |
| 1498 | IncompatibleObjC = true; |
| 1499 | ConvertedType = FromType; |
| 1500 | return true; |
| 1501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1503 | // 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] | 1504 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1505 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1506 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1507 | else if (const BlockPointerType *ToBlockPtr = |
| 1508 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1509 | // 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] | 1510 | // to a block pointer type. |
| 1511 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1512 | ConvertedType = ToType; |
| 1513 | return true; |
| 1514 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1515 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1516 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1517 | else if (FromType->getAs<BlockPointerType>() && |
| 1518 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1519 | // 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] | 1520 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1521 | ConvertedType = ToType; |
| 1522 | return true; |
| 1523 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1524 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1525 | return false; |
| 1526 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1527 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1528 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1529 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1530 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1531 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1532 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1533 | return false; |
| 1534 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1535 | // If we have pointers to pointers, recursively check whether this |
| 1536 | // is an Objective-C conversion. |
| 1537 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1538 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1539 | IncompatibleObjC)) { |
| 1540 | // We always complain about this conversion. |
| 1541 | IncompatibleObjC = true; |
| 1542 | ConvertedType = ToType; |
| 1543 | return true; |
| 1544 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1545 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1546 | // as in I* to id. |
| 1547 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1548 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1549 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1550 | IncompatibleObjC)) { |
| 1551 | ConvertedType = ToType; |
| 1552 | return true; |
| 1553 | } |
| 1554 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1555 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1556 | // differences in the argument and result types are in Objective-C |
| 1557 | // pointer conversions. If so, we permit the conversion (but |
| 1558 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1560 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1561 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1562 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1563 | if (FromFunctionType && ToFunctionType) { |
| 1564 | // If the function types are exactly the same, this isn't an |
| 1565 | // Objective-C pointer conversion. |
| 1566 | if (Context.getCanonicalType(FromPointeeType) |
| 1567 | == Context.getCanonicalType(ToPointeeType)) |
| 1568 | return false; |
| 1569 | |
| 1570 | // Perform the quick checks that will tell us whether these |
| 1571 | // function types are obviously different. |
| 1572 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1573 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1574 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1575 | return false; |
| 1576 | |
| 1577 | bool HasObjCConversion = false; |
| 1578 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1579 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1580 | // Okay, the types match exactly. Nothing to do. |
| 1581 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1582 | ToFunctionType->getResultType(), |
| 1583 | ConvertedType, IncompatibleObjC)) { |
| 1584 | // Okay, we have an Objective-C pointer conversion. |
| 1585 | HasObjCConversion = true; |
| 1586 | } else { |
| 1587 | // Function types are too different. Abort. |
| 1588 | return false; |
| 1589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1591 | // Check argument types. |
| 1592 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1593 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1594 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1595 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1596 | if (Context.getCanonicalType(FromArgType) |
| 1597 | == Context.getCanonicalType(ToArgType)) { |
| 1598 | // Okay, the types match exactly. Nothing to do. |
| 1599 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1600 | ConvertedType, IncompatibleObjC)) { |
| 1601 | // Okay, we have an Objective-C pointer conversion. |
| 1602 | HasObjCConversion = true; |
| 1603 | } else { |
| 1604 | // Argument types are too different. Abort. |
| 1605 | return false; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (HasObjCConversion) { |
| 1610 | // We had an Objective-C conversion. Allow this pointer |
| 1611 | // conversion, but complain about it. |
| 1612 | ConvertedType = ToType; |
| 1613 | IncompatibleObjC = true; |
| 1614 | return true; |
| 1615 | } |
| 1616 | } |
| 1617 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1618 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1619 | } |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1620 | |
| 1621 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1622 | /// for equlity of their argument types. Caller has already checked that |
| 1623 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1624 | /// pointer types which only differ in their protocol qualifiers are equal. |
| 1625 | bool Sema::FunctionArgTypesAreEqual(FunctionProtoType* OldType, |
| 1626 | FunctionProtoType* NewType){ |
| 1627 | if (!getLangOptions().ObjC1) |
| 1628 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1629 | NewType->arg_type_begin()); |
| 1630 | |
| 1631 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1632 | N = NewType->arg_type_begin(), |
| 1633 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1634 | QualType ToType = (*O); |
| 1635 | QualType FromType = (*N); |
| 1636 | if (ToType != FromType) { |
| 1637 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1638 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 0ee93de | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1639 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1640 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1641 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1642 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1643 | continue; |
| 1644 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1645 | else if (const ObjCObjectPointerType *PTTo = |
| 1646 | ToType->getAs<ObjCObjectPointerType>()) { |
| 1647 | if (const ObjCObjectPointerType *PTFr = |
| 1648 | FromType->getAs<ObjCObjectPointerType>()) |
| 1649 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1650 | continue; |
Fariborz Jahanian | d8d3441 | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1651 | } |
| 1652 | return false; |
| 1653 | } |
| 1654 | } |
| 1655 | return true; |
| 1656 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1658 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1659 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1660 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1661 | /// conversions for which IsPointerConversion has already returned |
| 1662 | /// true. It returns true and produces a diagnostic if there was an |
| 1663 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1664 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1665 | CastExpr::CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame^] | 1666 | CXXCastPath& BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1667 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1668 | QualType FromType = From->getType(); |
| 1669 | |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1670 | if (CXXBoolLiteralExpr* LitBool |
| 1671 | = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens())) |
| 1672 | if (LitBool->getValue() == false) |
| 1673 | Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false) |
| 1674 | << ToType; |
| 1675 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1676 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1677 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1678 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1679 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1681 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1682 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1683 | // We must have a derived-to-base conversion. Check an |
| 1684 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1685 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1686 | From->getExprLoc(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1687 | From->getSourceRange(), &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1688 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1689 | return true; |
| 1690 | |
| 1691 | // The conversion was successful. |
| 1692 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1696 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1698 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1699 | // Objective-C++ conversions are always okay. |
| 1700 | // FIXME: We should have a different class of conversions for the |
| 1701 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1702 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1703 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1704 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1705 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1706 | return false; |
| 1707 | } |
| 1708 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1709 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1710 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1711 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1712 | /// If so, returns true and places the converted type (that might differ from |
| 1713 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1714 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1715 | QualType ToType, |
| 1716 | bool InOverloadResolution, |
| 1717 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1718 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1719 | if (!ToTypePtr) |
| 1720 | return false; |
| 1721 | |
| 1722 | // 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] | 1723 | if (From->isNullPointerConstant(Context, |
| 1724 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1725 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1726 | ConvertedType = ToType; |
| 1727 | return true; |
| 1728 | } |
| 1729 | |
| 1730 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1731 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1732 | if (!FromTypePtr) |
| 1733 | return false; |
| 1734 | |
| 1735 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1736 | // where D is derived from B (C++ 4.11p2). |
| 1737 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1738 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1739 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1740 | |
| 1741 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1742 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1743 | ToClass.getTypePtr()); |
| 1744 | return true; |
| 1745 | } |
| 1746 | |
| 1747 | return false; |
| 1748 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1749 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1750 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1751 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1752 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1753 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1754 | /// true and produces a diagnostic if there was an error, or returns false |
| 1755 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1757 | CastExpr::CastKind &Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame^] | 1758 | CXXCastPath &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1759 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1760 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1761 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1762 | if (!FromPtrType) { |
| 1763 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1764 | assert(From->isNullPointerConstant(Context, |
| 1765 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1766 | "Expr must be null pointer constant!"); |
| 1767 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1768 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1769 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1770 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1771 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1772 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1773 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1774 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1775 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1776 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1777 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1778 | // FIXME: What about dependent types? |
| 1779 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1780 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1781 | |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1782 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1783 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1784 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1785 | assert(DerivationOkay && |
| 1786 | "Should not have been called if derivation isn't OK."); |
| 1787 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1788 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1789 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1790 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1791 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1792 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1793 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1794 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1795 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1797 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1798 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1799 | << FromClass << ToClass << QualType(VBase, 0) |
| 1800 | << From->getSourceRange(); |
| 1801 | return true; |
| 1802 | } |
| 1803 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1804 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1805 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1806 | Paths.front(), |
| 1807 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1808 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1809 | // Must be a base to derived member conversion. |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1810 | BuildBasePathArray(Paths, BasePath); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1811 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1812 | return false; |
| 1813 | } |
| 1814 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1815 | /// IsQualificationConversion - Determines whether the conversion from |
| 1816 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1817 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | bool |
| 1819 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1820 | FromType = Context.getCanonicalType(FromType); |
| 1821 | ToType = Context.getCanonicalType(ToType); |
| 1822 | |
| 1823 | // If FromType and ToType are the same type, this is not a |
| 1824 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1825 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1826 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1828 | // (C++ 4.4p4): |
| 1829 | // A conversion can add cv-qualifiers at levels other than the first |
| 1830 | // in multi-level pointers, subject to the following rules: [...] |
| 1831 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1832 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1833 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1834 | // Within each iteration of the loop, we check the qualifiers to |
| 1835 | // determine if this still looks like a qualification |
| 1836 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1837 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1838 | // until there are no more pointers or pointers-to-members left to |
| 1839 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1840 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1841 | |
| 1842 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1843 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1844 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1845 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1847 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1848 | // every cv for 0 < k < j. |
| 1849 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1850 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1851 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1853 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1854 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1856 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1857 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1858 | |
| 1859 | // We are left with FromType and ToType being the pointee types |
| 1860 | // after unwrapping the original FromType and ToType the same number |
| 1861 | // of types. If we unwrapped any pointers, and if FromType and |
| 1862 | // ToType have the same unqualified type (since we checked |
| 1863 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1864 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1867 | /// Determines whether there is a user-defined conversion sequence |
| 1868 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1869 | /// ToType. If such a conversion exists, User will contain the |
| 1870 | /// user-defined conversion sequence that performs such a conversion |
| 1871 | /// and this routine will return true. Otherwise, this routine returns |
| 1872 | /// false and User is unspecified. |
| 1873 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1874 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1875 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1876 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1877 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1878 | UserDefinedConversionSequence& User, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1879 | OverloadCandidateSet& CandidateSet, |
| 1880 | bool AllowExplicit) { |
| 1881 | // Whether we will only visit constructors. |
| 1882 | bool ConstructorsOnly = false; |
| 1883 | |
| 1884 | // If the type we are conversion to is a class type, enumerate its |
| 1885 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1886 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1887 | // C++ [over.match.ctor]p1: |
| 1888 | // When objects of class type are direct-initialized (8.5), or |
| 1889 | // copy-initialized from an expression of the same or a |
| 1890 | // derived class type (8.5), overload resolution selects the |
| 1891 | // constructor. [...] For copy-initialization, the candidate |
| 1892 | // functions are all the converting constructors (12.3.1) of |
| 1893 | // that class. The argument list is the expression-list within |
| 1894 | // the parentheses of the initializer. |
| 1895 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1896 | (From->getType()->getAs<RecordType>() && |
| 1897 | IsDerivedFrom(From->getType(), ToType))) |
| 1898 | ConstructorsOnly = true; |
| 1899 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1900 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1901 | // We're not going to find any constructors. |
| 1902 | } else if (CXXRecordDecl *ToRecordDecl |
| 1903 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1904 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 1905 | for (llvm::tie(Con, ConEnd) = LookupConstructors(ToRecordDecl); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1906 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1907 | NamedDecl *D = *Con; |
| 1908 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1909 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1910 | // Find the constructor (which may be a template). |
| 1911 | CXXConstructorDecl *Constructor = 0; |
| 1912 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1913 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1914 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1916 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1917 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1918 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1919 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1920 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1921 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1922 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1923 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1924 | /*ExplicitArgs*/ 0, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1925 | &From, 1, CandidateSet, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1926 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1927 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1928 | // Allow one user-defined conversion when user specifies a |
| 1929 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1930 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1931 | &From, 1, CandidateSet, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1932 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1933 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1934 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1938 | // Enumerate conversion functions, if we're allowed to. |
| 1939 | if (ConstructorsOnly) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1941 | PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1942 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1944 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1946 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1947 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1948 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1949 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1950 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1951 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1952 | DeclAccessPair FoundDecl = I.getPair(); |
| 1953 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1954 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1955 | if (isa<UsingShadowDecl>(D)) |
| 1956 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1957 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1958 | CXXConversionDecl *Conv; |
| 1959 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1960 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1961 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1962 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1963 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1966 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1967 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1968 | ActingContext, From, ToType, |
| 1969 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1970 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1971 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1972 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1976 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1977 | |
| 1978 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1979 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1980 | case OR_Success: |
| 1981 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1983 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1984 | // C++ [over.ics.user]p1: |
| 1985 | // If the user-defined conversion is specified by a |
| 1986 | // constructor (12.3.1), the initial standard conversion |
| 1987 | // sequence converts the source type to the type required by |
| 1988 | // the argument of the constructor. |
| 1989 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1990 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1991 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1992 | User.EllipsisConversion = true; |
| 1993 | else { |
| 1994 | User.Before = Best->Conversions[0].Standard; |
| 1995 | User.EllipsisConversion = false; |
| 1996 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1997 | User.ConversionFunction = Constructor; |
| 1998 | User.After.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1999 | User.After.setFromType( |
| 2000 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2001 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2002 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2003 | } else if (CXXConversionDecl *Conversion |
| 2004 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 2005 | // C++ [over.ics.user]p1: |
| 2006 | // |
| 2007 | // [...] If the user-defined conversion is specified by a |
| 2008 | // conversion function (12.3.2), the initial standard |
| 2009 | // conversion sequence converts the source type to the |
| 2010 | // implicit object parameter of the conversion function. |
| 2011 | User.Before = Best->Conversions[0].Standard; |
| 2012 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2013 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
| 2015 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2016 | // The second standard conversion sequence converts the |
| 2017 | // result of the user-defined conversion to the target type |
| 2018 | // for the sequence. Since an implicit conversion sequence |
| 2019 | // is an initialization, the special rules for |
| 2020 | // initialization by user-defined conversion apply when |
| 2021 | // selecting the best user-defined conversion for a |
| 2022 | // user-defined conversion sequence (see 13.3.3 and |
| 2023 | // 13.3.3.1). |
| 2024 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2025 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2026 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2027 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2028 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2031 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2032 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2033 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2034 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2035 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2036 | |
| 2037 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2038 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2041 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2042 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2043 | |
| 2044 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2045 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2046 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2047 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2048 | OverloadingResult OvResult = |
| 2049 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2050 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2051 | if (OvResult == OR_Ambiguous) |
| 2052 | Diag(From->getSourceRange().getBegin(), |
| 2053 | diag::err_typecheck_ambiguous_condition) |
| 2054 | << From->getType() << ToType << From->getSourceRange(); |
| 2055 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2056 | Diag(From->getSourceRange().getBegin(), |
| 2057 | diag::err_typecheck_nonviable_condition) |
| 2058 | << From->getType() << ToType << From->getSourceRange(); |
| 2059 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2060 | return false; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 2061 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2062 | return true; |
| 2063 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2064 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2065 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2066 | /// conversion sequences to determine whether one is better than the |
| 2067 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2069 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 2070 | const ImplicitConversionSequence& ICS2) |
| 2071 | { |
| 2072 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2073 | // conversion sequences (as defined in 13.3.3.1) |
| 2074 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2075 | // conversion sequence than a user-defined conversion sequence or |
| 2076 | // an ellipsis conversion sequence, and |
| 2077 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2078 | // conversion sequence than an ellipsis conversion sequence |
| 2079 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2081 | // C++0x [over.best.ics]p10: |
| 2082 | // For the purpose of ranking implicit conversion sequences as |
| 2083 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2084 | // treated as a user-defined sequence that is indistinguishable |
| 2085 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2086 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2087 | return ImplicitConversionSequence::Better; |
| 2088 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2089 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2090 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2091 | // The following checks require both conversion sequences to be of |
| 2092 | // the same kind. |
| 2093 | if (ICS1.getKind() != ICS2.getKind()) |
| 2094 | return ImplicitConversionSequence::Indistinguishable; |
| 2095 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2096 | // Two implicit conversion sequences of the same form are |
| 2097 | // indistinguishable conversion sequences unless one of the |
| 2098 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2099 | if (ICS1.isStandard()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2100 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2101 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2102 | // User-defined conversion sequence U1 is a better conversion |
| 2103 | // sequence than another user-defined conversion sequence U2 if |
| 2104 | // they contain the same user-defined conversion function or |
| 2105 | // constructor and if the second standard conversion sequence of |
| 2106 | // U1 is better than the second standard conversion sequence of |
| 2107 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2108 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2109 | ICS2.UserDefined.ConversionFunction) |
| 2110 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 2111 | ICS2.UserDefined.After); |
| 2112 | } |
| 2113 | |
| 2114 | return ImplicitConversionSequence::Indistinguishable; |
| 2115 | } |
| 2116 | |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2117 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2118 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2119 | Qualifiers Quals; |
| 2120 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
| 2121 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
| 2122 | } |
| 2123 | |
| 2124 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2125 | } |
| 2126 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2127 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2128 | // determine if one is a proper subset of the other. |
| 2129 | static ImplicitConversionSequence::CompareKind |
| 2130 | compareStandardConversionSubsets(ASTContext &Context, |
| 2131 | const StandardConversionSequence& SCS1, |
| 2132 | const StandardConversionSequence& SCS2) { |
| 2133 | ImplicitConversionSequence::CompareKind Result |
| 2134 | = ImplicitConversionSequence::Indistinguishable; |
| 2135 | |
Douglas Gregor | ae65f4b | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2136 | // the identity conversion sequence is considered to be a subsequence of |
| 2137 | // any non-identity conversion sequence |
| 2138 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2139 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2140 | return ImplicitConversionSequence::Better; |
| 2141 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2142 | return ImplicitConversionSequence::Worse; |
| 2143 | } |
| 2144 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2145 | if (SCS1.Second != SCS2.Second) { |
| 2146 | if (SCS1.Second == ICK_Identity) |
| 2147 | Result = ImplicitConversionSequence::Better; |
| 2148 | else if (SCS2.Second == ICK_Identity) |
| 2149 | Result = ImplicitConversionSequence::Worse; |
| 2150 | else |
| 2151 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2152 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2153 | return ImplicitConversionSequence::Indistinguishable; |
| 2154 | |
| 2155 | if (SCS1.Third == SCS2.Third) { |
| 2156 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2157 | : ImplicitConversionSequence::Indistinguishable; |
| 2158 | } |
| 2159 | |
| 2160 | if (SCS1.Third == ICK_Identity) |
| 2161 | return Result == ImplicitConversionSequence::Worse |
| 2162 | ? ImplicitConversionSequence::Indistinguishable |
| 2163 | : ImplicitConversionSequence::Better; |
| 2164 | |
| 2165 | if (SCS2.Third == ICK_Identity) |
| 2166 | return Result == ImplicitConversionSequence::Better |
| 2167 | ? ImplicitConversionSequence::Indistinguishable |
| 2168 | : ImplicitConversionSequence::Worse; |
| 2169 | |
| 2170 | return ImplicitConversionSequence::Indistinguishable; |
| 2171 | } |
| 2172 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2173 | /// CompareStandardConversionSequences - Compare two standard |
| 2174 | /// conversion sequences to determine whether one is better than the |
| 2175 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2177 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 2178 | const StandardConversionSequence& SCS2) |
| 2179 | { |
| 2180 | // Standard conversion sequence S1 is a better conversion sequence |
| 2181 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2182 | |
| 2183 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2184 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2185 | // excluding any Lvalue Transformation; the identity conversion |
| 2186 | // sequence is considered to be a subsequence of any |
| 2187 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2188 | if (ImplicitConversionSequence::CompareKind CK |
| 2189 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 2190 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2191 | |
| 2192 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2193 | // defined below), or, if not that, |
| 2194 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2195 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2196 | if (Rank1 < Rank2) |
| 2197 | return ImplicitConversionSequence::Better; |
| 2198 | else if (Rank2 < Rank1) |
| 2199 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2201 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2202 | // are indistinguishable unless one of the following rules |
| 2203 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2205 | // A conversion that is not a conversion of a pointer, or |
| 2206 | // pointer to member, to bool is better than another conversion |
| 2207 | // that is such a conversion. |
| 2208 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2209 | return SCS2.isPointerConversionToBool() |
| 2210 | ? ImplicitConversionSequence::Better |
| 2211 | : ImplicitConversionSequence::Worse; |
| 2212 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2213 | // C++ [over.ics.rank]p4b2: |
| 2214 | // |
| 2215 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2216 | // conversion of B* to A* is better than conversion of B* to |
| 2217 | // void*, and conversion of A* to void* is better than conversion |
| 2218 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2220 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2222 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2223 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2224 | // Exactly one of the conversion sequences is a conversion to |
| 2225 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2226 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2227 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2228 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2229 | // Neither conversion sequence converts to a void pointer; compare |
| 2230 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2231 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 2232 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 2233 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2234 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2235 | // Both conversion sequences are conversions to void |
| 2236 | // pointers. Compare the source types to determine if there's an |
| 2237 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2238 | QualType FromType1 = SCS1.getFromType(); |
| 2239 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2240 | |
| 2241 | // Adjust the types we're converting from via the array-to-pointer |
| 2242 | // conversion, if we need to. |
| 2243 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2244 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2245 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2246 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2247 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2248 | QualType FromPointee1 |
| 2249 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2250 | QualType FromPointee2 |
| 2251 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2252 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2253 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2254 | return ImplicitConversionSequence::Better; |
| 2255 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2256 | return ImplicitConversionSequence::Worse; |
| 2257 | |
| 2258 | // Objective-C++: If one interface is more specific than the |
| 2259 | // other, it is the better one. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2260 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2261 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2262 | if (FromIface1 && FromIface1) { |
| 2263 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2264 | return ImplicitConversionSequence::Better; |
| 2265 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2266 | return ImplicitConversionSequence::Worse; |
| 2267 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2268 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2269 | |
| 2270 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2271 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2273 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2274 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2275 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2276 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2277 | // C++0x [over.ics.rank]p3b4: |
| 2278 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2279 | // implicit object parameter of a non-static member function declared |
| 2280 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 2281 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2282 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 2283 | // or if the member function in this case has a ref qualifier. |
| 2284 | // (Of course, we don't have ref qualifiers yet.) |
| 2285 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 2286 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 2287 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2288 | |
| 2289 | // C++ [over.ics.rank]p3b4: |
| 2290 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2291 | // which the references refer are the same type except for |
| 2292 | // top-level cv-qualifiers, and the type to which the reference |
| 2293 | // initialized by S2 refers is more cv-qualified than the type |
| 2294 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2295 | QualType T1 = SCS1.getToType(2); |
| 2296 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2297 | T1 = Context.getCanonicalType(T1); |
| 2298 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2299 | Qualifiers T1Quals, T2Quals; |
| 2300 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2301 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2302 | if (UnqualT1 == UnqualT2) { |
| 2303 | // If the type is an array type, promote the element qualifiers to the type |
| 2304 | // for comparison. |
| 2305 | if (isa<ArrayType>(T1) && T1Quals) |
| 2306 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2307 | if (isa<ArrayType>(T2) && T2Quals) |
| 2308 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2309 | if (T2.isMoreQualifiedThan(T1)) |
| 2310 | return ImplicitConversionSequence::Better; |
| 2311 | else if (T1.isMoreQualifiedThan(T2)) |
| 2312 | return ImplicitConversionSequence::Worse; |
| 2313 | } |
| 2314 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2315 | |
| 2316 | return ImplicitConversionSequence::Indistinguishable; |
| 2317 | } |
| 2318 | |
| 2319 | /// CompareQualificationConversions - Compares two standard conversion |
| 2320 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2322 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2323 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2325 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2326 | // -- S1 and S2 differ only in their qualification conversion and |
| 2327 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2328 | // cv-qualification signature of type T1 is a proper subset of |
| 2329 | // the cv-qualification signature of type T2, and S1 is not the |
| 2330 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2331 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2332 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2333 | return ImplicitConversionSequence::Indistinguishable; |
| 2334 | |
| 2335 | // FIXME: the example in the standard doesn't use a qualification |
| 2336 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2337 | QualType T1 = SCS1.getToType(2); |
| 2338 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2339 | T1 = Context.getCanonicalType(T1); |
| 2340 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2341 | Qualifiers T1Quals, T2Quals; |
| 2342 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2343 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2344 | |
| 2345 | // If the types are the same, we won't learn anything by unwrapped |
| 2346 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2347 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2348 | return ImplicitConversionSequence::Indistinguishable; |
| 2349 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2350 | // If the type is an array type, promote the element qualifiers to the type |
| 2351 | // for comparison. |
| 2352 | if (isa<ArrayType>(T1) && T1Quals) |
| 2353 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2354 | if (isa<ArrayType>(T2) && T2Quals) |
| 2355 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2356 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2358 | = ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5a57efd | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2359 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2360 | // Within each iteration of the loop, we check the qualifiers to |
| 2361 | // determine if this still looks like a qualification |
| 2362 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2363 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2364 | // until there are no more pointers or pointers-to-members left |
| 2365 | // to unwrap. This essentially mimics what |
| 2366 | // IsQualificationConversion does, but here we're checking for a |
| 2367 | // strict subset of qualifiers. |
| 2368 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2369 | // The qualifiers are the same, so this doesn't tell us anything |
| 2370 | // about how the sequences rank. |
| 2371 | ; |
| 2372 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2373 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2374 | if (Result == ImplicitConversionSequence::Worse) |
| 2375 | // Neither has qualifiers that are a subset of the other's |
| 2376 | // qualifiers. |
| 2377 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2378 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2379 | Result = ImplicitConversionSequence::Better; |
| 2380 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2381 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2382 | if (Result == ImplicitConversionSequence::Better) |
| 2383 | // Neither has qualifiers that are a subset of the other's |
| 2384 | // qualifiers. |
| 2385 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2387 | Result = ImplicitConversionSequence::Worse; |
| 2388 | } else { |
| 2389 | // Qualifiers are disjoint. |
| 2390 | return ImplicitConversionSequence::Indistinguishable; |
| 2391 | } |
| 2392 | |
| 2393 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2394 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2395 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2398 | // Check that the winning standard conversion sequence isn't using |
| 2399 | // the deprecated string literal array to pointer conversion. |
| 2400 | switch (Result) { |
| 2401 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2402 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2403 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2404 | break; |
| 2405 | |
| 2406 | case ImplicitConversionSequence::Indistinguishable: |
| 2407 | break; |
| 2408 | |
| 2409 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2410 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2411 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2412 | break; |
| 2413 | } |
| 2414 | |
| 2415 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2418 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2419 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2420 | /// various kinds of derived-to-base conversions (C++ |
| 2421 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2422 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2423 | ImplicitConversionSequence::CompareKind |
| 2424 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2425 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2426 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2427 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2428 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2429 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2430 | |
| 2431 | // Adjust the types we're converting from via the array-to-pointer |
| 2432 | // conversion, if we need to. |
| 2433 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2434 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2435 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2436 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2437 | |
| 2438 | // Canonicalize all of the types. |
| 2439 | FromType1 = Context.getCanonicalType(FromType1); |
| 2440 | ToType1 = Context.getCanonicalType(ToType1); |
| 2441 | FromType2 = Context.getCanonicalType(FromType2); |
| 2442 | ToType2 = Context.getCanonicalType(ToType2); |
| 2443 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2444 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2445 | // |
| 2446 | // If class B is derived directly or indirectly from class A and |
| 2447 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2448 | // |
| 2449 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2450 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2451 | |
| 2452 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2454 | SCS2.Second == ICK_Pointer_Conversion && |
| 2455 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2456 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2457 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2459 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2460 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2461 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2462 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2463 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2464 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2465 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2466 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2467 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2468 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
| 2469 | const ObjCObjectType* ToIface1 = ToPointee1->getAs<ObjCObjectType>(); |
| 2470 | const ObjCObjectType* ToIface2 = ToPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2471 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2472 | // -- 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] | 2473 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2474 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2475 | return ImplicitConversionSequence::Better; |
| 2476 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2477 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2478 | |
| 2479 | if (ToIface1 && ToIface2) { |
| 2480 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2481 | return ImplicitConversionSequence::Better; |
| 2482 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2483 | return ImplicitConversionSequence::Worse; |
| 2484 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2485 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2486 | |
| 2487 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2488 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2489 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2490 | return ImplicitConversionSequence::Better; |
| 2491 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2492 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2494 | if (FromIface1 && FromIface2) { |
| 2495 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2496 | return ImplicitConversionSequence::Better; |
| 2497 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2498 | return ImplicitConversionSequence::Worse; |
| 2499 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2500 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2503 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2504 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2505 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2506 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2507 | const MemberPointerType * FromMemPointer1 = |
| 2508 | FromType1->getAs<MemberPointerType>(); |
| 2509 | const MemberPointerType * ToMemPointer1 = |
| 2510 | ToType1->getAs<MemberPointerType>(); |
| 2511 | const MemberPointerType * FromMemPointer2 = |
| 2512 | FromType2->getAs<MemberPointerType>(); |
| 2513 | const MemberPointerType * ToMemPointer2 = |
| 2514 | ToType2->getAs<MemberPointerType>(); |
| 2515 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2516 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2517 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2518 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2519 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2520 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2521 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2522 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2523 | // 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] | 2524 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2525 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2526 | return ImplicitConversionSequence::Worse; |
| 2527 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2528 | return ImplicitConversionSequence::Better; |
| 2529 | } |
| 2530 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2531 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2532 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2533 | return ImplicitConversionSequence::Better; |
| 2534 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2535 | return ImplicitConversionSequence::Worse; |
| 2536 | } |
| 2537 | } |
| 2538 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2539 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2540 | // -- 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] | 2541 | // -- binding of an expression of type C to a reference of type |
| 2542 | // B& is better than binding an expression of type C to a |
| 2543 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2544 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2545 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2546 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2547 | return ImplicitConversionSequence::Better; |
| 2548 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2549 | return ImplicitConversionSequence::Worse; |
| 2550 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2552 | // -- 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] | 2553 | // -- binding of an expression of type B to a reference of type |
| 2554 | // A& is better than binding an expression of type C to a |
| 2555 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2556 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2557 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2558 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2559 | return ImplicitConversionSequence::Better; |
| 2560 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2561 | return ImplicitConversionSequence::Worse; |
| 2562 | } |
| 2563 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2564 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2565 | return ImplicitConversionSequence::Indistinguishable; |
| 2566 | } |
| 2567 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2568 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2569 | /// determine whether they are reference-related, |
| 2570 | /// reference-compatible, reference-compatible with added |
| 2571 | /// qualification, or incompatible, for use in C++ initialization by |
| 2572 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2573 | /// type, and the first type (T1) is the pointee type of the reference |
| 2574 | /// type being initialized. |
| 2575 | Sema::ReferenceCompareResult |
| 2576 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2577 | QualType OrigT1, QualType OrigT2, |
| 2578 | bool& DerivedToBase) { |
| 2579 | assert(!OrigT1->isReferenceType() && |
| 2580 | "T1 must be the pointee type of the reference type"); |
| 2581 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2582 | |
| 2583 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2584 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2585 | Qualifiers T1Quals, T2Quals; |
| 2586 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2587 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2588 | |
| 2589 | // C++ [dcl.init.ref]p4: |
| 2590 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2591 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2592 | // T1 is a base class of T2. |
| 2593 | if (UnqualT1 == UnqualT2) |
| 2594 | DerivedToBase = false; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2595 | else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2596 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2597 | DerivedToBase = true; |
| 2598 | else |
| 2599 | return Ref_Incompatible; |
| 2600 | |
| 2601 | // At this point, we know that T1 and T2 are reference-related (at |
| 2602 | // least). |
| 2603 | |
| 2604 | // If the type is an array type, promote the element qualifiers to the type |
| 2605 | // for comparison. |
| 2606 | if (isa<ArrayType>(T1) && T1Quals) |
| 2607 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2608 | if (isa<ArrayType>(T2) && T2Quals) |
| 2609 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2610 | |
| 2611 | // C++ [dcl.init.ref]p4: |
| 2612 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2613 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2614 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2615 | // overload resolution, cases for which cv1 is greater |
| 2616 | // cv-qualification than cv2 are identified as |
| 2617 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2618 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2619 | return Ref_Compatible; |
| 2620 | else if (T1.isMoreQualifiedThan(T2)) |
| 2621 | return Ref_Compatible_With_Added_Qualification; |
| 2622 | else |
| 2623 | return Ref_Related; |
| 2624 | } |
| 2625 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2626 | /// \brief Look for a user-defined conversion to an lvalue reference-compatible |
| 2627 | /// with DeclType. Return true if something definite is found. |
| 2628 | static bool |
| 2629 | FindConversionToLValue(Sema &S, ImplicitConversionSequence &ICS, |
| 2630 | QualType DeclType, SourceLocation DeclLoc, |
| 2631 | Expr *Init, QualType T2, bool AllowExplicit) { |
| 2632 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 2633 | CXXRecordDecl *T2RecordDecl |
| 2634 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2635 | |
| 2636 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2637 | const UnresolvedSetImpl *Conversions |
| 2638 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2639 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2640 | E = Conversions->end(); I != E; ++I) { |
| 2641 | NamedDecl *D = *I; |
| 2642 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2643 | if (isa<UsingShadowDecl>(D)) |
| 2644 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2645 | |
| 2646 | FunctionTemplateDecl *ConvTemplate |
| 2647 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2648 | CXXConversionDecl *Conv; |
| 2649 | if (ConvTemplate) |
| 2650 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2651 | else |
| 2652 | Conv = cast<CXXConversionDecl>(D); |
| 2653 | |
| 2654 | // If the conversion function doesn't return a reference type, |
| 2655 | // it can't be considered for this conversion. An rvalue reference |
| 2656 | // is only acceptable if its referencee is a function type. |
| 2657 | const ReferenceType *RefType = |
| 2658 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 2659 | if (RefType && (RefType->isLValueReferenceType() || |
| 2660 | RefType->getPointeeType()->isFunctionType()) && |
| 2661 | (AllowExplicit || !Conv->isExplicit())) { |
| 2662 | if (ConvTemplate) |
| 2663 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2664 | Init, DeclType, CandidateSet); |
| 2665 | else |
| 2666 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2667 | DeclType, CandidateSet); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | OverloadCandidateSet::iterator Best; |
| 2672 | switch (S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2673 | case OR_Success: |
| 2674 | // C++ [over.ics.ref]p1: |
| 2675 | // |
| 2676 | // [...] If the parameter binds directly to the result of |
| 2677 | // applying a conversion function to the argument |
| 2678 | // expression, the implicit conversion sequence is a |
| 2679 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2680 | // second standard conversion sequence either an identity |
| 2681 | // conversion or, if the conversion function returns an |
| 2682 | // entity of a type that is a derived class of the parameter |
| 2683 | // type, a derived-to-base Conversion. |
| 2684 | if (!Best->FinalConversion.DirectBinding) |
| 2685 | return false; |
| 2686 | |
| 2687 | ICS.setUserDefined(); |
| 2688 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2689 | ICS.UserDefined.After = Best->FinalConversion; |
| 2690 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2691 | ICS.UserDefined.EllipsisConversion = false; |
| 2692 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2693 | ICS.UserDefined.After.DirectBinding && |
| 2694 | "Expected a direct reference binding!"); |
| 2695 | return true; |
| 2696 | |
| 2697 | case OR_Ambiguous: |
| 2698 | ICS.setAmbiguous(); |
| 2699 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2700 | Cand != CandidateSet.end(); ++Cand) |
| 2701 | if (Cand->Viable) |
| 2702 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2703 | return true; |
| 2704 | |
| 2705 | case OR_No_Viable_Function: |
| 2706 | case OR_Deleted: |
| 2707 | // There was no suitable conversion, or we found a deleted |
| 2708 | // conversion; continue with other checks. |
| 2709 | return false; |
| 2710 | } |
Eric Christopher | 1c3d502 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 2711 | |
| 2712 | return false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2715 | /// \brief Compute an implicit conversion sequence for reference |
| 2716 | /// initialization. |
| 2717 | static ImplicitConversionSequence |
| 2718 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2719 | SourceLocation DeclLoc, |
| 2720 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2721 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2722 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2723 | |
| 2724 | // Most paths end in a failed conversion. |
| 2725 | ImplicitConversionSequence ICS; |
| 2726 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2727 | |
| 2728 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2729 | QualType T2 = Init->getType(); |
| 2730 | |
| 2731 | // If the initializer is the address of an overloaded function, try |
| 2732 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2733 | // type of the resulting function. |
| 2734 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2735 | DeclAccessPair Found; |
| 2736 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2737 | false, Found)) |
| 2738 | T2 = Fn->getType(); |
| 2739 | } |
| 2740 | |
| 2741 | // Compute some basic properties of the types and the initializer. |
| 2742 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2743 | bool DerivedToBase = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2744 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2745 | Sema::ReferenceCompareResult RefRelationship |
| 2746 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
| 2747 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2748 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2749 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2750 | // A reference to type "cv1 T1" is initialized by an expression |
| 2751 | // of type "cv2 T2" as follows: |
| 2752 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2753 | // -- If reference is an lvalue reference and the initializer expression |
| 2754 | // The next bullet point (T1 is a function) is pretty much equivalent to this |
| 2755 | // one, so it's handled here. |
| 2756 | if (!isRValRef || T1->isFunctionType()) { |
| 2757 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2758 | // reference-compatible with "cv2 T2," or |
| 2759 | // |
| 2760 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2761 | if (InitCategory.isLValue() && |
| 2762 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2763 | // C++ [over.ics.ref]p1: |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2764 | // When a parameter of reference type binds directly (8.5.3) |
| 2765 | // to an argument expression, the implicit conversion sequence |
| 2766 | // is the identity conversion, unless the argument expression |
| 2767 | // has a type that is a derived class of the parameter type, |
| 2768 | // in which case the implicit conversion sequence is a |
| 2769 | // derived-to-base Conversion (13.3.3.1). |
| 2770 | ICS.setStandard(); |
| 2771 | ICS.Standard.First = ICK_Identity; |
| 2772 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2773 | ICS.Standard.Third = ICK_Identity; |
| 2774 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2775 | ICS.Standard.setToType(0, T2); |
| 2776 | ICS.Standard.setToType(1, T1); |
| 2777 | ICS.Standard.setToType(2, T1); |
| 2778 | ICS.Standard.ReferenceBinding = true; |
| 2779 | ICS.Standard.DirectBinding = true; |
| 2780 | ICS.Standard.RRefBinding = isRValRef; |
| 2781 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2782 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2783 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2784 | // derived-to-base conversions is suppressed when we're |
| 2785 | // computing the implicit conversion sequence (C++ |
| 2786 | // [over.best.ics]p2). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2787 | return ICS; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2788 | } |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2789 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2790 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2791 | // not reference-related to T2, and can be implicitly |
| 2792 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2793 | // is reference-compatible with "cv3 T3" 92) (this |
| 2794 | // conversion is selected by enumerating the applicable |
| 2795 | // conversion functions (13.3.1.6) and choosing the best |
| 2796 | // one through overload resolution (13.3)), |
| 2797 | if (!SuppressUserConversions && T2->isRecordType() && |
| 2798 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2799 | RefRelationship == Sema::Ref_Incompatible) { |
| 2800 | if (FindConversionToLValue(S, ICS, DeclType, DeclLoc, |
| 2801 | Init, T2, AllowExplicit)) |
| 2802 | return ICS; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2806 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 2807 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2808 | // shall be an rvalue reference and the initializer expression shall be |
| 2809 | // an rvalue or have a function type. |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2810 | // |
| 2811 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2812 | // point, which is that, due to p2 (which short-circuits reference |
| 2813 | // binding by only attempting a simple conversion for non-direct |
| 2814 | // bindings) and p3's strange wording, we allow a const volatile |
| 2815 | // reference to bind to an rvalue. Hence the check for the presence |
| 2816 | // of "const" rather than checking for "const" being the only |
| 2817 | // qualifier. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2818 | // This is also the point where rvalue references and lvalue inits no longer |
| 2819 | // go together. |
| 2820 | if ((!isRValRef && !T1.isConstQualified()) || |
| 2821 | (isRValRef && InitCategory.isLValue())) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2822 | return ICS; |
| 2823 | |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2824 | // -- If T1 is a function type, then |
| 2825 | // -- if T2 is the same type as T1, the reference is bound to the |
| 2826 | // initializer expression lvalue; |
| 2827 | // -- if T2 is a class type and the initializer expression can be |
| 2828 | // implicitly converted to an lvalue of type T1 [...], the |
| 2829 | // reference is bound to the function lvalue that is the result |
| 2830 | // of the conversion; |
| 2831 | // This is the same as for the lvalue case above, so it was handled there. |
| 2832 | // -- otherwise, the program is ill-formed. |
| 2833 | // This is the one difference to the lvalue case. |
| 2834 | if (T1->isFunctionType()) |
| 2835 | return ICS; |
| 2836 | |
| 2837 | // -- Otherwise, if T2 is a class type and |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2838 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2839 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2840 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2841 | // -- T1 is not reference-related to T2 and the initializer |
| 2842 | // expression can be implicitly converted to an rvalue |
| 2843 | // of type "cv3 T3" (this conversion is selected by |
| 2844 | // enumerating the applicable conversion functions |
| 2845 | // (13.3.1.6) and choosing the best one through overload |
| 2846 | // resolution (13.3)), |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2847 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2848 | // then the reference is bound to the initializer |
| 2849 | // expression rvalue in the first case and to the object |
| 2850 | // that is the result of the conversion in the second case |
| 2851 | // (or, in either case, to the appropriate base class |
| 2852 | // subobject of the object). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2853 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2854 | // We're only checking the first case here, which is a direct |
| 2855 | // binding in C++0x but not in C++03. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2856 | if (InitCategory.isRValue() && T2->isRecordType() && |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2857 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2858 | ICS.setStandard(); |
| 2859 | ICS.Standard.First = ICK_Identity; |
| 2860 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2861 | ICS.Standard.Third = ICK_Identity; |
| 2862 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2863 | ICS.Standard.setToType(0, T2); |
| 2864 | ICS.Standard.setToType(1, T1); |
| 2865 | ICS.Standard.setToType(2, T1); |
| 2866 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2867 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2868 | ICS.Standard.RRefBinding = isRValRef; |
| 2869 | ICS.Standard.CopyConstructor = 0; |
| 2870 | return ICS; |
| 2871 | } |
| 2872 | |
| 2873 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2874 | // initialized from the initializer expression using the |
| 2875 | // rules for a non-reference copy initialization (8.5). The |
| 2876 | // reference is then bound to the temporary. If T1 is |
| 2877 | // reference-related to T2, cv1 must be the same |
| 2878 | // cv-qualification as, or greater cv-qualification than, |
| 2879 | // cv2; otherwise, the program is ill-formed. |
| 2880 | if (RefRelationship == Sema::Ref_Related) { |
| 2881 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2882 | // we would be reference-compatible or reference-compatible with |
| 2883 | // added qualification. But that wasn't the case, so the reference |
| 2884 | // initialization fails. |
| 2885 | return ICS; |
| 2886 | } |
| 2887 | |
| 2888 | // If at least one of the types is a class type, the types are not |
| 2889 | // related, and we aren't allowed any user conversions, the |
| 2890 | // reference binding fails. This case is important for breaking |
| 2891 | // recursion, since TryImplicitConversion below will attempt to |
| 2892 | // create a temporary through the use of a copy constructor. |
| 2893 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 2894 | (T1->isRecordType() || T2->isRecordType())) |
| 2895 | return ICS; |
| 2896 | |
| 2897 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2898 | // When a parameter of reference type is not bound directly to |
| 2899 | // an argument expression, the conversion sequence is the one |
| 2900 | // required to convert the argument expression to the |
| 2901 | // underlying type of the reference according to |
| 2902 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 2903 | // to copy-initializing a temporary of the underlying type with |
| 2904 | // the argument expression. Any difference in top-level |
| 2905 | // cv-qualification is subsumed by the initialization itself |
| 2906 | // and does not constitute a conversion. |
| 2907 | ICS = S.TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 2908 | /*AllowExplicit=*/false, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2909 | /*InOverloadResolution=*/false); |
| 2910 | |
| 2911 | // Of course, that's still a reference binding. |
| 2912 | if (ICS.isStandard()) { |
| 2913 | ICS.Standard.ReferenceBinding = true; |
| 2914 | ICS.Standard.RRefBinding = isRValRef; |
| 2915 | } else if (ICS.isUserDefined()) { |
| 2916 | ICS.UserDefined.After.ReferenceBinding = true; |
| 2917 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 2918 | } |
| 2919 | return ICS; |
| 2920 | } |
| 2921 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2922 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2923 | /// ToType from the expression From. Return the implicit conversion |
| 2924 | /// sequence required to pass this argument, which may be a bad |
| 2925 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2926 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 2927 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2928 | static ImplicitConversionSequence |
| 2929 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | b7f9e6a | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 2930 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2931 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2932 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2933 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2934 | /*FIXME:*/From->getLocStart(), |
| 2935 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2936 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2937 | |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2938 | return S.TryImplicitConversion(From, ToType, |
| 2939 | SuppressUserConversions, |
| 2940 | /*AllowExplicit=*/false, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2941 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2944 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2945 | /// parameter of the given member function (@c Method) from the |
| 2946 | /// expression @p From. |
| 2947 | ImplicitConversionSequence |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2948 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2949 | CXXMethodDecl *Method, |
| 2950 | CXXRecordDecl *ActingContext) { |
| 2951 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2952 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2953 | // const volatile object. |
| 2954 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2955 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2956 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2957 | |
| 2958 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2959 | // to exit early. |
| 2960 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2961 | |
| 2962 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2963 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2964 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2965 | FromType = PT->getPointeeType(); |
| 2966 | |
| 2967 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2968 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2969 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2970 | // where X is the class of which the function is a member |
| 2971 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2972 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2974 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2975 | // reference binding here, that allows class rvalues to bind to |
| 2976 | // non-constant references. |
| 2977 | |
| 2978 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2979 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2980 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2981 | if (ImplicitParamType.getCVRQualifiers() |
| 2982 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2983 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2984 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2985 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2986 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2987 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2988 | |
| 2989 | // Check that we have either the same type or a derived type. It |
| 2990 | // affects the conversion rank. |
| 2991 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2992 | ImplicitConversionKind SecondKind; |
| 2993 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2994 | SecondKind = ICK_Identity; |
| 2995 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2996 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2997 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2998 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2999 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3000 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3001 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3002 | |
| 3003 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3004 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3005 | ICS.Standard.setAsIdentityConversion(); |
| 3006 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3007 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3008 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3009 | ICS.Standard.ReferenceBinding = true; |
| 3010 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 3011 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3012 | return ICS; |
| 3013 | } |
| 3014 | |
| 3015 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3016 | /// the implicit object parameter for the given Method with the given |
| 3017 | /// expression. |
| 3018 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3019 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 3020 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3021 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3022 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3023 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3024 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3025 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3027 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3028 | FromRecordType = PT->getPointeeType(); |
| 3029 | DestType = Method->getThisType(Context); |
| 3030 | } else { |
| 3031 | FromRecordType = From->getType(); |
| 3032 | DestType = ImplicitParamRecordType; |
| 3033 | } |
| 3034 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3035 | // Note that we always use the true parent context when performing |
| 3036 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | ImplicitConversionSequence ICS |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3038 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 3039 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3040 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3041 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3042 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3043 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3045 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3046 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3048 | if (!Context.hasSameType(From->getType(), DestType)) |
Anders Carlsson | f1b48b7 | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3049 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3050 | From->getType()->isPointerType() ? |
| 3051 | ImplicitCastExpr::RValue : ImplicitCastExpr::LValue); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3052 | return false; |
| 3053 | } |
| 3054 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3055 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3056 | /// expression From to bool (C++0x [conv]p3). |
| 3057 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3058 | // FIXME: This is pretty broken. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3060 | // FIXME: Are these flags correct? |
| 3061 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3062 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3063 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3067 | /// of the expression From to bool (C++0x [conv]p3). |
| 3068 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 3069 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3070 | if (!ICS.isBad()) |
| 3071 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3072 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3073 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3074 | return Diag(From->getSourceRange().getBegin(), |
| 3075 | diag::err_typecheck_bool_condition) |
| 3076 | << From->getType() << From->getSourceRange(); |
| 3077 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3078 | } |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3079 | |
| 3080 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3081 | /// expression From to 'id'. |
| 3082 | ImplicitConversionSequence Sema::TryContextuallyConvertToObjCId(Expr *From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3083 | QualType Ty = Context.getObjCIdType(); |
| 3084 | return TryImplicitConversion(From, Ty, |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3085 | // FIXME: Are these flags correct? |
| 3086 | /*SuppressUserConversions=*/false, |
| 3087 | /*AllowExplicit=*/true, |
| 3088 | /*InOverloadResolution=*/false); |
| 3089 | } |
| 3090 | |
| 3091 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3092 | /// of the expression From to 'id'. |
| 3093 | bool Sema::PerformContextuallyConvertToObjCId(Expr *&From) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3094 | QualType Ty = Context.getObjCIdType(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3095 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(From); |
| 3096 | if (!ICS.isBad()) |
| 3097 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| 3098 | return true; |
| 3099 | } |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3100 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3101 | /// \brief Attempt to convert the given expression to an integral or |
| 3102 | /// enumeration type. |
| 3103 | /// |
| 3104 | /// This routine will attempt to convert an expression of class type to an |
| 3105 | /// integral or enumeration type, if that class type only has a single |
| 3106 | /// conversion to an integral or enumeration type. |
| 3107 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3108 | /// \param Loc The source location of the construct that requires the |
| 3109 | /// conversion. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3110 | /// |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3111 | /// \param FromE The expression we're converting from. |
| 3112 | /// |
| 3113 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3114 | /// have integral or enumeration type. |
| 3115 | /// |
| 3116 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3117 | /// incomplete class type. |
| 3118 | /// |
| 3119 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3120 | /// explicit conversion function (because no implicit conversion functions |
| 3121 | /// were available). This is a recovery mode. |
| 3122 | /// |
| 3123 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3124 | /// showing which conversion was picked. |
| 3125 | /// |
| 3126 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3127 | /// conversion function that could convert to integral or enumeration type. |
| 3128 | /// |
| 3129 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
| 3130 | /// usable conversion function. |
| 3131 | /// |
| 3132 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3133 | /// function, which may be an extension in this case. |
| 3134 | /// |
| 3135 | /// \returns The expression, converted to an integral or enumeration type if |
| 3136 | /// successful. |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3137 | Sema::OwningExprResult |
| 3138 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, ExprArg FromE, |
| 3139 | const PartialDiagnostic &NotIntDiag, |
| 3140 | const PartialDiagnostic &IncompleteDiag, |
| 3141 | const PartialDiagnostic &ExplicitConvDiag, |
| 3142 | const PartialDiagnostic &ExplicitConvNote, |
| 3143 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3144 | const PartialDiagnostic &AmbigNote, |
| 3145 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3146 | Expr *From = static_cast<Expr *>(FromE.get()); |
| 3147 | |
| 3148 | // We can't perform any more checking for type-dependent expressions. |
| 3149 | if (From->isTypeDependent()) |
| 3150 | return move(FromE); |
| 3151 | |
| 3152 | // If the expression already has integral or enumeration type, we're golden. |
| 3153 | QualType T = From->getType(); |
| 3154 | if (T->isIntegralOrEnumerationType()) |
| 3155 | return move(FromE); |
| 3156 | |
| 3157 | // FIXME: Check for missing '()' if T is a function type? |
| 3158 | |
| 3159 | // If we don't have a class type in C++, there's no way we can get an |
| 3160 | // expression of integral or enumeration type. |
| 3161 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3162 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3163 | Diag(Loc, NotIntDiag) |
| 3164 | << T << From->getSourceRange(); |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3165 | return move(FromE); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | // We must have a complete class type. |
| 3169 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3170 | return move(FromE); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3171 | |
| 3172 | // Look for a conversion to an integral or enumeration type. |
| 3173 | UnresolvedSet<4> ViableConversions; |
| 3174 | UnresolvedSet<4> ExplicitConversions; |
| 3175 | const UnresolvedSetImpl *Conversions |
| 3176 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
| 3177 | |
| 3178 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3179 | E = Conversions->end(); |
| 3180 | I != E; |
| 3181 | ++I) { |
| 3182 | if (CXXConversionDecl *Conversion |
| 3183 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3184 | if (Conversion->getConversionType().getNonReferenceType() |
| 3185 | ->isIntegralOrEnumerationType()) { |
| 3186 | if (Conversion->isExplicit()) |
| 3187 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3188 | else |
| 3189 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | switch (ViableConversions.size()) { |
| 3194 | case 0: |
| 3195 | if (ExplicitConversions.size() == 1) { |
| 3196 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3197 | CXXConversionDecl *Conversion |
| 3198 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3199 | |
| 3200 | // The user probably meant to invoke the given explicit |
| 3201 | // conversion; use it. |
| 3202 | QualType ConvTy |
| 3203 | = Conversion->getConversionType().getNonReferenceType(); |
| 3204 | std::string TypeStr; |
| 3205 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
| 3206 | |
| 3207 | Diag(Loc, ExplicitConvDiag) |
| 3208 | << T << ConvTy |
| 3209 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3210 | "static_cast<" + TypeStr + ">(") |
| 3211 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3212 | ")"); |
| 3213 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3214 | << ConvTy->isEnumeralType() << ConvTy; |
| 3215 | |
| 3216 | // If we aren't in a SFINAE context, build a call to the |
| 3217 | // explicit conversion function. |
| 3218 | if (isSFINAEContext()) |
| 3219 | return ExprError(); |
| 3220 | |
| 3221 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
| 3222 | From = BuildCXXMemberCallExpr(FromE.takeAs<Expr>(), Found, Conversion); |
| 3223 | FromE = Owned(From); |
| 3224 | } |
| 3225 | |
| 3226 | // We'll complain below about a non-integral condition type. |
| 3227 | break; |
| 3228 | |
| 3229 | case 1: { |
| 3230 | // Apply this conversion. |
| 3231 | DeclAccessPair Found = ViableConversions[0]; |
| 3232 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3233 | |
| 3234 | CXXConversionDecl *Conversion |
| 3235 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3236 | QualType ConvTy |
| 3237 | = Conversion->getConversionType().getNonReferenceType(); |
| 3238 | if (ConvDiag.getDiagID()) { |
| 3239 | if (isSFINAEContext()) |
| 3240 | return ExprError(); |
| 3241 | |
| 3242 | Diag(Loc, ConvDiag) |
| 3243 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3244 | } |
| 3245 | |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3246 | From = BuildCXXMemberCallExpr(FromE.takeAs<Expr>(), Found, |
| 3247 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
| 3248 | FromE = Owned(From); |
| 3249 | break; |
| 3250 | } |
| 3251 | |
| 3252 | default: |
| 3253 | Diag(Loc, AmbigDiag) |
| 3254 | << T << From->getSourceRange(); |
| 3255 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3256 | CXXConversionDecl *Conv |
| 3257 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3258 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3259 | Diag(Conv->getLocation(), AmbigNote) |
| 3260 | << ConvTy->isEnumeralType() << ConvTy; |
| 3261 | } |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3262 | return move(FromE); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Douglas Gregor | acb0bd8 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3265 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3266 | Diag(Loc, NotIntDiag) |
| 3267 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3268 | |
| 3269 | return move(FromE); |
| 3270 | } |
| 3271 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3272 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3273 | /// candidate functions, using the given function call arguments. If |
| 3274 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3275 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3276 | /// |
| 3277 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3278 | /// based on an incomplete set of function arguments. This feature is used by |
| 3279 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | void |
| 3281 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3282 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3283 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3284 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3285 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3286 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3288 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3289 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3291 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3293 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3294 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3295 | // If we get here, it's because we're calling a member function |
| 3296 | // that is named without a member access expression (e.g., |
| 3297 | // "this->f") that was either written explicitly or created |
| 3298 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3299 | // function, e.g., X::f(). We use an empty type for the implied |
| 3300 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3301 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3302 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3303 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3304 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3305 | return; |
| 3306 | } |
| 3307 | // We treat a constructor like a non-member function, since its object |
| 3308 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3311 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3312 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3313 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3314 | // Overload resolution is always an unevaluated context. |
| 3315 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3316 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3317 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3318 | // C++ [class.copy]p3: |
| 3319 | // A member function template is never instantiated to perform the copy |
| 3320 | // of a class object to an object of its class type. |
| 3321 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 3322 | if (NumArgs == 1 && |
| 3323 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3324 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3325 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3326 | return; |
| 3327 | } |
| 3328 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3329 | // Add this candidate |
| 3330 | CandidateSet.push_back(OverloadCandidate()); |
| 3331 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3332 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3333 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3334 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3335 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3336 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3337 | |
| 3338 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3339 | |
| 3340 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3341 | // parameters is viable only if it has an ellipsis in its parameter |
| 3342 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3343 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 3344 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3345 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3346 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3347 | return; |
| 3348 | } |
| 3349 | |
| 3350 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3351 | // is viable only if the (m+1)st parameter has a default argument |
| 3352 | // (8.3.6). For the purposes of overload resolution, the |
| 3353 | // parameter list is truncated on the right, so that there are |
| 3354 | // exactly m parameters. |
| 3355 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3356 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3357 | // Not enough arguments. |
| 3358 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3359 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3360 | return; |
| 3361 | } |
| 3362 | |
| 3363 | // Determine the implicit conversion sequences for each of the |
| 3364 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3365 | Candidate.Conversions.resize(NumArgs); |
| 3366 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3367 | if (ArgIdx < NumArgsInProto) { |
| 3368 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3369 | // exist for each argument an implicit conversion sequence |
| 3370 | // (13.3.3.1) that converts that argument to the corresponding |
| 3371 | // parameter of F. |
| 3372 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3374 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3375 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3376 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3377 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3378 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3379 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3380 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3381 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3382 | } else { |
| 3383 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3384 | // argument for which there is no corresponding parameter is |
| 3385 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3386 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | } |
| 3390 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3391 | /// \brief Add all of the function declarations in the given function set to |
| 3392 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3393 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3394 | Expr **Args, unsigned NumArgs, |
| 3395 | OverloadCandidateSet& CandidateSet, |
| 3396 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3397 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3398 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3399 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3400 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3401 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3402 | cast<CXXMethodDecl>(FD)->getParent(), |
| 3403 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3404 | CandidateSet, SuppressUserConversions); |
| 3405 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3406 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3407 | SuppressUserConversions); |
| 3408 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3409 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3410 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3411 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3412 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3413 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3414 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3415 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3416 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3417 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3418 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3419 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3420 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3421 | Args, NumArgs, CandidateSet, |
| 3422 | SuppressUserConversions); |
| 3423 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3424 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3427 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3428 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3429 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3430 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3431 | Expr **Args, unsigned NumArgs, |
| 3432 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3433 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3434 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3435 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3436 | |
| 3437 | if (isa<UsingShadowDecl>(Decl)) |
| 3438 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 3439 | |
| 3440 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3441 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3442 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3443 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3444 | /*ExplicitArgs*/ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3445 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3446 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3447 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3448 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3449 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3450 | ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3451 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3455 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3456 | /// of candidate functions, using the given function call arguments |
| 3457 | /// and the object argument (@c Object). For example, in a call |
| 3458 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 3459 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 3460 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3461 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3463 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3464 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 3465 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3466 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3467 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3469 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3470 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3471 | assert(!isa<CXXConstructorDecl>(Method) && |
| 3472 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3473 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3474 | if (!CandidateSet.isNewCandidate(Method)) |
| 3475 | return; |
| 3476 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3477 | // Overload resolution is always an unevaluated context. |
| 3478 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3479 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3480 | // Add this candidate |
| 3481 | CandidateSet.push_back(OverloadCandidate()); |
| 3482 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3483 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3484 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3485 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3486 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3487 | |
| 3488 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3489 | |
| 3490 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3491 | // parameters is viable only if it has an ellipsis in its parameter |
| 3492 | // list (8.3.5). |
| 3493 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3494 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3495 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3496 | return; |
| 3497 | } |
| 3498 | |
| 3499 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3500 | // is viable only if the (m+1)st parameter has a default argument |
| 3501 | // (8.3.6). For the purposes of overload resolution, the |
| 3502 | // parameter list is truncated on the right, so that there are |
| 3503 | // exactly m parameters. |
| 3504 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 3505 | if (NumArgs < MinRequiredArgs) { |
| 3506 | // Not enough arguments. |
| 3507 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3508 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3509 | return; |
| 3510 | } |
| 3511 | |
| 3512 | Candidate.Viable = true; |
| 3513 | Candidate.Conversions.resize(NumArgs + 1); |
| 3514 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3515 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3516 | // The implicit object argument is ignored. |
| 3517 | Candidate.IgnoreObjectArgument = true; |
| 3518 | else { |
| 3519 | // Determine the implicit conversion sequence for the object |
| 3520 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3521 | Candidate.Conversions[0] |
| 3522 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3523 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3524 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3525 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3526 | return; |
| 3527 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | // Determine the implicit conversion sequences for each of the |
| 3531 | // arguments. |
| 3532 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3533 | if (ArgIdx < NumArgsInProto) { |
| 3534 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3535 | // exist for each argument an implicit conversion sequence |
| 3536 | // (13.3.3.1) that converts that argument to the corresponding |
| 3537 | // parameter of F. |
| 3538 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3540 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3541 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3542 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3543 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3544 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3545 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3546 | break; |
| 3547 | } |
| 3548 | } else { |
| 3549 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3550 | // argument for which there is no corresponding parameter is |
| 3551 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3552 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 3556 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3557 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 3558 | /// set, using template argument deduction to produce an appropriate member |
| 3559 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3561 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3562 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3563 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3564 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3565 | QualType ObjectType, |
| 3566 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3567 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3568 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3569 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3570 | return; |
| 3571 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3572 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3574 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | // 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] | 3576 | // candidate functions in the usual way.113) A given name can refer to one |
| 3577 | // or more function templates and also to a set of overloaded non-template |
| 3578 | // functions. In such a case, the candidate functions generated from each |
| 3579 | // function template are combined with the set of non-template candidate |
| 3580 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3581 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3582 | FunctionDecl *Specialization = 0; |
| 3583 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3584 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3585 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3586 | CandidateSet.push_back(OverloadCandidate()); |
| 3587 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3588 | Candidate.FoundDecl = FoundDecl; |
| 3589 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 3590 | Candidate.Viable = false; |
| 3591 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3592 | Candidate.IsSurrogate = false; |
| 3593 | Candidate.IgnoreObjectArgument = false; |
| 3594 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3595 | Info); |
| 3596 | return; |
| 3597 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3599 | // Add the function template specialization produced by template argument |
| 3600 | // deduction as a candidate. |
| 3601 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3603 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3604 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3605 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3606 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3609 | /// \brief Add a C++ function template specialization as a candidate |
| 3610 | /// in the candidate set, using template argument deduction to produce |
| 3611 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3612 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3613 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3614 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3615 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3616 | Expr **Args, unsigned NumArgs, |
| 3617 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3618 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3619 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3620 | return; |
| 3621 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3622 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3624 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3625 | // 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] | 3626 | // candidate functions in the usual way.113) A given name can refer to one |
| 3627 | // or more function templates and also to a set of overloaded non-template |
| 3628 | // functions. In such a case, the candidate functions generated from each |
| 3629 | // function template are combined with the set of non-template candidate |
| 3630 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3631 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3632 | FunctionDecl *Specialization = 0; |
| 3633 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3634 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3635 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3636 | CandidateSet.push_back(OverloadCandidate()); |
| 3637 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3638 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3639 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3640 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3641 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3642 | Candidate.IsSurrogate = false; |
| 3643 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3644 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3645 | Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3646 | return; |
| 3647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3649 | // Add the function template specialization produced by template argument |
| 3650 | // deduction as a candidate. |
| 3651 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3652 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3653 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3656 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3658 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | /// 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] | 3660 | /// (which may or may not be the same type as the type that the |
| 3661 | /// conversion function produces). |
| 3662 | void |
| 3663 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3664 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3665 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3666 | Expr *From, QualType ToType, |
| 3667 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3668 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3669 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3670 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3671 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3672 | return; |
| 3673 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3674 | // Overload resolution is always an unevaluated context. |
| 3675 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3676 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3677 | // Add this candidate |
| 3678 | CandidateSet.push_back(OverloadCandidate()); |
| 3679 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3680 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3681 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3682 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3683 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3684 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3685 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3686 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3688 | // Determine the implicit conversion sequence for the implicit |
| 3689 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3690 | Candidate.Viable = true; |
| 3691 | Candidate.Conversions.resize(1); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3692 | Candidate.Conversions[0] |
| 3693 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 3694 | ActingContext); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3695 | // Conversion functions to a different type in the base class is visible in |
| 3696 | // the derived class. So, a derived to base conversion should not participate |
| 3697 | // in overload resolution. |
| 3698 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 3699 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3700 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3701 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3702 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3703 | return; |
| 3704 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3705 | |
| 3706 | // We won't go through a user-define type conversion function to convert a |
| 3707 | // derived to base as such conversions are given Conversion Rank. They only |
| 3708 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3709 | QualType FromCanon |
| 3710 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3711 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3712 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3713 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3714 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3715 | return; |
| 3716 | } |
| 3717 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3718 | // To determine what the conversion from the result of calling the |
| 3719 | // conversion function to the type we're eventually trying to |
| 3720 | // convert to (ToType), we need to synthesize a call to the |
| 3721 | // conversion function and attempt copy initialization from it. This |
| 3722 | // makes sure that we get the right semantics with respect to |
| 3723 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3724 | // call on the stack and we don't need its arguments to be |
| 3725 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3727 | From->getLocStart()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame^] | 3728 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 3729 | Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3730 | CastExpr::CK_FunctionToPointerDecay, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame^] | 3731 | &ConversionRef, ImplicitCastExpr::RValue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | |
| 3733 | // 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] | 3734 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3735 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3737 | Conversion->getConversionType().getNonLValueExprType(Context), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3738 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3739 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3740 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3741 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3742 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3744 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3745 | case ImplicitConversionSequence::StandardConversion: |
| 3746 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3747 | |
| 3748 | // C++ [over.ics.user]p3: |
| 3749 | // If the user-defined conversion is specified by a specialization of a |
| 3750 | // conversion function template, the second standard conversion sequence |
| 3751 | // shall have exact match rank. |
| 3752 | if (Conversion->getPrimaryTemplate() && |
| 3753 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3754 | Candidate.Viable = false; |
| 3755 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3756 | } |
| 3757 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3758 | break; |
| 3759 | |
| 3760 | case ImplicitConversionSequence::BadConversion: |
| 3761 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3762 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3763 | break; |
| 3764 | |
| 3765 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3767 | "Can only end up with a standard conversion sequence or failure"); |
| 3768 | } |
| 3769 | } |
| 3770 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3771 | /// \brief Adds a conversion function template specialization |
| 3772 | /// candidate to the overload set, using template argument deduction |
| 3773 | /// to deduce the template arguments of the conversion function |
| 3774 | /// template from the type that we are converting to (C++ |
| 3775 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3777 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3778 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3779 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3780 | Expr *From, QualType ToType, |
| 3781 | OverloadCandidateSet &CandidateSet) { |
| 3782 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3783 | "Only conversion function templates permitted here"); |
| 3784 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3785 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3786 | return; |
| 3787 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3788 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3789 | CXXConversionDecl *Specialization = 0; |
| 3790 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3792 | Specialization, Info)) { |
Douglas Gregor | ff5adac | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3793 | CandidateSet.push_back(OverloadCandidate()); |
| 3794 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3795 | Candidate.FoundDecl = FoundDecl; |
| 3796 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3797 | Candidate.Viable = false; |
| 3798 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3799 | Candidate.IsSurrogate = false; |
| 3800 | Candidate.IgnoreObjectArgument = false; |
| 3801 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3802 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3803 | return; |
| 3804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3806 | // Add the conversion function template specialization produced by |
| 3807 | // template argument deduction as a candidate. |
| 3808 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3809 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3810 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3813 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3814 | /// converts the given @c Object to a function pointer via the |
| 3815 | /// conversion function @c Conversion, and then attempts to call it |
| 3816 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3817 | /// the type of function that we'll eventually be calling. |
| 3818 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3819 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3820 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3821 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3822 | QualType ObjectType, |
| 3823 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3824 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3825 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3826 | return; |
| 3827 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3828 | // Overload resolution is always an unevaluated context. |
| 3829 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3830 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3831 | CandidateSet.push_back(OverloadCandidate()); |
| 3832 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3833 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3834 | Candidate.Function = 0; |
| 3835 | Candidate.Surrogate = Conversion; |
| 3836 | Candidate.Viable = true; |
| 3837 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3838 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3839 | Candidate.Conversions.resize(NumArgs + 1); |
| 3840 | |
| 3841 | // Determine the implicit conversion sequence for the implicit |
| 3842 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | ImplicitConversionSequence ObjectInit |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3844 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3845 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3846 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3847 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3848 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3849 | return; |
| 3850 | } |
| 3851 | |
| 3852 | // The first conversion is actually a user-defined conversion whose |
| 3853 | // first conversion is ObjectInit's standard conversion (which is |
| 3854 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3855 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3856 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3857 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3858 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3860 | = Candidate.Conversions[0].UserDefined.Before; |
| 3861 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3862 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3864 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3865 | |
| 3866 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3867 | // parameters is viable only if it has an ellipsis in its parameter |
| 3868 | // list (8.3.5). |
| 3869 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3870 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3871 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3872 | return; |
| 3873 | } |
| 3874 | |
| 3875 | // Function types don't have any default arguments, so just check if |
| 3876 | // we have enough arguments. |
| 3877 | if (NumArgs < NumArgsInProto) { |
| 3878 | // Not enough arguments. |
| 3879 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3880 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3881 | return; |
| 3882 | } |
| 3883 | |
| 3884 | // Determine the implicit conversion sequences for each of the |
| 3885 | // arguments. |
| 3886 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3887 | if (ArgIdx < NumArgsInProto) { |
| 3888 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3889 | // exist for each argument an implicit conversion sequence |
| 3890 | // (13.3.3.1) that converts that argument to the corresponding |
| 3891 | // parameter of F. |
| 3892 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3894 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3895 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3896 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3897 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3898 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3899 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3900 | break; |
| 3901 | } |
| 3902 | } else { |
| 3903 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3904 | // argument for which there is no corresponding parameter is |
| 3905 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3906 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3911 | /// \brief Add overload candidates for overloaded operators that are |
| 3912 | /// member functions. |
| 3913 | /// |
| 3914 | /// Add the overloaded operator candidates that are member functions |
| 3915 | /// for the operator Op that was used in an operator expression such |
| 3916 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3917 | /// CandidateSet will store the added overload candidates. (C++ |
| 3918 | /// [over.match.oper]). |
| 3919 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3920 | SourceLocation OpLoc, |
| 3921 | Expr **Args, unsigned NumArgs, |
| 3922 | OverloadCandidateSet& CandidateSet, |
| 3923 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3924 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3925 | |
| 3926 | // C++ [over.match.oper]p3: |
| 3927 | // For a unary operator @ with an operand of a type whose |
| 3928 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3929 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3930 | // a right operand of a type whose cv-unqualified version is T2, |
| 3931 | // three sets of candidate functions, designated member |
| 3932 | // candidates, non-member candidates and built-in candidates, are |
| 3933 | // constructed as follows: |
| 3934 | QualType T1 = Args[0]->getType(); |
| 3935 | QualType T2; |
| 3936 | if (NumArgs > 1) |
| 3937 | T2 = Args[1]->getType(); |
| 3938 | |
| 3939 | // -- If T1 is a class type, the set of member candidates is the |
| 3940 | // result of the qualified lookup of T1::operator@ |
| 3941 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3942 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3943 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3944 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3945 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3946 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3948 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3949 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3950 | Operators.suppressDiagnostics(); |
| 3951 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3952 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3953 | OperEnd = Operators.end(); |
| 3954 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3955 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3956 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3957 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3958 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3959 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3962 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3963 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3964 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3965 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3966 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3967 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3968 | /// (at the beginning of the argument list) that will be contextually |
| 3969 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3971 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3972 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3973 | bool IsAssignmentOperator, |
| 3974 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3975 | // Overload resolution is always an unevaluated context. |
| 3976 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3977 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3978 | // Add this candidate |
| 3979 | CandidateSet.push_back(OverloadCandidate()); |
| 3980 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3981 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3982 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3983 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3984 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3985 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3986 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3987 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3988 | |
| 3989 | // Determine the implicit conversion sequences for each of the |
| 3990 | // arguments. |
| 3991 | Candidate.Viable = true; |
| 3992 | Candidate.Conversions.resize(NumArgs); |
| 3993 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3994 | // C++ [over.match.oper]p4: |
| 3995 | // For the built-in assignment operators, conversions of the |
| 3996 | // left operand are restricted as follows: |
| 3997 | // -- no temporaries are introduced to hold the left operand, and |
| 3998 | // -- no user-defined conversions are applied to the left |
| 3999 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4001 | // |
| 4002 | // We block these conversions by turning off user-defined |
| 4003 | // conversions, since that is the only way that initialization of |
| 4004 | // a reference to a non-class type can occur from something that |
| 4005 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4006 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4007 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4008 | "Contextual conversion to bool requires bool type"); |
| 4009 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 4010 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4012 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4013 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4014 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4015 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4016 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4017 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4018 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4019 | break; |
| 4020 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4025 | /// candidate operator functions for built-in operators (C++ |
| 4026 | /// [over.built]). The types are separated into pointer types and |
| 4027 | /// enumeration types. |
| 4028 | class BuiltinCandidateTypeSet { |
| 4029 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4030 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4031 | |
| 4032 | /// PointerTypes - The set of pointer types that will be used in the |
| 4033 | /// built-in candidates. |
| 4034 | TypeSet PointerTypes; |
| 4035 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4036 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4037 | /// used in the built-in candidates. |
| 4038 | TypeSet MemberPointerTypes; |
| 4039 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4040 | /// EnumerationTypes - The set of enumeration types that will be |
| 4041 | /// used in the built-in candidates. |
| 4042 | TypeSet EnumerationTypes; |
| 4043 | |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4044 | /// \brief The set of vector types that will be used in the built-in |
| 4045 | /// candidates. |
| 4046 | TypeSet VectorTypes; |
| 4047 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4048 | /// Sema - The semantic analysis instance where we are building the |
| 4049 | /// candidate type set. |
| 4050 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4052 | /// Context - The AST context in which we will build the type sets. |
| 4053 | ASTContext &Context; |
| 4054 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4055 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4056 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4057 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4058 | |
| 4059 | public: |
| 4060 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4061 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4062 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4064 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4065 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4066 | void AddTypesConvertedFrom(QualType Ty, |
| 4067 | SourceLocation Loc, |
| 4068 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4069 | bool AllowExplicitConversions, |
| 4070 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4071 | |
| 4072 | /// pointer_begin - First pointer type found; |
| 4073 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4074 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4075 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4076 | iterator pointer_end() { return PointerTypes.end(); } |
| 4077 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4078 | /// member_pointer_begin - First member pointer type found; |
| 4079 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4080 | |
| 4081 | /// member_pointer_end - Past the last member pointer type found; |
| 4082 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4083 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4084 | /// enumeration_begin - First enumeration type found; |
| 4085 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4086 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4087 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4088 | iterator enumeration_end() { return EnumerationTypes.end(); } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4089 | |
| 4090 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4091 | iterator vector_end() { return VectorTypes.end(); } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4092 | }; |
| 4093 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4094 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4095 | /// the set of pointer types along with any more-qualified variants of |
| 4096 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4097 | /// will add "int const *", "int const volatile *", "int const |
| 4098 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4099 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4100 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4101 | /// |
| 4102 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4103 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4104 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4105 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4106 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4107 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4108 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4109 | return false; |
| 4110 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4111 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 4112 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4113 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4114 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4115 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4116 | // (the qualifier would sink to the element type), and for another, the |
| 4117 | // only overload situation where it matters is subscript or pointer +- int, |
| 4118 | // and those shouldn't have qualifier variants anyway. |
| 4119 | if (PointeeTy->isArrayType()) |
| 4120 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4121 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4122 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4123 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4124 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4125 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 4126 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4127 | // Iterate through all strict supersets of BaseCVR. |
| 4128 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4129 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4130 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4131 | // in the types. |
| 4132 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4133 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4134 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 4135 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | return true; |
| 4139 | } |
| 4140 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4141 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4142 | /// to the set of pointer types along with any more-qualified variants of |
| 4143 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4144 | /// will add "int const *", "int const volatile *", "int const |
| 4145 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4146 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4147 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4148 | /// |
| 4149 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4150 | bool |
| 4151 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4152 | QualType Ty) { |
| 4153 | // Insert this type. |
| 4154 | if (!MemberPointerTypes.insert(Ty)) |
| 4155 | return false; |
| 4156 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4157 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4158 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4159 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4160 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4161 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4162 | // (the qualifier would sink to the element type), and for another, the |
| 4163 | // only overload situation where it matters is subscript or pointer +- int, |
| 4164 | // and those shouldn't have qualifier variants anyway. |
| 4165 | if (PointeeTy->isArrayType()) |
| 4166 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4167 | const Type *ClassTy = PointerTy->getClass(); |
| 4168 | |
| 4169 | // Iterate through all strict supersets of the pointee type's CVR |
| 4170 | // qualifiers. |
| 4171 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4172 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4173 | if ((CVR | BaseCVR) != CVR) continue; |
| 4174 | |
| 4175 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 4176 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | return true; |
| 4180 | } |
| 4181 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4182 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4183 | /// 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] | 4184 | /// primarily interested in pointer types and enumeration types. We also |
| 4185 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4186 | /// AllowUserConversions is true if we should look at the conversion |
| 4187 | /// functions of a class type, and AllowExplicitConversions if we |
| 4188 | /// should also include the explicit conversion functions of a class |
| 4189 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4190 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4191 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4192 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4193 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4194 | bool AllowExplicitConversions, |
| 4195 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4196 | // Only deal with canonical types. |
| 4197 | Ty = Context.getCanonicalType(Ty); |
| 4198 | |
| 4199 | // Look through reference types; they aren't part of the type of an |
| 4200 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4201 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4202 | Ty = RefTy->getPointeeType(); |
| 4203 | |
| 4204 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4205 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4206 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 4207 | // If we're dealing with an array type, decay to the pointer. |
| 4208 | if (Ty->isArrayType()) |
| 4209 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4210 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4211 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4212 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 4213 | |
| 4214 | // Insert our type, and its more-qualified variants, into the set |
| 4215 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4216 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4217 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4218 | } else if (Ty->isMemberPointerType()) { |
| 4219 | // Member pointers are far easier, since the pointee can't be converted. |
| 4220 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4221 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4222 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4223 | EnumerationTypes.insert(Ty); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4224 | } else if (Ty->isVectorType()) { |
| 4225 | VectorTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4226 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4227 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4228 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4229 | // No conversion functions in incomplete types. |
| 4230 | return; |
| 4231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4232 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4233 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4234 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 4235 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4236 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4237 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4238 | NamedDecl *D = I.getDecl(); |
| 4239 | if (isa<UsingShadowDecl>(D)) |
| 4240 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4243 | // about which builtin types we can convert to. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4244 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4245 | continue; |
| 4246 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4247 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4248 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4249 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4250 | VisibleQuals); |
| 4251 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | } |
| 4256 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4257 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4258 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4259 | /// given type to the candidate set. |
| 4260 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4261 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4263 | unsigned NumArgs, |
| 4264 | OverloadCandidateSet &CandidateSet) { |
| 4265 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4267 | // T& operator=(T&, T) |
| 4268 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4269 | ParamTypes[1] = T; |
| 4270 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4271 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4273 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4274 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4275 | ParamTypes[0] |
| 4276 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4277 | ParamTypes[1] = T; |
| 4278 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4279 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4280 | } |
| 4281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4283 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4284 | /// 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] | 4285 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4286 | Qualifiers VRQuals; |
| 4287 | const RecordType *TyRec; |
| 4288 | if (const MemberPointerType *RHSMPType = |
| 4289 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4290 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4291 | else |
| 4292 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4293 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4294 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4295 | VRQuals.addVolatile(); |
| 4296 | VRQuals.addRestrict(); |
| 4297 | return VRQuals; |
| 4298 | } |
| 4299 | |
| 4300 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4301 | if (!ClassDecl->hasDefinition()) |
| 4302 | return VRQuals; |
| 4303 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4304 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4305 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4306 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4307 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4308 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4309 | NamedDecl *D = I.getDecl(); |
| 4310 | if (isa<UsingShadowDecl>(D)) |
| 4311 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4312 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4313 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4314 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4315 | CanTy = ResTypeRef->getPointeeType(); |
| 4316 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4317 | // as see them. |
| 4318 | bool done = false; |
| 4319 | while (!done) { |
| 4320 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4321 | CanTy = ResTypePtr->getPointeeType(); |
| 4322 | else if (const MemberPointerType *ResTypeMPtr = |
| 4323 | CanTy->getAs<MemberPointerType>()) |
| 4324 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4325 | else |
| 4326 | done = true; |
| 4327 | if (CanTy.isVolatileQualified()) |
| 4328 | VRQuals.addVolatile(); |
| 4329 | if (CanTy.isRestrictQualified()) |
| 4330 | VRQuals.addRestrict(); |
| 4331 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4332 | return VRQuals; |
| 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | return VRQuals; |
| 4337 | } |
| 4338 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4339 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 4340 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 4341 | /// on the operator @p Op and the arguments given. For example, if the |
| 4342 | /// operator is a binary '+', this routine might add "int |
| 4343 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4344 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4345 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4346 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4347 | Expr **Args, unsigned NumArgs, |
| 4348 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4349 | // The set of "promoted arithmetic types", which are the arithmetic |
| 4350 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 4351 | // that the first few of these types are the promoted integral |
| 4352 | // types; these types need to be first. |
| 4353 | // FIXME: What about complex? |
| 4354 | const unsigned FirstIntegralType = 0; |
| 4355 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4357 | LastPromotedIntegralType = 13; |
| 4358 | const unsigned FirstPromotedArithmeticType = 7, |
| 4359 | LastPromotedArithmeticType = 16; |
| 4360 | const unsigned NumArithmeticTypes = 16; |
| 4361 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 4363 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4364 | Context.SignedCharTy, Context.ShortTy, |
| 4365 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 4366 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 4367 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 4368 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 4369 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 4370 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 4371 | "Invalid first promoted integral type"); |
| 4372 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 4373 | == Context.UnsignedLongLongTy && |
| 4374 | "Invalid last promoted integral type"); |
| 4375 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 4376 | "Invalid first promoted arithmetic type"); |
| 4377 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 4378 | == Context.LongDoubleTy && |
| 4379 | "Invalid last promoted arithmetic type"); |
| 4380 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4381 | // Find all of the types that the arguments can convert to, but only |
| 4382 | // if the operator we're looking at has built-in operator candidates |
| 4383 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4384 | Qualifiers VisibleTypeConversionsQuals; |
| 4385 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4386 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4387 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 4388 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4389 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4390 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4391 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 4392 | OpLoc, |
| 4393 | true, |
| 4394 | (Op == OO_Exclaim || |
| 4395 | Op == OO_AmpAmp || |
| 4396 | Op == OO_PipePipe), |
| 4397 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4398 | |
| 4399 | bool isComparison = false; |
| 4400 | switch (Op) { |
| 4401 | case OO_None: |
| 4402 | case NUM_OVERLOADED_OPERATORS: |
| 4403 | assert(false && "Expected an overloaded operator"); |
| 4404 | break; |
| 4405 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4406 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4408 | goto UnaryStar; |
| 4409 | else |
| 4410 | goto BinaryStar; |
| 4411 | break; |
| 4412 | |
| 4413 | case OO_Plus: // '+' is either unary or binary |
| 4414 | if (NumArgs == 1) |
| 4415 | goto UnaryPlus; |
| 4416 | else |
| 4417 | goto BinaryPlus; |
| 4418 | break; |
| 4419 | |
| 4420 | case OO_Minus: // '-' is either unary or binary |
| 4421 | if (NumArgs == 1) |
| 4422 | goto UnaryMinus; |
| 4423 | else |
| 4424 | goto BinaryMinus; |
| 4425 | break; |
| 4426 | |
| 4427 | case OO_Amp: // '&' is either unary or binary |
| 4428 | if (NumArgs == 1) |
| 4429 | goto UnaryAmp; |
| 4430 | else |
| 4431 | goto BinaryAmp; |
| 4432 | |
| 4433 | case OO_PlusPlus: |
| 4434 | case OO_MinusMinus: |
| 4435 | // C++ [over.built]p3: |
| 4436 | // |
| 4437 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 4438 | // is either volatile or empty, there exist candidate operator |
| 4439 | // functions of the form |
| 4440 | // |
| 4441 | // VQ T& operator++(VQ T&); |
| 4442 | // T operator++(VQ T&, int); |
| 4443 | // |
| 4444 | // C++ [over.built]p4: |
| 4445 | // |
| 4446 | // For every pair (T, VQ), where T is an arithmetic type other |
| 4447 | // than bool, and VQ is either volatile or empty, there exist |
| 4448 | // candidate operator functions of the form |
| 4449 | // |
| 4450 | // VQ T& operator--(VQ T&); |
| 4451 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4453 | Arith < NumArithmeticTypes; ++Arith) { |
| 4454 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4456 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4457 | |
| 4458 | // Non-volatile version. |
| 4459 | if (NumArgs == 1) |
| 4460 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4461 | else |
| 4462 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4463 | // heuristic to reduce number of builtin candidates in the set. |
| 4464 | // Add volatile version only if there are conversions to a volatile type. |
| 4465 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4466 | // Volatile version |
| 4467 | ParamTypes[0] |
| 4468 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 4469 | if (NumArgs == 1) |
| 4470 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4471 | else |
| 4472 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 4473 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | // C++ [over.built]p5: |
| 4477 | // |
| 4478 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4479 | // cv-unqualified object type, and VQ is either volatile or |
| 4480 | // empty, there exist candidate operator functions of the form |
| 4481 | // |
| 4482 | // T*VQ& operator++(T*VQ&); |
| 4483 | // T*VQ& operator--(T*VQ&); |
| 4484 | // T* operator++(T*VQ&, int); |
| 4485 | // T* operator--(T*VQ&, int); |
| 4486 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4487 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4488 | // Skip pointer types that aren't pointers to object types. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4489 | if (!(*Ptr)->getPointeeType()->isIncompleteOrObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4490 | continue; |
| 4491 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | QualType ParamTypes[2] = { |
| 4493 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4494 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4495 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4496 | // Without volatile |
| 4497 | if (NumArgs == 1) |
| 4498 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4499 | else |
| 4500 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4501 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4502 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4503 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4504 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4505 | ParamTypes[0] |
| 4506 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4507 | if (NumArgs == 1) |
| 4508 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4509 | else |
| 4510 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4511 | } |
| 4512 | } |
| 4513 | break; |
| 4514 | |
| 4515 | UnaryStar: |
| 4516 | // C++ [over.built]p6: |
| 4517 | // For every cv-qualified or cv-unqualified object type T, there |
| 4518 | // exist candidate operator functions of the form |
| 4519 | // |
| 4520 | // T& operator*(T*); |
| 4521 | // |
| 4522 | // C++ [over.built]p7: |
| 4523 | // For every function type T, there exist candidate operator |
| 4524 | // functions of the form |
| 4525 | // T& operator*(T*); |
| 4526 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4527 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4528 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4529 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4531 | &ParamTy, Args, 1, CandidateSet); |
| 4532 | } |
| 4533 | break; |
| 4534 | |
| 4535 | UnaryPlus: |
| 4536 | // C++ [over.built]p8: |
| 4537 | // For every type T, there exist candidate operator functions of |
| 4538 | // the form |
| 4539 | // |
| 4540 | // T* operator+(T*); |
| 4541 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4542 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4543 | QualType ParamTy = *Ptr; |
| 4544 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 4545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4546 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4547 | // Fall through |
| 4548 | |
| 4549 | UnaryMinus: |
| 4550 | // C++ [over.built]p9: |
| 4551 | // For every promoted arithmetic type T, there exist candidate |
| 4552 | // operator functions of the form |
| 4553 | // |
| 4554 | // T operator+(T); |
| 4555 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4557 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 4558 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 4559 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 4560 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4561 | |
| 4562 | // Extension: We also add these operators for vector types. |
| 4563 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4564 | VecEnd = CandidateTypes.vector_end(); |
| 4565 | Vec != VecEnd; ++Vec) { |
| 4566 | QualType VecTy = *Vec; |
| 4567 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4568 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4569 | break; |
| 4570 | |
| 4571 | case OO_Tilde: |
| 4572 | // C++ [over.built]p10: |
| 4573 | // For every promoted integral type T, there exist candidate |
| 4574 | // operator functions of the form |
| 4575 | // |
| 4576 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4577 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4578 | Int < LastPromotedIntegralType; ++Int) { |
| 4579 | QualType IntTy = ArithmeticTypes[Int]; |
| 4580 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 4581 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4582 | |
| 4583 | // Extension: We also add this operator for vector types. |
| 4584 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4585 | VecEnd = CandidateTypes.vector_end(); |
| 4586 | Vec != VecEnd; ++Vec) { |
| 4587 | QualType VecTy = *Vec; |
| 4588 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4589 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4590 | break; |
| 4591 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4592 | case OO_New: |
| 4593 | case OO_Delete: |
| 4594 | case OO_Array_New: |
| 4595 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4596 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4597 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4598 | break; |
| 4599 | |
| 4600 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4601 | UnaryAmp: |
| 4602 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4603 | // C++ [over.match.oper]p3: |
| 4604 | // -- For the operator ',', the unary operator '&', or the |
| 4605 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4606 | break; |
| 4607 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4608 | case OO_EqualEqual: |
| 4609 | case OO_ExclaimEqual: |
| 4610 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | // For every pointer to member type T, there exist candidate operator |
| 4612 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4613 | // |
| 4614 | // bool operator==(T,T); |
| 4615 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4616 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4617 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4618 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4619 | MemPtr != MemPtrEnd; |
| 4620 | ++MemPtr) { |
| 4621 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4622 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4623 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4625 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4627 | case OO_Less: |
| 4628 | case OO_Greater: |
| 4629 | case OO_LessEqual: |
| 4630 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4631 | // C++ [over.built]p15: |
| 4632 | // |
| 4633 | // For every pointer or enumeration type T, there exist |
| 4634 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4636 | // bool operator<(T, T); |
| 4637 | // bool operator>(T, T); |
| 4638 | // bool operator<=(T, T); |
| 4639 | // bool operator>=(T, T); |
| 4640 | // bool operator==(T, T); |
| 4641 | // bool operator!=(T, T); |
| 4642 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4643 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4644 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4645 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4646 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4648 | = CandidateTypes.enumeration_begin(); |
| 4649 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4650 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4651 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4652 | } |
| 4653 | |
| 4654 | // Fall through. |
| 4655 | isComparison = true; |
| 4656 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4657 | BinaryPlus: |
| 4658 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4659 | if (!isComparison) { |
| 4660 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4661 | |
| 4662 | // C++ [over.built]p13: |
| 4663 | // |
| 4664 | // For every cv-qualified or cv-unqualified object type T |
| 4665 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4667 | // T* operator+(T*, ptrdiff_t); |
| 4668 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4669 | // T* operator-(T*, ptrdiff_t); |
| 4670 | // T* operator+(ptrdiff_t, T*); |
| 4671 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4672 | // |
| 4673 | // C++ [over.built]p14: |
| 4674 | // |
| 4675 | // For every T, where T is a pointer to object type, there |
| 4676 | // exist candidate operator functions of the form |
| 4677 | // |
| 4678 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4680 | = CandidateTypes.pointer_begin(); |
| 4681 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4682 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4683 | |
| 4684 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4685 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4686 | |
| 4687 | if (Op == OO_Plus) { |
| 4688 | // T* operator+(ptrdiff_t, T*); |
| 4689 | ParamTypes[0] = ParamTypes[1]; |
| 4690 | ParamTypes[1] = *Ptr; |
| 4691 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4692 | } else { |
| 4693 | // ptrdiff_t operator-(T, T); |
| 4694 | ParamTypes[1] = *Ptr; |
| 4695 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4696 | Args, 2, CandidateSet); |
| 4697 | } |
| 4698 | } |
| 4699 | } |
| 4700 | // Fall through |
| 4701 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4702 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4703 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4704 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4705 | // C++ [over.built]p12: |
| 4706 | // |
| 4707 | // For every pair of promoted arithmetic types L and R, there |
| 4708 | // exist candidate operator functions of the form |
| 4709 | // |
| 4710 | // LR operator*(L, R); |
| 4711 | // LR operator/(L, R); |
| 4712 | // LR operator+(L, R); |
| 4713 | // LR operator-(L, R); |
| 4714 | // bool operator<(L, R); |
| 4715 | // bool operator>(L, R); |
| 4716 | // bool operator<=(L, R); |
| 4717 | // bool operator>=(L, R); |
| 4718 | // bool operator==(L, R); |
| 4719 | // bool operator!=(L, R); |
| 4720 | // |
| 4721 | // where LR is the result of the usual arithmetic conversions |
| 4722 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4723 | // |
| 4724 | // C++ [over.built]p24: |
| 4725 | // |
| 4726 | // For every pair of promoted arithmetic types L and R, there exist |
| 4727 | // candidate operator functions of the form |
| 4728 | // |
| 4729 | // LR operator?(bool, L, R); |
| 4730 | // |
| 4731 | // where LR is the result of the usual arithmetic conversions |
| 4732 | // between types L and R. |
| 4733 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4734 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4735 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4737 | Right < LastPromotedArithmeticType; ++Right) { |
| 4738 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4739 | QualType Result |
| 4740 | = isComparison |
| 4741 | ? Context.BoolTy |
| 4742 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4743 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4744 | } |
| 4745 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4746 | |
| 4747 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 4748 | // conditional operator for vector types. |
| 4749 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4750 | Vec1End = CandidateTypes.vector_end(); |
| 4751 | Vec1 != Vec1End; ++Vec1) |
| 4752 | for (BuiltinCandidateTypeSet::iterator |
| 4753 | Vec2 = CandidateTypes.vector_begin(), |
| 4754 | Vec2End = CandidateTypes.vector_end(); |
| 4755 | Vec2 != Vec2End; ++Vec2) { |
| 4756 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 4757 | QualType Result; |
| 4758 | if (isComparison) |
| 4759 | Result = Context.BoolTy; |
| 4760 | else { |
| 4761 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 4762 | Result = *Vec1; |
| 4763 | else |
| 4764 | Result = *Vec2; |
| 4765 | } |
| 4766 | |
| 4767 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4768 | } |
| 4769 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4770 | break; |
| 4771 | |
| 4772 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4773 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4774 | case OO_Caret: |
| 4775 | case OO_Pipe: |
| 4776 | case OO_LessLess: |
| 4777 | case OO_GreaterGreater: |
| 4778 | // C++ [over.built]p17: |
| 4779 | // |
| 4780 | // For every pair of promoted integral types L and R, there |
| 4781 | // exist candidate operator functions of the form |
| 4782 | // |
| 4783 | // LR operator%(L, R); |
| 4784 | // LR operator&(L, R); |
| 4785 | // LR operator^(L, R); |
| 4786 | // LR operator|(L, R); |
| 4787 | // L operator<<(L, R); |
| 4788 | // L operator>>(L, R); |
| 4789 | // |
| 4790 | // where LR is the result of the usual arithmetic conversions |
| 4791 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4792 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4793 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4795 | Right < LastPromotedIntegralType; ++Right) { |
| 4796 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4797 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4798 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4799 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4800 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4801 | } |
| 4802 | } |
| 4803 | break; |
| 4804 | |
| 4805 | case OO_Equal: |
| 4806 | // C++ [over.built]p20: |
| 4807 | // |
| 4808 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4809 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4810 | // empty, there exist candidate operator functions of the form |
| 4811 | // |
| 4812 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4813 | for (BuiltinCandidateTypeSet::iterator |
| 4814 | Enum = CandidateTypes.enumeration_begin(), |
| 4815 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4816 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4817 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4818 | CandidateSet); |
| 4819 | for (BuiltinCandidateTypeSet::iterator |
| 4820 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4821 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4822 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4823 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4824 | CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4825 | |
| 4826 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4827 | |
| 4828 | case OO_PlusEqual: |
| 4829 | case OO_MinusEqual: |
| 4830 | // C++ [over.built]p19: |
| 4831 | // |
| 4832 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4833 | // volatile or empty, there exist candidate operator functions |
| 4834 | // of the form |
| 4835 | // |
| 4836 | // T*VQ& operator=(T*VQ&, T*); |
| 4837 | // |
| 4838 | // C++ [over.built]p21: |
| 4839 | // |
| 4840 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4841 | // cv-unqualified object type and VQ is either volatile or |
| 4842 | // empty, there exist candidate operator functions of the form |
| 4843 | // |
| 4844 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4845 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4846 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4847 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4848 | QualType ParamTypes[2]; |
| 4849 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4850 | |
| 4851 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4852 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4853 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4854 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4855 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4856 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4857 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4858 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4859 | ParamTypes[0] |
| 4860 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4861 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4862 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4863 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4864 | } |
| 4865 | // Fall through. |
| 4866 | |
| 4867 | case OO_StarEqual: |
| 4868 | case OO_SlashEqual: |
| 4869 | // C++ [over.built]p18: |
| 4870 | // |
| 4871 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 4872 | // VQ is either volatile or empty, and R is a promoted |
| 4873 | // arithmetic type, there exist candidate operator functions of |
| 4874 | // the form |
| 4875 | // |
| 4876 | // VQ L& operator=(VQ L&, R); |
| 4877 | // VQ L& operator*=(VQ L&, R); |
| 4878 | // VQ L& operator/=(VQ L&, R); |
| 4879 | // VQ L& operator+=(VQ L&, R); |
| 4880 | // VQ L& operator-=(VQ L&, R); |
| 4881 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4882 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4883 | Right < LastPromotedArithmeticType; ++Right) { |
| 4884 | QualType ParamTypes[2]; |
| 4885 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4886 | |
| 4887 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4888 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4889 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4890 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4891 | |
| 4892 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4893 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4894 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 4895 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4896 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4897 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4898 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4899 | } |
| 4900 | } |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4901 | |
| 4902 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 4903 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4904 | Vec1End = CandidateTypes.vector_end(); |
| 4905 | Vec1 != Vec1End; ++Vec1) |
| 4906 | for (BuiltinCandidateTypeSet::iterator |
| 4907 | Vec2 = CandidateTypes.vector_begin(), |
| 4908 | Vec2End = CandidateTypes.vector_end(); |
| 4909 | Vec2 != Vec2End; ++Vec2) { |
| 4910 | QualType ParamTypes[2]; |
| 4911 | ParamTypes[1] = *Vec2; |
| 4912 | // Add this built-in operator as a candidate (VQ is empty). |
| 4913 | ParamTypes[0] = Context.getLValueReferenceType(*Vec1); |
| 4914 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4915 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4916 | |
| 4917 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4918 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4919 | ParamTypes[0] = Context.getVolatileType(*Vec1); |
| 4920 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4921 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4922 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4923 | } |
| 4924 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4925 | break; |
| 4926 | |
| 4927 | case OO_PercentEqual: |
| 4928 | case OO_LessLessEqual: |
| 4929 | case OO_GreaterGreaterEqual: |
| 4930 | case OO_AmpEqual: |
| 4931 | case OO_CaretEqual: |
| 4932 | case OO_PipeEqual: |
| 4933 | // C++ [over.built]p22: |
| 4934 | // |
| 4935 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 4936 | // is either volatile or empty, and R is a promoted integral |
| 4937 | // type, there exist candidate operator functions of the form |
| 4938 | // |
| 4939 | // VQ L& operator%=(VQ L&, R); |
| 4940 | // VQ L& operator<<=(VQ L&, R); |
| 4941 | // VQ L& operator>>=(VQ L&, R); |
| 4942 | // VQ L& operator&=(VQ L&, R); |
| 4943 | // VQ L& operator^=(VQ L&, R); |
| 4944 | // VQ L& operator|=(VQ L&, R); |
| 4945 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4947 | Right < LastPromotedIntegralType; ++Right) { |
| 4948 | QualType ParamTypes[2]; |
| 4949 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4950 | |
| 4951 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4952 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4953 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4954 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4955 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4956 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4957 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4958 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4959 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4960 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4961 | } |
| 4962 | } |
| 4963 | break; |
| 4964 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4965 | case OO_Exclaim: { |
| 4966 | // C++ [over.operator]p23: |
| 4967 | // |
| 4968 | // There also exist candidate operator functions of the form |
| 4969 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4971 | // bool operator&&(bool, bool); [BELOW] |
| 4972 | // bool operator||(bool, bool); [BELOW] |
| 4973 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4974 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4975 | /*IsAssignmentOperator=*/false, |
| 4976 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4977 | break; |
| 4978 | } |
| 4979 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4980 | case OO_AmpAmp: |
| 4981 | case OO_PipePipe: { |
| 4982 | // C++ [over.operator]p23: |
| 4983 | // |
| 4984 | // There also exist candidate operator functions of the form |
| 4985 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4986 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4987 | // bool operator&&(bool, bool); |
| 4988 | // bool operator||(bool, bool); |
| 4989 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4990 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4991 | /*IsAssignmentOperator=*/false, |
| 4992 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4993 | break; |
| 4994 | } |
| 4995 | |
| 4996 | case OO_Subscript: |
| 4997 | // C++ [over.built]p13: |
| 4998 | // |
| 4999 | // For every cv-qualified or cv-unqualified object type T there |
| 5000 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5002 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5003 | // T& operator[](T*, ptrdiff_t); |
| 5004 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5005 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5006 | // T& operator[](ptrdiff_t, T*); |
| 5007 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 5008 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5009 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5010 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5011 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5012 | |
| 5013 | // T& operator[](T*, ptrdiff_t) |
| 5014 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5015 | |
| 5016 | // T& operator[](ptrdiff_t, T*); |
| 5017 | ParamTypes[0] = ParamTypes[1]; |
| 5018 | ParamTypes[1] = *Ptr; |
| 5019 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5020 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5021 | break; |
| 5022 | |
| 5023 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5024 | // C++ [over.built]p11: |
| 5025 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5026 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5027 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5028 | // there exist candidate operator functions of the form |
| 5029 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5030 | // where CV12 is the union of CV1 and CV2. |
| 5031 | { |
| 5032 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5033 | CandidateTypes.pointer_begin(); |
| 5034 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 5035 | QualType C1Ty = (*Ptr); |
| 5036 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5037 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5038 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5039 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5040 | if (!isa<RecordType>(C1)) |
| 5041 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5042 | // heuristic to reduce number of builtin candidates in the set. |
| 5043 | // Add volatile/restrict version only if there are conversions to a |
| 5044 | // volatile/restrict type. |
| 5045 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5046 | continue; |
| 5047 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5048 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5049 | } |
| 5050 | for (BuiltinCandidateTypeSet::iterator |
| 5051 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 5052 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 5053 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5054 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5055 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 5056 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5057 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 5058 | break; |
| 5059 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5060 | // build CV12 T& |
| 5061 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5062 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5063 | T.isVolatileQualified()) |
| 5064 | continue; |
| 5065 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5066 | T.isRestrictQualified()) |
| 5067 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 5068 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 5069 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 5070 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5071 | } |
| 5072 | } |
| 5073 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5074 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5075 | |
| 5076 | case OO_Conditional: |
| 5077 | // Note that we don't consider the first argument, since it has been |
| 5078 | // contextually converted to bool long ago. The candidates below are |
| 5079 | // therefore added as binary. |
| 5080 | // |
| 5081 | // C++ [over.built]p24: |
| 5082 | // For every type T, where T is a pointer or pointer-to-member type, |
| 5083 | // there exist candidate operator functions of the form |
| 5084 | // |
| 5085 | // T operator?(bool, T, T); |
| 5086 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5087 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 5088 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 5089 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5090 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5091 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 5092 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 5093 | CandidateTypes.member_pointer_begin(), |
| 5094 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 5095 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5096 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5097 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5098 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5102 | /// \brief Add function candidates found via argument-dependent lookup |
| 5103 | /// to the set of overloading candidates. |
| 5104 | /// |
| 5105 | /// This routine performs argument-dependent name lookup based on the |
| 5106 | /// given function name (which may also be an operator name) and adds |
| 5107 | /// all of the overload candidates found by ADL to the overload |
| 5108 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5109 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5110 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5111 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5112 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5113 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5114 | OverloadCandidateSet& CandidateSet, |
| 5115 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5116 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5117 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 5118 | // FIXME: This approach for uniquing ADL results (and removing |
| 5119 | // redundant candidates from the set) relies on pointer-equality, |
| 5120 | // which means we need to key off the canonical decl. However, |
| 5121 | // always going back to the canonical decl might not get us the |
| 5122 | // right set of default arguments. What default arguments are |
| 5123 | // we supposed to consider on ADL candidates, anyway? |
| 5124 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5125 | // FIXME: Pass in the explicit template arguments? |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5126 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5127 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5128 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5129 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5130 | CandEnd = CandidateSet.end(); |
| 5131 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5132 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5133 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5134 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5135 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5136 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5137 | |
| 5138 | // For each of the ADL candidates we found, add it to the overload |
| 5139 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 5140 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5141 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5142 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5143 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5144 | continue; |
| 5145 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5146 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5147 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5148 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5149 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5150 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 5151 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 5152 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5155 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 5156 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5157 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5158 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5159 | const OverloadCandidate& Cand2, |
| 5160 | SourceLocation Loc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5161 | // Define viable functions to be better candidates than non-viable |
| 5162 | // functions. |
| 5163 | if (!Cand2.Viable) |
| 5164 | return Cand1.Viable; |
| 5165 | else if (!Cand1.Viable) |
| 5166 | return false; |
| 5167 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5168 | // C++ [over.match.best]p1: |
| 5169 | // |
| 5170 | // -- if F is a static member function, ICS1(F) is defined such |
| 5171 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 5172 | // any function G, and, symmetrically, ICS1(G) is neither |
| 5173 | // better nor worse than ICS1(F). |
| 5174 | unsigned StartArg = 0; |
| 5175 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 5176 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5177 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5178 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5179 | // A viable function F1 is defined to be a better function than another |
| 5180 | // 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] | 5181 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5182 | unsigned NumArgs = Cand1.Conversions.size(); |
| 5183 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 5184 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5185 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5186 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 5187 | Cand2.Conversions[ArgIdx])) { |
| 5188 | case ImplicitConversionSequence::Better: |
| 5189 | // Cand1 has a better conversion sequence. |
| 5190 | HasBetterConversion = true; |
| 5191 | break; |
| 5192 | |
| 5193 | case ImplicitConversionSequence::Worse: |
| 5194 | // Cand1 can't be better than Cand2. |
| 5195 | return false; |
| 5196 | |
| 5197 | case ImplicitConversionSequence::Indistinguishable: |
| 5198 | // Do nothing. |
| 5199 | break; |
| 5200 | } |
| 5201 | } |
| 5202 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5203 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5204 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5205 | if (HasBetterConversion) |
| 5206 | return true; |
| 5207 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5209 | // specialization, or, if not that, |
Douglas Gregor | ccd4713 | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 5210 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5211 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 5212 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5213 | |
| 5214 | // -- F1 and F2 are function template specializations, and the function |
| 5215 | // template for F1 is more specialized than the template for F2 |
| 5216 | // 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] | 5217 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 5218 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 5219 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5220 | if (FunctionTemplateDecl *BetterTemplate |
| 5221 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 5222 | Cand2.Function->getPrimaryTemplate(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5223 | Loc, |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 5224 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 5225 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5226 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5227 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5228 | // -- the context is an initialization by user-defined conversion |
| 5229 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 5230 | // from the return type of F1 to the destination type (i.e., |
| 5231 | // the type of the entity being initialized) is a better |
| 5232 | // conversion sequence than the standard conversion sequence |
| 5233 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5234 | if (Cand1.Function && Cand2.Function && |
| 5235 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5236 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 5237 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 5238 | Cand2.FinalConversion)) { |
| 5239 | case ImplicitConversionSequence::Better: |
| 5240 | // Cand1 has a better conversion sequence. |
| 5241 | return true; |
| 5242 | |
| 5243 | case ImplicitConversionSequence::Worse: |
| 5244 | // Cand1 can't be better than Cand2. |
| 5245 | return false; |
| 5246 | |
| 5247 | case ImplicitConversionSequence::Indistinguishable: |
| 5248 | // Do nothing |
| 5249 | break; |
| 5250 | } |
| 5251 | } |
| 5252 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5253 | return false; |
| 5254 | } |
| 5255 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5256 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5257 | /// within an overload candidate set. |
| 5258 | /// |
| 5259 | /// \param CandidateSet the set of candidate functions. |
| 5260 | /// |
| 5261 | /// \param Loc the location of the function name (or operator symbol) for |
| 5262 | /// which overload resolution occurs. |
| 5263 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5264 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5265 | /// function, Best points to the candidate function found. |
| 5266 | /// |
| 5267 | /// \returns The result of overload resolution. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5268 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 5269 | SourceLocation Loc, |
| 5270 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5271 | // Find the best viable function. |
| 5272 | Best = CandidateSet.end(); |
| 5273 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 5274 | Cand != CandidateSet.end(); ++Cand) { |
| 5275 | if (Cand->Viable) { |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5276 | if (Best == CandidateSet.end() || |
| 5277 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5278 | Best = Cand; |
| 5279 | } |
| 5280 | } |
| 5281 | |
| 5282 | // If we didn't find any viable functions, abort. |
| 5283 | if (Best == CandidateSet.end()) |
| 5284 | return OR_No_Viable_Function; |
| 5285 | |
| 5286 | // Make sure that this function is better than every other viable |
| 5287 | // function. If not, we have an ambiguity. |
| 5288 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 5289 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5290 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5291 | Cand != Best && |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5292 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5293 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5294 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5295 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5297 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5298 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5299 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5300 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5301 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5302 | return OR_Deleted; |
| 5303 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5304 | // C++ [basic.def.odr]p2: |
| 5305 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5306 | // when referred to from a potentially-evaluated expression. [Note: this |
| 5307 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5308 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 5309 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 5310 | if (Best->Function) |
| 5311 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5312 | return OR_Success; |
| 5313 | } |
| 5314 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5315 | namespace { |
| 5316 | |
| 5317 | enum OverloadCandidateKind { |
| 5318 | oc_function, |
| 5319 | oc_method, |
| 5320 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5321 | oc_function_template, |
| 5322 | oc_method_template, |
| 5323 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5324 | oc_implicit_default_constructor, |
| 5325 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5326 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5327 | }; |
| 5328 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5329 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 5330 | FunctionDecl *Fn, |
| 5331 | std::string &Description) { |
| 5332 | bool isTemplate = false; |
| 5333 | |
| 5334 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 5335 | isTemplate = true; |
| 5336 | Description = S.getTemplateArgumentBindingsText( |
| 5337 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 5338 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5339 | |
| 5340 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5341 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5342 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5343 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5344 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 5345 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5349 | // This actually gets spelled 'candidate function' for now, but |
| 5350 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5351 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5352 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5353 | |
| 5354 | assert(Meth->isCopyAssignment() |
| 5355 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5356 | return oc_implicit_copy_assignment; |
| 5357 | } |
| 5358 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5359 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
| 5362 | } // end anonymous namespace |
| 5363 | |
| 5364 | // Notes the location of an overload candidate. |
| 5365 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5366 | std::string FnDesc; |
| 5367 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 5368 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 5369 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5372 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 5373 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 5374 | /// target types of the conversion. |
| 5375 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 5376 | SourceLocation CaretLoc, |
| 5377 | const PartialDiagnostic &PDiag) { |
| 5378 | Diag(CaretLoc, PDiag) |
| 5379 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 5380 | for (AmbiguousConversionSequence::const_iterator |
| 5381 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 5382 | NoteOverloadCandidate(*I); |
| 5383 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5384 | } |
| 5385 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5386 | namespace { |
| 5387 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5388 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 5389 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 5390 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5391 | assert(Cand->Function && "for now, candidate must be a function"); |
| 5392 | FunctionDecl *Fn = Cand->Function; |
| 5393 | |
| 5394 | // There's a conversion slot for the object argument if this is a |
| 5395 | // non-constructor method. Note that 'I' corresponds the |
| 5396 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5397 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5398 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5399 | if (I == 0) |
| 5400 | isObjectArgument = true; |
| 5401 | else |
| 5402 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5405 | std::string FnDesc; |
| 5406 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 5407 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5408 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 5409 | QualType FromTy = Conv.Bad.getFromType(); |
| 5410 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5411 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5412 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5413 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5414 | Expr *E = FromExpr->IgnoreParens(); |
| 5415 | if (isa<UnaryOperator>(E)) |
| 5416 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5417 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5418 | |
| 5419 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 5420 | << (unsigned) FnKind << FnDesc |
| 5421 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5422 | << ToTy << Name << I+1; |
| 5423 | return; |
| 5424 | } |
| 5425 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5426 | // Do some hand-waving analysis to see if the non-viability is due |
| 5427 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5428 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 5429 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 5430 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 5431 | CToTy = RT->getPointeeType(); |
| 5432 | else { |
| 5433 | // TODO: detect and diagnose the full richness of const mismatches. |
| 5434 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 5435 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 5436 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 5437 | } |
| 5438 | |
| 5439 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 5440 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 5441 | // It is dumb that we have to do this here. |
| 5442 | while (isa<ArrayType>(CFromTy)) |
| 5443 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5444 | while (isa<ArrayType>(CToTy)) |
| 5445 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5446 | |
| 5447 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 5448 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 5449 | |
| 5450 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 5451 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 5452 | << (unsigned) FnKind << FnDesc |
| 5453 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5454 | << FromTy |
| 5455 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 5456 | << (unsigned) isObjectArgument << I+1; |
| 5457 | return; |
| 5458 | } |
| 5459 | |
| 5460 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5461 | assert(CVR && "unexpected qualifiers mismatch"); |
| 5462 | |
| 5463 | if (isObjectArgument) { |
| 5464 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 5465 | << (unsigned) FnKind << FnDesc |
| 5466 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5467 | << FromTy << (CVR - 1); |
| 5468 | } else { |
| 5469 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 5470 | << (unsigned) FnKind << FnDesc |
| 5471 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5472 | << FromTy << (CVR - 1) << I+1; |
| 5473 | } |
| 5474 | return; |
| 5475 | } |
| 5476 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5477 | // Diagnose references or pointers to incomplete types differently, |
| 5478 | // since it's far from impossible that the incompleteness triggered |
| 5479 | // the failure. |
| 5480 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 5481 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 5482 | TempFromTy = PTy->getPointeeType(); |
| 5483 | if (TempFromTy->isIncompleteType()) { |
| 5484 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 5485 | << (unsigned) FnKind << FnDesc |
| 5486 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5487 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 5488 | return; |
| 5489 | } |
| 5490 | |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5491 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5492 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5493 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 5494 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 5495 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5496 | FromPtrTy->getPointeeType()) && |
| 5497 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 5498 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
| 5499 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
| 5500 | FromPtrTy->getPointeeType())) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5501 | BaseToDerivedConversion = 1; |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5502 | } |
| 5503 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 5504 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 5505 | if (const ObjCObjectPointerType *ToPtrTy |
| 5506 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 5507 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 5508 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 5509 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 5510 | FromPtrTy->getPointeeType()) && |
| 5511 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5512 | BaseToDerivedConversion = 2; |
| 5513 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 5514 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 5515 | !FromTy->isIncompleteType() && |
| 5516 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 5517 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 5518 | BaseToDerivedConversion = 3; |
| 5519 | } |
| 5520 | |
| 5521 | if (BaseToDerivedConversion) { |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5522 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5523 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5524 | << (unsigned) FnKind << FnDesc |
| 5525 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | 2f9d874 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 5526 | << (BaseToDerivedConversion - 1) |
Douglas Gregor | 8578981 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 5527 | << FromTy << ToTy << I+1; |
| 5528 | return; |
| 5529 | } |
| 5530 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5531 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5532 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 5533 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5534 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 5535 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 5539 | unsigned NumFormalArgs) { |
| 5540 | // TODO: treat calls to a missing default constructor as a special case |
| 5541 | |
| 5542 | FunctionDecl *Fn = Cand->Function; |
| 5543 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 5544 | |
| 5545 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 5546 | |
| 5547 | // at least / at most / exactly |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5548 | // FIXME: variadic templates "at most" should account for parameter packs |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5549 | unsigned mode, modeCount; |
| 5550 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5551 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 5552 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5553 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5554 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 5555 | mode = 0; // "at least" |
| 5556 | else |
| 5557 | mode = 2; // "exactly" |
| 5558 | modeCount = MinParams; |
| 5559 | } else { |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5560 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 5561 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5562 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5563 | if (MinParams != FnTy->getNumArgs()) |
| 5564 | mode = 1; // "at most" |
| 5565 | else |
| 5566 | mode = 2; // "exactly" |
| 5567 | modeCount = FnTy->getNumArgs(); |
| 5568 | } |
| 5569 | |
| 5570 | std::string Description; |
| 5571 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 5572 | |
| 5573 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5574 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 5575 | << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5578 | /// Diagnose a failed template-argument deduction. |
| 5579 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 5580 | Expr **Args, unsigned NumArgs) { |
| 5581 | FunctionDecl *Fn = Cand->Function; // pattern |
| 5582 | |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5583 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5584 | NamedDecl *ParamD; |
| 5585 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 5586 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 5587 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5588 | switch (Cand->DeductionFailure.Result) { |
| 5589 | case Sema::TDK_Success: |
| 5590 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 5591 | |
| 5592 | case Sema::TDK_Incomplete: { |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5593 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 5594 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 5595 | << ParamD->getDeclName(); |
| 5596 | return; |
| 5597 | } |
| 5598 | |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 5599 | case Sema::TDK_Underqualified: { |
| 5600 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 5601 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 5602 | |
| 5603 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 5604 | |
| 5605 | // Param will have been canonicalized, but it should just be a |
| 5606 | // qualified version of ParamD, so move the qualifiers to that. |
| 5607 | QualifierCollector Qs(S.Context); |
| 5608 | Qs.strip(Param); |
| 5609 | QualType NonCanonParam = Qs.apply(TParam->getTypeForDecl()); |
| 5610 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 5611 | |
| 5612 | // Arg has also been canonicalized, but there's nothing we can do |
| 5613 | // about that. It also doesn't matter as much, because it won't |
| 5614 | // have any template parameters in it (because deduction isn't |
| 5615 | // done on dependent types). |
| 5616 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 5617 | |
| 5618 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 5619 | << ParamD->getDeclName() << Arg << NonCanonParam; |
| 5620 | return; |
| 5621 | } |
| 5622 | |
| 5623 | case Sema::TDK_Inconsistent: { |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5624 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5625 | int which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5626 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5627 | which = 0; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5628 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5629 | which = 1; |
| 5630 | else { |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5631 | which = 2; |
| 5632 | } |
| 5633 | |
| 5634 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
| 5635 | << which << ParamD->getDeclName() |
| 5636 | << *Cand->DeductionFailure.getFirstArg() |
| 5637 | << *Cand->DeductionFailure.getSecondArg(); |
| 5638 | return; |
| 5639 | } |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5640 | |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5641 | case Sema::TDK_InvalidExplicitArguments: |
| 5642 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| 5643 | if (ParamD->getDeclName()) |
| 5644 | S.Diag(Fn->getLocation(), |
| 5645 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 5646 | << ParamD->getDeclName(); |
| 5647 | else { |
| 5648 | int index = 0; |
| 5649 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 5650 | index = TTP->getIndex(); |
| 5651 | else if (NonTypeTemplateParmDecl *NTTP |
| 5652 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 5653 | index = NTTP->getIndex(); |
| 5654 | else |
| 5655 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| 5656 | S.Diag(Fn->getLocation(), |
| 5657 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 5658 | << (index + 1); |
| 5659 | } |
| 5660 | return; |
| 5661 | |
Douglas Gregor | a18592e | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5662 | case Sema::TDK_TooManyArguments: |
| 5663 | case Sema::TDK_TooFewArguments: |
| 5664 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 5665 | return; |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 5666 | |
| 5667 | case Sema::TDK_InstantiationDepth: |
| 5668 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
| 5669 | return; |
| 5670 | |
| 5671 | case Sema::TDK_SubstitutionFailure: { |
| 5672 | std::string ArgString; |
| 5673 | if (TemplateArgumentList *Args |
| 5674 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 5675 | ArgString = S.getTemplateArgumentBindingsText( |
| 5676 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 5677 | *Args); |
| 5678 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 5679 | << ArgString; |
| 5680 | return; |
| 5681 | } |
Douglas Gregor | a933319 | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5682 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5683 | // TODO: diagnose these individually, then kill off |
| 5684 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5685 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5686 | case Sema::TDK_FailedOverloadResolution: |
| 5687 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 5688 | return; |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 5693 | /// already generated a primary error at the call site. |
| 5694 | /// |
| 5695 | /// It really does need to be a single diagnostic with its caret |
| 5696 | /// pointed at the candidate declaration. Yes, this creates some |
| 5697 | /// major challenges of technical writing. Yes, this makes pointing |
| 5698 | /// out problems with specific arguments quite awkward. It's still |
| 5699 | /// better than generating twenty screens of text for every failed |
| 5700 | /// overload. |
| 5701 | /// |
| 5702 | /// It would be great to be able to express per-candidate problems |
| 5703 | /// more richly for those diagnostic clients that cared, but we'd |
| 5704 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5705 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 5706 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5707 | FunctionDecl *Fn = Cand->Function; |
| 5708 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5709 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5710 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5711 | std::string FnDesc; |
| 5712 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5713 | |
| 5714 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5715 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5716 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5717 | } |
| 5718 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5719 | // We don't really have anything else to say about viable candidates. |
| 5720 | if (Cand->Viable) { |
| 5721 | S.NoteOverloadCandidate(Fn); |
| 5722 | return; |
| 5723 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5724 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5725 | switch (Cand->FailureKind) { |
| 5726 | case ovl_fail_too_many_arguments: |
| 5727 | case ovl_fail_too_few_arguments: |
| 5728 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5729 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5730 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5731 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 5732 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5733 | case ovl_fail_trivial_conversion: |
| 5734 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5735 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5736 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5737 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5738 | case ovl_fail_bad_conversion: { |
| 5739 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 5740 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5741 | if (Cand->Conversions[I].isBad()) |
| 5742 | return DiagnoseBadConversion(S, Cand, I); |
| 5743 | |
| 5744 | // FIXME: this currently happens when we're called from SemaInit |
| 5745 | // when user-conversion overload fails. Figure out how to handle |
| 5746 | // those conditions and diagnose them well. |
| 5747 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5748 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5749 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 5753 | // Desugar the type of the surrogate down to a function type, |
| 5754 | // retaining as many typedefs as possible while still showing |
| 5755 | // the function type (and, therefore, its parameter types). |
| 5756 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 5757 | bool isLValueReference = false; |
| 5758 | bool isRValueReference = false; |
| 5759 | bool isPointer = false; |
| 5760 | if (const LValueReferenceType *FnTypeRef = |
| 5761 | FnType->getAs<LValueReferenceType>()) { |
| 5762 | FnType = FnTypeRef->getPointeeType(); |
| 5763 | isLValueReference = true; |
| 5764 | } else if (const RValueReferenceType *FnTypeRef = |
| 5765 | FnType->getAs<RValueReferenceType>()) { |
| 5766 | FnType = FnTypeRef->getPointeeType(); |
| 5767 | isRValueReference = true; |
| 5768 | } |
| 5769 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5770 | FnType = FnTypePtr->getPointeeType(); |
| 5771 | isPointer = true; |
| 5772 | } |
| 5773 | // Desugar down to a function type. |
| 5774 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5775 | // Reconstruct the pointer/reference as appropriate. |
| 5776 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5777 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5778 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5779 | |
| 5780 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5781 | << FnType; |
| 5782 | } |
| 5783 | |
| 5784 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5785 | const char *Opc, |
| 5786 | SourceLocation OpLoc, |
| 5787 | OverloadCandidate *Cand) { |
| 5788 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5789 | std::string TypeStr("operator"); |
| 5790 | TypeStr += Opc; |
| 5791 | TypeStr += "("; |
| 5792 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5793 | if (Cand->Conversions.size() == 1) { |
| 5794 | TypeStr += ")"; |
| 5795 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5796 | } else { |
| 5797 | TypeStr += ", "; |
| 5798 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5799 | TypeStr += ")"; |
| 5800 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5805 | OverloadCandidate *Cand) { |
| 5806 | unsigned NoOperands = Cand->Conversions.size(); |
| 5807 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5808 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5809 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5810 | if (!ICS.isAmbiguous()) continue; |
| 5811 | |
| 5812 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5813 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5814 | } |
| 5815 | } |
| 5816 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5817 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5818 | if (Cand->Function) |
| 5819 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5820 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5821 | return Cand->Surrogate->getLocation(); |
| 5822 | return SourceLocation(); |
| 5823 | } |
| 5824 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5825 | struct CompareOverloadCandidatesForDisplay { |
| 5826 | Sema &S; |
| 5827 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5828 | |
| 5829 | bool operator()(const OverloadCandidate *L, |
| 5830 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5831 | // Fast-path this check. |
| 5832 | if (L == R) return false; |
| 5833 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5834 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5835 | if (L->Viable) { |
| 5836 | if (!R->Viable) return true; |
| 5837 | |
| 5838 | // TODO: introduce a tri-valued comparison for overload |
| 5839 | // candidates. Would be more worthwhile if we had a sort |
| 5840 | // that could exploit it. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5841 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 5842 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5843 | } else if (R->Viable) |
| 5844 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5845 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5846 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5847 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5848 | // Criteria by which we can sort non-viable candidates: |
| 5849 | if (!L->Viable) { |
| 5850 | // 1. Arity mismatches come after other candidates. |
| 5851 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5852 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5853 | return false; |
| 5854 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5855 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5856 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5857 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5858 | // 2. Bad conversions come first and are ordered by the number |
| 5859 | // of bad conversions and quality of good conversions. |
| 5860 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5861 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5862 | return true; |
| 5863 | |
| 5864 | // If there's any ordering between the defined conversions... |
| 5865 | // FIXME: this might not be transitive. |
| 5866 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5867 | |
| 5868 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5869 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5870 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5871 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 5872 | R->Conversions[I])) { |
| 5873 | case ImplicitConversionSequence::Better: |
| 5874 | leftBetter++; |
| 5875 | break; |
| 5876 | |
| 5877 | case ImplicitConversionSequence::Worse: |
| 5878 | leftBetter--; |
| 5879 | break; |
| 5880 | |
| 5881 | case ImplicitConversionSequence::Indistinguishable: |
| 5882 | break; |
| 5883 | } |
| 5884 | } |
| 5885 | if (leftBetter > 0) return true; |
| 5886 | if (leftBetter < 0) return false; |
| 5887 | |
| 5888 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 5889 | return false; |
| 5890 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5891 | // TODO: others? |
| 5892 | } |
| 5893 | |
| 5894 | // Sort everything else by location. |
| 5895 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 5896 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 5897 | |
| 5898 | // Put candidates without locations (e.g. builtins) at the end. |
| 5899 | if (LLoc.isInvalid()) return false; |
| 5900 | if (RLoc.isInvalid()) return true; |
| 5901 | |
| 5902 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5903 | } |
| 5904 | }; |
| 5905 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5906 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 5907 | /// computes up to the first |
| 5908 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 5909 | Expr **Args, unsigned NumArgs) { |
| 5910 | assert(!Cand->Viable); |
| 5911 | |
| 5912 | // Don't do anything on failures other than bad conversion. |
| 5913 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 5914 | |
| 5915 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5916 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5917 | unsigned ConvCount = Cand->Conversions.size(); |
| 5918 | while (true) { |
| 5919 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 5920 | ConvIdx++; |
| 5921 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 5922 | break; |
| 5923 | } |
| 5924 | |
| 5925 | if (ConvIdx == ConvCount) |
| 5926 | return; |
| 5927 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5928 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 5929 | "remaining conversion is initialized?"); |
| 5930 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 5931 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5932 | // operation somehow. |
| 5933 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5934 | |
| 5935 | const FunctionProtoType* Proto; |
| 5936 | unsigned ArgIdx = ConvIdx; |
| 5937 | |
| 5938 | if (Cand->IsSurrogate) { |
| 5939 | QualType ConvType |
| 5940 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 5941 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5942 | ConvType = ConvPtrType->getPointeeType(); |
| 5943 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 5944 | ArgIdx--; |
| 5945 | } else if (Cand->Function) { |
| 5946 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 5947 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 5948 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 5949 | ArgIdx--; |
| 5950 | } else { |
| 5951 | // Builtin binary operator with a bad first conversion. |
| 5952 | assert(ConvCount <= 3); |
| 5953 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 5954 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5955 | = TryCopyInitialization(S, Args[ConvIdx], |
| 5956 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 5957 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5958 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5959 | return; |
| 5960 | } |
| 5961 | |
| 5962 | // Fill in the rest of the conversions. |
| 5963 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5964 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 5965 | if (ArgIdx < NumArgsInProto) |
| 5966 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5967 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 5968 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5969 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5970 | else |
| 5971 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 5972 | } |
| 5973 | } |
| 5974 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5975 | } // end anonymous namespace |
| 5976 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5977 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 5978 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5979 | /// set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5980 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5981 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5982 | OverloadCandidateDisplayKind OCD, |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5983 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5984 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 5985 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5986 | // Sort the candidates by viability and position. Sorting directly would |
| 5987 | // be prohibitive, so we make a set of pointers and sort those. |
| 5988 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 5989 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 5990 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5991 | LastCand = CandidateSet.end(); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5992 | Cand != LastCand; ++Cand) { |
| 5993 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5994 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5995 | else if (OCD == OCD_AllCandidates) { |
| 5996 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5997 | if (Cand->Function || Cand->IsSurrogate) |
| 5998 | Cands.push_back(Cand); |
| 5999 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 6000 | // want to list every possible builtin candidate. |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6001 | } |
| 6002 | } |
| 6003 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6004 | std::sort(Cands.begin(), Cands.end(), |
| 6005 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6006 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6007 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6008 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6009 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6010 | const Diagnostic::OverloadsShown ShowOverloads = Diags.getShowOverloads(); |
| 6011 | unsigned CandsShown = 0; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6012 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 6013 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 6014 | |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6015 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 6016 | // the user with. FIXME: This limit should depend on details of the |
| 6017 | // candidate list. |
| 6018 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 6019 | break; |
| 6020 | } |
| 6021 | ++CandsShown; |
| 6022 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6023 | if (Cand->Function) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6024 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6025 | else if (Cand->IsSurrogate) |
| 6026 | NoteSurrogateCandidate(*this, Cand); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6027 | else { |
| 6028 | assert(Cand->Viable && |
| 6029 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6030 | // Generally we only see ambiguities including viable builtin |
| 6031 | // operators if overload resolution got screwed up by an |
| 6032 | // ambiguous user-defined conversion. |
| 6033 | // |
| 6034 | // FIXME: It's quite possible for different conversions to see |
| 6035 | // different ambiguities, though. |
| 6036 | if (!ReportedAmbiguousConversions) { |
| 6037 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 6038 | ReportedAmbiguousConversions = true; |
| 6039 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6040 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6041 | // If this is a viable builtin, print it. |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6042 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6043 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6044 | } |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 6045 | |
| 6046 | if (I != E) |
Jeffrey Yasskin | 258de30 | 2010-06-11 06:58:43 +0000 | [diff] [blame] | 6047 | Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6050 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6051 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6052 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6053 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6054 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6055 | } |
| 6056 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6057 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 6058 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 6059 | /// expression with overloaded function type and @p ToType is the type |
| 6060 | /// we're trying to resolve to. For example: |
| 6061 | /// |
| 6062 | /// @code |
| 6063 | /// int f(double); |
| 6064 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6065 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6066 | /// int (*pfd)(double) = f; // selects f(double) |
| 6067 | /// @endcode |
| 6068 | /// |
| 6069 | /// This routine returns the resulting FunctionDecl if it could be |
| 6070 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 6071 | /// routine will emit diagnostics if there is an error. |
| 6072 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6073 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6074 | bool Complain, |
| 6075 | DeclAccessPair &FoundResult) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6076 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6077 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6078 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6079 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6080 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 6081 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6082 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6083 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6084 | FunctionType = MemTypePtr->getPointeeType(); |
| 6085 | IsMember = true; |
| 6086 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6087 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6088 | // C++ [over.over]p1: |
| 6089 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6090 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6091 | // C++ [over.over]p1: |
| 6092 | // [...] The overloaded function name can be preceded by the & |
| 6093 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6094 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 6095 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 6096 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 6097 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 6098 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6099 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6100 | |
| 6101 | // We expect a pointer or reference to function, or a function pointer. |
| 6102 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 6103 | if (!FunctionType->isFunctionType()) { |
| 6104 | if (Complain) |
| 6105 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 6106 | << OvlExpr->getName() << ToType; |
| 6107 | |
| 6108 | return 0; |
| 6109 | } |
| 6110 | |
| 6111 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6112 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6113 | // Look through all of the overloaded functions, searching for one |
| 6114 | // whose type matches exactly. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6115 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 6116 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 6117 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6118 | bool FoundNonTemplateFunction = false; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6119 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6120 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6121 | // Look through any using declarations to find the underlying function. |
| 6122 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 6123 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6124 | // C++ [over.over]p3: |
| 6125 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 6126 | // targets of type "pointer-to-function" or "reference-to-function." |
| 6127 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6128 | // type "pointer-to-member-function." |
| 6129 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6130 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6131 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6132 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6133 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6134 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6135 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6136 | // static when converting to member pointer. |
| 6137 | if (Method->isStatic() == IsMember) |
| 6138 | continue; |
| 6139 | } else if (IsMember) |
| 6140 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6142 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | // If the name is a function template, template argument deduction is |
| 6144 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6145 | // resulting template argument list is used to generate a single |
| 6146 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6147 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6148 | // 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] | 6149 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6150 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6151 | if (TemplateDeductionResult Result |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6152 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6153 | FunctionType, Specialization, Info)) { |
| 6154 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6155 | (void)Result; |
| 6156 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 6157 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 6158 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6160 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6161 | Matches.push_back(std::make_pair(I.getPair(), |
| 6162 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6163 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6164 | |
| 6165 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 6166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6167 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6168 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6169 | // Skip non-static functions when converting to pointer, and static |
| 6170 | // when converting to member pointer. |
| 6171 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6172 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6173 | |
| 6174 | // If we have explicit template arguments, skip non-templates. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6175 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6176 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6177 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6178 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6179 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 6180 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 6181 | QualType ResultTy; |
| 6182 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 6183 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 6184 | ResultTy)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6185 | Matches.push_back(std::make_pair(I.getPair(), |
| 6186 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6187 | FoundNonTemplateFunction = true; |
| 6188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6189 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6190 | } |
| 6191 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6192 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6193 | if (Matches.empty()) { |
| 6194 | if (Complain) { |
| 6195 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 6196 | << OvlExpr->getName() << FunctionType; |
| 6197 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6198 | E = OvlExpr->decls_end(); |
| 6199 | I != E; ++I) |
| 6200 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 6201 | NoteOverloadCandidate(F); |
| 6202 | } |
| 6203 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6204 | return 0; |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 6205 | } else if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6206 | FunctionDecl *Result = Matches[0].second; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6207 | FoundResult = Matches[0].first; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6208 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6209 | if (Complain) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6210 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6211 | return Result; |
| 6212 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6213 | |
| 6214 | // C++ [over.over]p4: |
| 6215 | // If more than one function is selected, [...] |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6216 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6217 | // [...] and any given function template specialization F1 is |
| 6218 | // eliminated if the set contains a second function template |
| 6219 | // specialization whose function template is more specialized |
| 6220 | // than the function template of F1 according to the partial |
| 6221 | // ordering rules of 14.5.5.2. |
| 6222 | |
| 6223 | // The algorithm specified above is quadratic. We instead use a |
| 6224 | // two-pass algorithm (similar to the one used to identify the |
| 6225 | // best viable function in an overload set) that identifies the |
| 6226 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6227 | |
| 6228 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 6229 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6230 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6231 | |
| 6232 | UnresolvedSetIterator Result = |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6233 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6234 | TPOC_Other, From->getLocStart(), |
| 6235 | PDiag(), |
| 6236 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6237 | << Matches[0].second->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6238 | PDiag(diag::note_ovl_candidate) |
| 6239 | << (unsigned) oc_function_template); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6240 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6241 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6242 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6243 | if (Complain) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6244 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6245 | DiagnoseUseOfDecl(FoundResult, OvlExpr->getNameLoc()); |
| 6246 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6247 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6249 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6250 | // [...] any function template specializations in the set are |
| 6251 | // eliminated if the set also contains a non-template function, [...] |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6252 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6253 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6254 | ++I; |
| 6255 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6256 | Matches[I] = Matches[--N]; |
| 6257 | Matches.set_size(N); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6258 | } |
| 6259 | } |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 6260 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6261 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6262 | // selected function. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6263 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6264 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6265 | FoundResult = Matches[0].first; |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6266 | if (Complain) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6267 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6268 | DiagnoseUseOfDecl(Matches[0].first, OvlExpr->getNameLoc()); |
| 6269 | } |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6270 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6272 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6273 | // FIXME: We should probably return the same thing that BestViableFunction |
| 6274 | // returns (even if we issue the diagnostics here). |
| 6275 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6276 | << Matches[0].second->getDeclName(); |
| 6277 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6278 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6279 | return 0; |
| 6280 | } |
| 6281 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6282 | /// \brief Given an expression that refers to an overloaded function, try to |
| 6283 | /// resolve that overloaded function expression down to a single function. |
| 6284 | /// |
| 6285 | /// This routine can only resolve template-ids that refer to a single function |
| 6286 | /// template, where that template-id refers to a single template whose template |
| 6287 | /// arguments are either provided by the template-id or have defaults, |
| 6288 | /// as described in C++0x [temp.arg.explicit]p3. |
| 6289 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 6290 | // C++ [over.over]p1: |
| 6291 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6292 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6293 | // C++ [over.over]p1: |
| 6294 | // [...] The overloaded function name can be preceded by the & |
| 6295 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6296 | |
| 6297 | if (From->getType() != Context.OverloadTy) |
| 6298 | return 0; |
| 6299 | |
| 6300 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6301 | |
| 6302 | // If we didn't actually find any template-ids, we're done. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6303 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6304 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6305 | |
| 6306 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 6307 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6308 | |
| 6309 | // Look through all of the overloaded functions, searching for one |
| 6310 | // whose type matches exactly. |
| 6311 | FunctionDecl *Matched = 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6312 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6313 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6314 | // C++0x [temp.arg.explicit]p3: |
| 6315 | // [...] In contexts where deduction is done and fails, or in contexts |
| 6316 | // where deduction is not done, if a template argument list is |
| 6317 | // specified and it, along with any default template arguments, |
| 6318 | // identifies a single function template specialization, then the |
| 6319 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | 66a8c9a | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 6320 | FunctionTemplateDecl *FunctionTemplate |
| 6321 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6322 | |
| 6323 | // C++ [over.over]p2: |
| 6324 | // If the name is a function template, template argument deduction is |
| 6325 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6326 | // resulting template argument list is used to generate a single |
| 6327 | // function template specialization, which is added to the set of |
| 6328 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6329 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6330 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6331 | if (TemplateDeductionResult Result |
| 6332 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 6333 | Specialization, Info)) { |
| 6334 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6335 | (void)Result; |
| 6336 | continue; |
| 6337 | } |
| 6338 | |
| 6339 | // Multiple matches; we can't resolve to a single declaration. |
| 6340 | if (Matched) |
| 6341 | return 0; |
| 6342 | |
| 6343 | Matched = Specialization; |
| 6344 | } |
| 6345 | |
| 6346 | return Matched; |
| 6347 | } |
| 6348 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6349 | /// \brief Add a single candidate to the overload set. |
| 6350 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6351 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6352 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
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 | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6356 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6357 | if (isa<UsingShadowDecl>(Callee)) |
| 6358 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 6359 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6360 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6361 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6362 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6363 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6364 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6365 | } |
| 6366 | |
| 6367 | if (FunctionTemplateDecl *FuncTemplate |
| 6368 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6369 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 6370 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6371 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6372 | return; |
| 6373 | } |
| 6374 | |
| 6375 | assert(false && "unhandled case in overloaded call candidate"); |
| 6376 | |
| 6377 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 6381 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6382 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6383 | Expr **Args, unsigned NumArgs, |
| 6384 | OverloadCandidateSet &CandidateSet, |
| 6385 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6386 | |
| 6387 | #ifndef NDEBUG |
| 6388 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 6389 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6390 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6391 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 6392 | // and let Y be the lookup set produced by argument dependent |
| 6393 | // lookup (defined as follows). If X contains |
| 6394 | // |
| 6395 | // -- a declaration of a class member, or |
| 6396 | // |
| 6397 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6398 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6399 | // |
| 6400 | // -- a declaration that is neither a function or a function |
| 6401 | // template |
| 6402 | // |
| 6403 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6404 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6405 | if (ULE->requiresADL()) { |
| 6406 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6407 | E = ULE->decls_end(); I != E; ++I) { |
| 6408 | assert(!(*I)->getDeclContext()->isRecord()); |
| 6409 | assert(isa<UsingShadowDecl>(*I) || |
| 6410 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 6411 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6412 | } |
| 6413 | } |
| 6414 | #endif |
| 6415 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6416 | // It would be nice to avoid this copy. |
| 6417 | TemplateArgumentListInfo TABuffer; |
| 6418 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6419 | if (ULE->hasExplicitTemplateArgs()) { |
| 6420 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6421 | ExplicitTemplateArgs = &TABuffer; |
| 6422 | } |
| 6423 | |
| 6424 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6425 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6426 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6427 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6428 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6429 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6430 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6431 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 6432 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6433 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6434 | CandidateSet, |
| 6435 | PartialOverloading); |
| 6436 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6437 | |
| 6438 | /// Attempts to recover from a call where no functions were found. |
| 6439 | /// |
| 6440 | /// Returns true if new candidates were found. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6441 | static Sema::OwningExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6442 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6443 | UnresolvedLookupExpr *ULE, |
| 6444 | SourceLocation LParenLoc, |
| 6445 | Expr **Args, unsigned NumArgs, |
| 6446 | SourceLocation *CommaLocs, |
| 6447 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6448 | |
| 6449 | CXXScopeSpec SS; |
| 6450 | if (ULE->getQualifier()) { |
| 6451 | SS.setScopeRep(ULE->getQualifier()); |
| 6452 | SS.setRange(ULE->getQualifierRange()); |
| 6453 | } |
| 6454 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6455 | TemplateArgumentListInfo TABuffer; |
| 6456 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6457 | if (ULE->hasExplicitTemplateArgs()) { |
| 6458 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6459 | ExplicitTemplateArgs = &TABuffer; |
| 6460 | } |
| 6461 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6462 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 6463 | Sema::LookupOrdinaryName); |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 6464 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6465 | return SemaRef.ExprError(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6466 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6467 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 6468 | |
| 6469 | // Build an implicit member call if appropriate. Just drop the |
| 6470 | // casts and such from the call, we don't really care. |
| 6471 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 6472 | if ((*R.begin())->isCXXClassMember()) |
| 6473 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 6474 | else if (ExplicitTemplateArgs) |
| 6475 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 6476 | else |
| 6477 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 6478 | |
| 6479 | if (NewFn.isInvalid()) |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6480 | return SemaRef.ExprError(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6481 | |
| 6482 | // This shouldn't cause an infinite loop because we're giving it |
| 6483 | // an expression with non-empty lookup results, which should never |
| 6484 | // end up here. |
| 6485 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 6486 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 6487 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6488 | } |
Douglas Gregor | d7a9597 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 6489 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6490 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6491 | /// (which eventually refers to the declaration Func) and the call |
| 6492 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 6493 | /// to a specific function. If overload resolution succeeds, returns |
| 6494 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 6495 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6496 | /// arguments and Fn, and returns NULL. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6497 | Sema::OwningExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6498 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6499 | SourceLocation LParenLoc, |
| 6500 | Expr **Args, unsigned NumArgs, |
| 6501 | SourceLocation *CommaLocs, |
| 6502 | SourceLocation RParenLoc) { |
| 6503 | #ifndef NDEBUG |
| 6504 | if (ULE->requiresADL()) { |
| 6505 | // To do ADL, we must have found an unqualified name. |
| 6506 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 6507 | |
| 6508 | // We don't perform ADL for implicit declarations of builtins. |
| 6509 | // Verify that this was correctly set up. |
| 6510 | FunctionDecl *F; |
| 6511 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 6512 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 6513 | F->getBuiltinID() && F->isImplicit()) |
| 6514 | assert(0 && "performing ADL for builtin"); |
| 6515 | |
| 6516 | // We don't perform ADL in C. |
| 6517 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 6518 | } |
| 6519 | #endif |
| 6520 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6521 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 6522 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6523 | // Add the functions denoted by the callee to the set of candidate |
| 6524 | // functions, including those from argument-dependent lookup. |
| 6525 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6526 | |
| 6527 | // If we found nothing, try to recover. |
| 6528 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 6529 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6530 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6531 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6532 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6533 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6534 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6535 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6536 | case OR_Success: { |
| 6537 | FunctionDecl *FDecl = Best->Function; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6538 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6539 | DiagnoseUseOfDecl(Best->FoundDecl, ULE->getNameLoc()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6540 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6541 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 6542 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6543 | |
| 6544 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 6545 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6546 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6547 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6548 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6549 | break; |
| 6550 | |
| 6551 | case OR_Ambiguous: |
| 6552 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6553 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6554 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6555 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6556 | |
| 6557 | case OR_Deleted: |
| 6558 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 6559 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6560 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6561 | << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6562 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6563 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6566 | // Overload resolution failed. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6567 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6570 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6571 | return Functions.size() > 1 || |
| 6572 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 6573 | } |
| 6574 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6575 | /// \brief Create a unary operation that may resolve to an overloaded |
| 6576 | /// operator. |
| 6577 | /// |
| 6578 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 6579 | /// |
| 6580 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 6581 | /// operator. |
| 6582 | /// |
| 6583 | /// \param Functions The set of non-member functions that will be |
| 6584 | /// considered by overload resolution. The caller needs to build this |
| 6585 | /// set based on the context using, e.g., |
| 6586 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6587 | /// set should not contain any member functions; those will be added |
| 6588 | /// by CreateOverloadedUnaryOp(). |
| 6589 | /// |
| 6590 | /// \param input The input argument. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6591 | Sema::OwningExprResult |
| 6592 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 6593 | const UnresolvedSetImpl &Fns, |
| 6594 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6595 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 6596 | Expr *Input = (Expr *)input.get(); |
| 6597 | |
| 6598 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 6599 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 6600 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6601 | |
| 6602 | Expr *Args[2] = { Input, 0 }; |
| 6603 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6604 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6605 | // For post-increment and post-decrement, add the implicit '0' as |
| 6606 | // the second argument, so that we know this is a post-increment or |
| 6607 | // post-decrement. |
| 6608 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 6609 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6610 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6611 | SourceLocation()); |
| 6612 | NumArgs = 2; |
| 6613 | } |
| 6614 | |
| 6615 | if (Input->isTypeDependent()) { |
Douglas Gregor | 1ec8ef7 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 6616 | if (Fns.empty()) |
| 6617 | return Owned(new (Context) UnaryOperator(input.takeAs<Expr>(), |
| 6618 | Opc, |
| 6619 | Context.DependentTy, |
| 6620 | OpLoc)); |
| 6621 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6622 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6623 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6624 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6625 | 0, SourceRange(), OpName, OpLoc, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6626 | /*ADL*/ true, IsOverloaded(Fns), |
| 6627 | Fns.begin(), Fns.end()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6628 | input.release(); |
| 6629 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 6630 | &Args[0], NumArgs, |
| 6631 | Context.DependentTy, |
| 6632 | OpLoc)); |
| 6633 | } |
| 6634 | |
| 6635 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6636 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6637 | |
| 6638 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6639 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6640 | |
| 6641 | // Add operator candidates that are member functions. |
| 6642 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 6643 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6644 | // Add candidates from ADL. |
| 6645 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 6646 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6647 | /*ExplicitTemplateArgs*/ 0, |
| 6648 | CandidateSet); |
| 6649 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6650 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6651 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6652 | |
| 6653 | // Perform overload resolution. |
| 6654 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6655 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6656 | case OR_Success: { |
| 6657 | // We found a built-in operator or an overloaded operator. |
| 6658 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6659 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6660 | if (FnDecl) { |
| 6661 | // We matched an overloaded operator. Build a call to that |
| 6662 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6663 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6664 | // Convert the arguments. |
| 6665 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6666 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6667 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6668 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 6669 | Best->FoundDecl, Method)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6670 | return ExprError(); |
| 6671 | } else { |
| 6672 | // Convert the arguments. |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6673 | OwningExprResult InputInit |
| 6674 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6675 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6676 | SourceLocation(), |
| 6677 | move(input)); |
| 6678 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6679 | return ExprError(); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6680 | |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6681 | input = move(InputInit); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6682 | Input = (Expr *)input.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6685 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6686 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6687 | // Determine the result type |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 6688 | QualType ResultTy = FnDecl->getCallResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6689 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6690 | // Build the actual expression node. |
| 6691 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6692 | SourceLocation()); |
| 6693 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6695 | input.release(); |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6696 | Args[0] = Input; |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6697 | ExprOwningPtr<CallExpr> TheCall(this, |
| 6698 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6699 | Args, NumArgs, ResultTy, OpLoc)); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6700 | |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6701 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6702 | FnDecl)) |
| 6703 | return ExprError(); |
| 6704 | |
| 6705 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6706 | } else { |
| 6707 | // We matched a built-in operator. Convert the arguments, then |
| 6708 | // break out so that we will build the appropriate built-in |
| 6709 | // operator node. |
| 6710 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6711 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6712 | return ExprError(); |
| 6713 | |
| 6714 | break; |
| 6715 | } |
| 6716 | } |
| 6717 | |
| 6718 | case OR_No_Viable_Function: |
| 6719 | // No viable function; fall through to handling this as a |
| 6720 | // built-in operator, which will produce an error message for us. |
| 6721 | break; |
| 6722 | |
| 6723 | case OR_Ambiguous: |
| 6724 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6725 | << UnaryOperator::getOpcodeStr(Opc) |
| 6726 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6727 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6728 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6729 | return ExprError(); |
| 6730 | |
| 6731 | case OR_Deleted: |
| 6732 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6733 | << Best->Function->isDeleted() |
| 6734 | << UnaryOperator::getOpcodeStr(Opc) |
| 6735 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6736 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6737 | return ExprError(); |
| 6738 | } |
| 6739 | |
| 6740 | // Either we found no viable overloaded operator or we matched a |
| 6741 | // built-in operator. In either case, fall through to trying to |
| 6742 | // build a built-in operation. |
| 6743 | input.release(); |
| 6744 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 6745 | } |
| 6746 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6747 | /// \brief Create a binary operation that may resolve to an overloaded |
| 6748 | /// operator. |
| 6749 | /// |
| 6750 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 6751 | /// |
| 6752 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 6753 | /// operator. |
| 6754 | /// |
| 6755 | /// \param Functions The set of non-member functions that will be |
| 6756 | /// considered by overload resolution. The caller needs to build this |
| 6757 | /// set based on the context using, e.g., |
| 6758 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6759 | /// set should not contain any member functions; those will be added |
| 6760 | /// by CreateOverloadedBinOp(). |
| 6761 | /// |
| 6762 | /// \param LHS Left-hand argument. |
| 6763 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6764 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6765 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6766 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6767 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6768 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6769 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6770 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6771 | |
| 6772 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 6773 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 6774 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6775 | |
| 6776 | // If either side is type-dependent, create an appropriate dependent |
| 6777 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6778 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6779 | if (Fns.empty()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6780 | // If there are no functions to store, just build a dependent |
| 6781 | // BinaryOperator or CompoundAssignment. |
| 6782 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 6783 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6784 | Context.DependentTy, OpLoc)); |
| 6785 | |
| 6786 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6787 | Context.DependentTy, |
| 6788 | Context.DependentTy, |
| 6789 | Context.DependentTy, |
| 6790 | OpLoc)); |
| 6791 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6792 | |
| 6793 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6794 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6795 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6796 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6797 | 0, SourceRange(), OpName, OpLoc, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6798 | /*ADL*/ true, IsOverloaded(Fns), |
| 6799 | Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6800 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6801 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6802 | Context.DependentTy, |
| 6803 | OpLoc)); |
| 6804 | } |
| 6805 | |
| 6806 | // If this is the .* operator, which is not overloadable, just |
| 6807 | // create a built-in binary operator. |
| 6808 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6809 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6810 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6811 | // If this is the assignment operator, we only perform overload resolution |
| 6812 | // if the left-hand side is a class or enumeration type. This is actually |
| 6813 | // a hack. The standard requires that we do overload resolution between the |
| 6814 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6815 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6816 | // Note that we go the traditional code path for compound assignment forms. |
| 6817 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6818 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6819 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6820 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6821 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6822 | |
| 6823 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6824 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6825 | |
| 6826 | // Add operator candidates that are member functions. |
| 6827 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6828 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6829 | // Add candidates from ADL. |
| 6830 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6831 | Args, 2, |
| 6832 | /*ExplicitTemplateArgs*/ 0, |
| 6833 | CandidateSet); |
| 6834 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6835 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6836 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6837 | |
| 6838 | // Perform overload resolution. |
| 6839 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6840 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6841 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6842 | // We found a built-in operator or an overloaded operator. |
| 6843 | FunctionDecl *FnDecl = Best->Function; |
| 6844 | |
| 6845 | if (FnDecl) { |
| 6846 | // We matched an overloaded operator. Build a call to that |
| 6847 | // operator. |
| 6848 | |
| 6849 | // Convert the arguments. |
| 6850 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6851 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6852 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6853 | |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6854 | OwningExprResult Arg1 |
| 6855 | = PerformCopyInitialization( |
| 6856 | InitializedEntity::InitializeParameter( |
| 6857 | FnDecl->getParamDecl(0)), |
| 6858 | SourceLocation(), |
| 6859 | Owned(Args[1])); |
| 6860 | if (Arg1.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 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6863 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6864 | Best->FoundDecl, Method)) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6865 | return ExprError(); |
| 6866 | |
| 6867 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6868 | } else { |
| 6869 | // Convert the arguments. |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6870 | OwningExprResult Arg0 |
| 6871 | = PerformCopyInitialization( |
| 6872 | InitializedEntity::InitializeParameter( |
| 6873 | FnDecl->getParamDecl(0)), |
| 6874 | SourceLocation(), |
| 6875 | Owned(Args[0])); |
| 6876 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6877 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6878 | |
| 6879 | OwningExprResult Arg1 |
| 6880 | = PerformCopyInitialization( |
| 6881 | InitializedEntity::InitializeParameter( |
| 6882 | FnDecl->getParamDecl(1)), |
| 6883 | SourceLocation(), |
| 6884 | Owned(Args[1])); |
| 6885 | if (Arg1.isInvalid()) |
| 6886 | return ExprError(); |
| 6887 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 6888 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6891 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6892 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6893 | // Determine the result type |
| 6894 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 6895 | = FnDecl->getType()->getAs<FunctionType>() |
| 6896 | ->getCallResultType(Context); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6897 | |
| 6898 | // Build the actual expression node. |
| 6899 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 6900 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6901 | UsualUnaryConversions(FnExpr); |
| 6902 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6903 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6904 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 6905 | Args, 2, ResultTy, |
| 6906 | OpLoc)); |
| 6907 | |
| 6908 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6909 | FnDecl)) |
| 6910 | return ExprError(); |
| 6911 | |
| 6912 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6913 | } else { |
| 6914 | // We matched a built-in operator. Convert the arguments, then |
| 6915 | // break out so that we will build the appropriate built-in |
| 6916 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6917 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6918 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6919 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6920 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6921 | return ExprError(); |
| 6922 | |
| 6923 | break; |
| 6924 | } |
| 6925 | } |
| 6926 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6927 | case OR_No_Viable_Function: { |
| 6928 | // C++ [over.match.oper]p9: |
| 6929 | // If the operator is the operator , [...] and there are no |
| 6930 | // viable functions, then the operator is assumed to be the |
| 6931 | // built-in operator and interpreted according to clause 5. |
| 6932 | if (Opc == BinaryOperator::Comma) |
| 6933 | break; |
| 6934 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6935 | // For class as left operand for assignment or compound assigment operator |
| 6936 | // do not fall through to handling in built-in, but report that no overloaded |
| 6937 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6938 | OwningExprResult Result = ExprError(); |
| 6939 | if (Args[0]->getType()->isRecordType() && |
| 6940 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6941 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6942 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6943 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6944 | } else { |
| 6945 | // No viable function; try to create a built-in operation, which will |
| 6946 | // produce an error. Then, show the non-viable candidates. |
| 6947 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6948 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6949 | assert(Result.isInvalid() && |
| 6950 | "C++ binary operator overloading is missing candidates!"); |
| 6951 | if (Result.isInvalid()) |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6952 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6953 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6954 | return move(Result); |
| 6955 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6956 | |
| 6957 | case OR_Ambiguous: |
| 6958 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6959 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6960 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6961 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6962 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6963 | return ExprError(); |
| 6964 | |
| 6965 | case OR_Deleted: |
| 6966 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6967 | << Best->Function->isDeleted() |
| 6968 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6969 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6970 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6971 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6972 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6974 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6975 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6976 | } |
| 6977 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6978 | Action::OwningExprResult |
| 6979 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 6980 | SourceLocation RLoc, |
| 6981 | ExprArg Base, ExprArg Idx) { |
| 6982 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 6983 | static_cast<Expr*>(Idx.get()) }; |
| 6984 | DeclarationName OpName = |
| 6985 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 6986 | |
| 6987 | // If either side is type-dependent, create an appropriate dependent |
| 6988 | // expression. |
| 6989 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 6990 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6991 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6992 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6993 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6994 | 0, SourceRange(), OpName, LLoc, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6995 | /*ADL*/ true, /*Overloaded*/ false, |
| 6996 | UnresolvedSetIterator(), |
| 6997 | UnresolvedSetIterator()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6998 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6999 | |
| 7000 | Base.release(); |
| 7001 | Idx.release(); |
| 7002 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 7003 | Args, 2, |
| 7004 | Context.DependentTy, |
| 7005 | RLoc)); |
| 7006 | } |
| 7007 | |
| 7008 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7009 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7010 | |
| 7011 | // Subscript can only be overloaded as a member function. |
| 7012 | |
| 7013 | // Add operator candidates that are member functions. |
| 7014 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7015 | |
| 7016 | // Add builtin operator candidates. |
| 7017 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 7018 | |
| 7019 | // Perform overload resolution. |
| 7020 | OverloadCandidateSet::iterator Best; |
| 7021 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 7022 | case OR_Success: { |
| 7023 | // We found a built-in operator or an overloaded operator. |
| 7024 | FunctionDecl *FnDecl = Best->Function; |
| 7025 | |
| 7026 | if (FnDecl) { |
| 7027 | // We matched an overloaded operator. Build a call to that |
| 7028 | // operator. |
| 7029 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7030 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7031 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7032 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7033 | // Convert the arguments. |
| 7034 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7035 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7036 | Best->FoundDecl, Method)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7037 | return ExprError(); |
| 7038 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 7039 | // Convert the arguments. |
| 7040 | OwningExprResult InputInit |
| 7041 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7042 | FnDecl->getParamDecl(0)), |
| 7043 | SourceLocation(), |
| 7044 | Owned(Args[1])); |
| 7045 | if (InputInit.isInvalid()) |
| 7046 | return ExprError(); |
| 7047 | |
| 7048 | Args[1] = InputInit.takeAs<Expr>(); |
| 7049 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7050 | // Determine the result type |
| 7051 | QualType ResultTy |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7052 | = FnDecl->getType()->getAs<FunctionType>() |
| 7053 | ->getCallResultType(Context); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7054 | |
| 7055 | // Build the actual expression node. |
| 7056 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 7057 | LLoc); |
| 7058 | UsualUnaryConversions(FnExpr); |
| 7059 | |
| 7060 | Base.release(); |
| 7061 | Idx.release(); |
| 7062 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7063 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 7064 | FnExpr, Args, 2, |
| 7065 | ResultTy, RLoc)); |
| 7066 | |
| 7067 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 7068 | FnDecl)) |
| 7069 | return ExprError(); |
| 7070 | |
| 7071 | return MaybeBindToTemporary(TheCall.release()); |
| 7072 | } else { |
| 7073 | // We matched a built-in operator. Convert the arguments, then |
| 7074 | // break out so that we will build the appropriate built-in |
| 7075 | // operator node. |
| 7076 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7077 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7078 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7079 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7080 | return ExprError(); |
| 7081 | |
| 7082 | break; |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7087 | if (CandidateSet.empty()) |
| 7088 | Diag(LLoc, diag::err_ovl_no_oper) |
| 7089 | << Args[0]->getType() << /*subscript*/ 0 |
| 7090 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 7091 | else |
| 7092 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 7093 | << Args[0]->getType() |
| 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 | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7096 | "[]", LLoc); |
| 7097 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7098 | } |
| 7099 | |
| 7100 | case OR_Ambiguous: |
| 7101 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 7102 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7103 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7104 | "[]", LLoc); |
| 7105 | return ExprError(); |
| 7106 | |
| 7107 | case OR_Deleted: |
| 7108 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 7109 | << Best->Function->isDeleted() << "[]" |
| 7110 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7111 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7112 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 7113 | return ExprError(); |
| 7114 | } |
| 7115 | |
| 7116 | // We matched a built-in operator; build it. |
| 7117 | Base.release(); |
| 7118 | Idx.release(); |
| 7119 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 7120 | Owned(Args[1]), RLoc); |
| 7121 | } |
| 7122 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7123 | /// BuildCallToMemberFunction - Build a call to a member |
| 7124 | /// function. MemExpr is the expression that refers to the member |
| 7125 | /// function (and includes the object parameter), Args/NumArgs are the |
| 7126 | /// arguments to the function call (not including the object |
| 7127 | /// parameter). The caller needs to validate that the member |
| 7128 | /// expression refers to a member function or an overloaded member |
| 7129 | /// function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7130 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7131 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 7132 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7133 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 7134 | SourceLocation RParenLoc) { |
| 7135 | // Dig out the member expression. This holds both the object |
| 7136 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7137 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 7138 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7139 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7140 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 7141 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7142 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7143 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 7144 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7145 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7146 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7147 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7148 | } else { |
| 7149 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7150 | Qualifier = UnresExpr->getQualifier(); |
| 7151 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7152 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7153 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7154 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7155 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7156 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7157 | // FIXME: avoid copy. |
| 7158 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7159 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 7160 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7161 | TemplateArgs = &TemplateArgsBuffer; |
| 7162 | } |
| 7163 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7164 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 7165 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 7166 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7167 | NamedDecl *Func = *I; |
| 7168 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 7169 | if (isa<UsingShadowDecl>(Func)) |
| 7170 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 7171 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7172 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7173 | // If explicit template arguments were provided, we can't call a |
| 7174 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7175 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 7176 | continue; |
| 7177 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7178 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7179 | Args, NumArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7180 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7181 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7182 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7183 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7184 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7185 | CandidateSet, |
| 7186 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7187 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 7188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7189 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7190 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 7191 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7192 | OverloadCandidateSet::iterator Best; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7193 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7194 | case OR_Success: |
| 7195 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7196 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7197 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7198 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7199 | break; |
| 7200 | |
| 7201 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7202 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7203 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7204 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7205 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7206 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7207 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7208 | |
| 7209 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7210 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7211 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7212 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7213 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7214 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7215 | |
| 7216 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7217 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7218 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 7219 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7220 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7221 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7222 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7223 | } |
| 7224 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7225 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7226 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7227 | // If overload resolution picked a static member, build a |
| 7228 | // non-member call based on that function. |
| 7229 | if (Method->isStatic()) { |
| 7230 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 7231 | Args, NumArgs, RParenLoc); |
| 7232 | } |
| 7233 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7234 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7235 | } |
| 7236 | |
| 7237 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7238 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7239 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7240 | NumArgs, |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7241 | Method->getCallResultType(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7242 | RParenLoc)); |
| 7243 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7244 | // Check for a valid return type. |
| 7245 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 7246 | TheCall.get(), Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7247 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 7248 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7249 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7250 | // We only need to do this if there was actually an overload; otherwise |
| 7251 | // it was done at lookup. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7252 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7253 | if (!Method->isStatic() && |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7254 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 7255 | FoundDecl, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7256 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7257 | MemExpr->setBase(ObjectArg); |
| 7258 | |
| 7259 | // Convert the rest of the arguments |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 7260 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7261 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7262 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7263 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7264 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7265 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7266 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 7267 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7268 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7269 | } |
| 7270 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7271 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 7272 | /// type (C++ [over.call.object]), which can end up invoking an |
| 7273 | /// overloaded function call operator (@c operator()) or performing a |
| 7274 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7275 | Sema::ExprResult |
| 7276 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 7277 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7278 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7279 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7280 | SourceLocation RParenLoc) { |
| 7281 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7282 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7283 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7284 | // C++ [over.call.object]p1: |
| 7285 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7286 | // 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] | 7287 | // candidate functions includes at least the function call |
| 7288 | // operators of T. The function call operators of T are obtained by |
| 7289 | // ordinary lookup of the name operator() in the context of |
| 7290 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7291 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7292 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7293 | |
| 7294 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7295 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7296 | << Object->getSourceRange())) |
| 7297 | return true; |
| 7298 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7299 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 7300 | LookupQualifiedName(R, Record->getDecl()); |
| 7301 | R.suppressDiagnostics(); |
| 7302 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7303 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7304 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7305 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7306 | Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 7307 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7308 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7309 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7310 | // C++ [over.call.object]p2: |
| 7311 | // In addition, for each conversion function declared in T of the |
| 7312 | // form |
| 7313 | // |
| 7314 | // operator conversion-type-id () cv-qualifier; |
| 7315 | // |
| 7316 | // where cv-qualifier is the same cv-qualification as, or a |
| 7317 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 7318 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 7319 | // R", or the type "reference to pointer to function of |
| 7320 | // (P1,...,Pn) returning R", or the type "reference to function |
| 7321 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7322 | // is also considered as a candidate function. Similarly, |
| 7323 | // surrogate call functions are added to the set of candidate |
| 7324 | // functions for each conversion function declared in an |
| 7325 | // accessible base class provided the function is not hidden |
| 7326 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7327 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 7328 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7329 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7330 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7331 | NamedDecl *D = *I; |
| 7332 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 7333 | if (isa<UsingShadowDecl>(D)) |
| 7334 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7335 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7336 | // Skip over templated conversion functions; they aren't |
| 7337 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7338 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7339 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7340 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7341 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7342 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7343 | // Strip the reference type (if any) and then the pointer type (if |
| 7344 | // any) to get down to what might be a function type. |
| 7345 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 7346 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7347 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7348 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7349 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7350 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7351 | Object->getType(), Args, NumArgs, |
| 7352 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7353 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7354 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7355 | // Perform overload resolution. |
| 7356 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7357 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7358 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7359 | // Overload resolution succeeded; we'll build the appropriate call |
| 7360 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7361 | break; |
| 7362 | |
| 7363 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7364 | if (CandidateSet.empty()) |
| 7365 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 7366 | << Object->getType() << /*call*/ 1 |
| 7367 | << Object->getSourceRange(); |
| 7368 | else |
| 7369 | Diag(Object->getSourceRange().getBegin(), |
| 7370 | diag::err_ovl_no_viable_object_call) |
| 7371 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7372 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7373 | break; |
| 7374 | |
| 7375 | case OR_Ambiguous: |
| 7376 | Diag(Object->getSourceRange().getBegin(), |
| 7377 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7378 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7379 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7380 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7381 | |
| 7382 | case OR_Deleted: |
| 7383 | Diag(Object->getSourceRange().getBegin(), |
| 7384 | diag::err_ovl_deleted_object_call) |
| 7385 | << Best->Function->isDeleted() |
| 7386 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7387 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7388 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7389 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7390 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7391 | if (Best == CandidateSet.end()) |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7392 | return true; |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7393 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7394 | if (Best->Function == 0) { |
| 7395 | // Since there is no function declaration, this is one of the |
| 7396 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7397 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7398 | = cast<CXXConversionDecl>( |
| 7399 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 7400 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7401 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7402 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7403 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7404 | // We selected one of the surrogate functions that converts the |
| 7405 | // object parameter to a function pointer. Perform the conversion |
| 7406 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7407 | |
| 7408 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7409 | // and then call it. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7410 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 7411 | Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7412 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7413 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7414 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
Douglas Gregor | aa0be17 | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7415 | CommaLocs, RParenLoc).result(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7418 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7419 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7420 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7421 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 7422 | // that calls this method, using Object for the implicit object |
| 7423 | // parameter and passing along the remaining arguments. |
| 7424 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7425 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7426 | |
| 7427 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7428 | unsigned NumArgsToCheck = NumArgs; |
| 7429 | |
| 7430 | // Build the full argument list for the method call (the |
| 7431 | // implicit object parameter is placed at the beginning of the |
| 7432 | // list). |
| 7433 | Expr **MethodArgs; |
| 7434 | if (NumArgs < NumArgsInProto) { |
| 7435 | NumArgsToCheck = NumArgsInProto; |
| 7436 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 7437 | } else { |
| 7438 | MethodArgs = new Expr*[NumArgs + 1]; |
| 7439 | } |
| 7440 | MethodArgs[0] = Object; |
| 7441 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7442 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7443 | |
| 7444 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7445 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7446 | UsualUnaryConversions(NewFn); |
| 7447 | |
| 7448 | // Once we've built TheCall, all of the expressions are properly |
| 7449 | // owned. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7450 | QualType ResultTy = Method->getCallResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7451 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7452 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7453 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7454 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7455 | delete [] MethodArgs; |
| 7456 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 7457 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 7458 | Method)) |
| 7459 | return true; |
| 7460 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7461 | // We may have default arguments. If so, we need to allocate more |
| 7462 | // slots in the call for them. |
| 7463 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7464 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7465 | else if (NumArgs > NumArgsInProto) |
| 7466 | NumArgsToCheck = NumArgsInProto; |
| 7467 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7468 | bool IsError = false; |
| 7469 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7470 | // Initialize the implicit object parameter. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7471 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7472 | Best->FoundDecl, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7473 | TheCall->setArg(0, Object); |
| 7474 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7475 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7476 | // Check the argument types. |
| 7477 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7478 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7479 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7480 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7481 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7482 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7483 | |
| 7484 | OwningExprResult InputInit |
| 7485 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7486 | Method->getParamDecl(i)), |
| 7487 | SourceLocation(), Owned(Arg)); |
| 7488 | |
| 7489 | IsError |= InputInit.isInvalid(); |
| 7490 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7491 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 7492 | OwningExprResult DefArg |
| 7493 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 7494 | if (DefArg.isInvalid()) { |
| 7495 | IsError = true; |
| 7496 | break; |
| 7497 | } |
| 7498 | |
| 7499 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7500 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7501 | |
| 7502 | TheCall->setArg(i + 1, Arg); |
| 7503 | } |
| 7504 | |
| 7505 | // If this is a variadic call, handle args passed through "...". |
| 7506 | if (Proto->isVariadic()) { |
| 7507 | // Promote the arguments (C99 6.5.2.2p7). |
| 7508 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 7509 | Expr *Arg = Args[i]; |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 7510 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod, 0); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7511 | TheCall->setArg(i + 1, Arg); |
| 7512 | } |
| 7513 | } |
| 7514 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7515 | if (IsError) return true; |
| 7516 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7517 | if (CheckFunctionCall(Method, TheCall.get())) |
| 7518 | return true; |
| 7519 | |
Douglas Gregor | aa0be17 | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7520 | return MaybeBindToTemporary(TheCall.release()).result(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7521 | } |
| 7522 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7523 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7524 | /// (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] | 7525 | /// @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] | 7526 | Sema::OwningExprResult |
| 7527 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 7528 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7529 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7530 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7531 | SourceLocation Loc = Base->getExprLoc(); |
| 7532 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7533 | // C++ [over.ref]p1: |
| 7534 | // |
| 7535 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 7536 | // for a class object x of type T if T::operator->() exists and if |
| 7537 | // the operator is selected as the best match function by the |
| 7538 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7539 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7540 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7541 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7542 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7543 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 7544 | PDiag(diag::err_typecheck_incomplete_tag) |
| 7545 | << Base->getSourceRange())) |
| 7546 | return ExprError(); |
| 7547 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7548 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7549 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 7550 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7551 | |
| 7552 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7553 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7554 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7555 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7556 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7557 | |
| 7558 | // Perform overload resolution. |
| 7559 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7560 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7561 | case OR_Success: |
| 7562 | // Overload resolution succeeded; we'll build the call below. |
| 7563 | break; |
| 7564 | |
| 7565 | case OR_No_Viable_Function: |
| 7566 | if (CandidateSet.empty()) |
| 7567 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7568 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7569 | else |
| 7570 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7571 | << "operator->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7572 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7573 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7574 | |
| 7575 | case OR_Ambiguous: |
| 7576 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7577 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7578 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7579 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7580 | |
| 7581 | case OR_Deleted: |
| 7582 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7583 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7584 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7585 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7586 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7587 | } |
| 7588 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7589 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7590 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7591 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7592 | // Convert the object parameter. |
| 7593 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7594 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 7595 | Best->FoundDecl, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7596 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 7597 | |
| 7598 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7599 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7600 | |
| 7601 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7602 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 7603 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7604 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7605 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 7606 | QualType ResultTy = Method->getCallResultType(); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7607 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7608 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 7609 | &Base, 1, ResultTy, OpLoc)); |
| 7610 | |
| 7611 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 7612 | Method)) |
| 7613 | return ExprError(); |
| 7614 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7615 | } |
| 7616 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7617 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 7618 | /// a C++ overloaded function (possibly with some parentheses and |
| 7619 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 7620 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7621 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7622 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7623 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7624 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7625 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 7626 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7627 | if (SubExpr == PE->getSubExpr()) |
| 7628 | return PE->Retain(); |
| 7629 | |
| 7630 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 7631 | } |
| 7632 | |
| 7633 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7634 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 7635 | Found, Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7636 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7637 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7638 | "Implicit cast type cannot be determined from overload"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame^] | 7639 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7640 | if (SubExpr == ICE->getSubExpr()) |
| 7641 | return ICE->Retain(); |
| 7642 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame^] | 7643 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
| 7644 | ICE->getCastKind(), |
| 7645 | SubExpr, 0, |
| 7646 | ICE->getCategory()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7650 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7651 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7652 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7653 | if (Method->isStatic()) { |
| 7654 | // Do nothing: static member functions aren't any different |
| 7655 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7656 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7657 | // Fix the sub expression, which really has to be an |
| 7658 | // UnresolvedLookupExpr holding an overloaded member function |
| 7659 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7660 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7661 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7662 | if (SubExpr == UnOp->getSubExpr()) |
| 7663 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7664 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7665 | assert(isa<DeclRefExpr>(SubExpr) |
| 7666 | && "fixed to something other than a decl ref"); |
| 7667 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 7668 | && "fixed to a member ref with no nested name qualifier"); |
| 7669 | |
| 7670 | // We have taken the address of a pointer to member |
| 7671 | // function. Perform the computation here so that we get the |
| 7672 | // appropriate pointer to member type. |
| 7673 | QualType ClassType |
| 7674 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 7675 | QualType MemPtrType |
| 7676 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 7677 | |
| 7678 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7679 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7680 | } |
| 7681 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7682 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7683 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7684 | if (SubExpr == UnOp->getSubExpr()) |
| 7685 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7686 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7687 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7688 | Context.getPointerType(SubExpr->getType()), |
| 7689 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7690 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7691 | |
| 7692 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7693 | // FIXME: avoid copy. |
| 7694 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7695 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7696 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7697 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7698 | } |
| 7699 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7700 | return DeclRefExpr::Create(Context, |
| 7701 | ULE->getQualifier(), |
| 7702 | ULE->getQualifierRange(), |
| 7703 | Fn, |
| 7704 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7705 | Fn->getType(), |
| 7706 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7707 | } |
| 7708 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7709 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7710 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7711 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7712 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 7713 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7714 | TemplateArgs = &TemplateArgsBuffer; |
| 7715 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7716 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7717 | Expr *Base; |
| 7718 | |
| 7719 | // If we're filling in |
| 7720 | if (MemExpr->isImplicitAccess()) { |
| 7721 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 7722 | return DeclRefExpr::Create(Context, |
| 7723 | MemExpr->getQualifier(), |
| 7724 | MemExpr->getQualifierRange(), |
| 7725 | Fn, |
| 7726 | MemExpr->getMemberLoc(), |
| 7727 | Fn->getType(), |
| 7728 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 7729 | } else { |
| 7730 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 7731 | if (MemExpr->getQualifier()) |
| 7732 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 7733 | Base = new (Context) CXXThisExpr(Loc, |
| 7734 | MemExpr->getBaseType(), |
| 7735 | /*isImplicit=*/true); |
| 7736 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7737 | } else |
| 7738 | Base = MemExpr->getBase()->Retain(); |
| 7739 | |
| 7740 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7741 | MemExpr->isArrow(), |
| 7742 | MemExpr->getQualifier(), |
| 7743 | MemExpr->getQualifierRange(), |
| 7744 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7745 | Found, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7746 | MemExpr->getMemberLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7747 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7748 | Fn->getType()); |
| 7749 | } |
| 7750 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7751 | assert(false && "Invalid reference to overloaded function"); |
| 7752 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7753 | } |
| 7754 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7755 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7756 | DeclAccessPair Found, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7757 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7758 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7759 | } |
| 7760 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7761 | } // end namespace clang |