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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 55 | ICC_Conversion |
| 56 | }; |
| 57 | return Category[(int)Kind]; |
| 58 | } |
| 59 | |
| 60 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 61 | /// corresponding to the given implicit conversion kind. |
| 62 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 63 | static const ImplicitConversionRank |
| 64 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 70 | ICR_Exact_Match, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Promotion, |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 73 | ICR_Promotion, |
| 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 81 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 82 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 83 | ICR_Conversion |
| 84 | }; |
| 85 | return Rank[(int)Kind]; |
| 86 | } |
| 87 | |
| 88 | /// GetImplicitConversionName - Return the name of this kind of |
| 89 | /// implicit conversion. |
| 90 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 91 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | "No conversion", |
| 93 | "Lvalue-to-rvalue", |
| 94 | "Array-to-pointer", |
| 95 | "Function-to-pointer", |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 96 | "Noreturn adjustment", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 97 | "Qualification", |
| 98 | "Integral promotion", |
| 99 | "Floating point promotion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Integral conversion", |
| 102 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 103 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 104 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 105 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | "Pointer conversion", |
| 107 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 108 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 109 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 110 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 111 | }; |
| 112 | return Name[Kind]; |
| 113 | } |
| 114 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 115 | /// StandardConversionSequence - Set the standard conversion |
| 116 | /// sequence to the identity conversion. |
| 117 | void StandardConversionSequence::setAsIdentityConversion() { |
| 118 | First = ICK_Identity; |
| 119 | Second = ICK_Identity; |
| 120 | Third = ICK_Identity; |
| 121 | Deprecated = false; |
| 122 | ReferenceBinding = false; |
| 123 | DirectBinding = false; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 124 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 125 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 128 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 129 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 130 | /// implicit conversions. |
| 131 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 132 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 133 | if (GetConversionRank(First) > Rank) |
| 134 | Rank = GetConversionRank(First); |
| 135 | if (GetConversionRank(Second) > Rank) |
| 136 | Rank = GetConversionRank(Second); |
| 137 | if (GetConversionRank(Third) > Rank) |
| 138 | Rank = GetConversionRank(Third); |
| 139 | return Rank; |
| 140 | } |
| 141 | |
| 142 | /// isPointerConversionToBool - Determines whether this conversion is |
| 143 | /// 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] | 144 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 145 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 151 | if (getToType()->isBooleanType() && |
| 152 | (getFromType()->isPointerType() || getFromType()->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 166 | QualType FromType = getFromType(); |
| 167 | QualType ToType = getToType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 175 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 185 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | bool PrintedSomething = false; |
| 187 | if (First != ICK_Identity) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 188 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 189 | PrintedSomething = true; |
| 190 | } |
| 191 | |
| 192 | if (Second != ICK_Identity) { |
| 193 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 194 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 195 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 196 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 197 | |
| 198 | if (CopyConstructor) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 199 | OS << " (by copy constructor)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 200 | } else if (DirectBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 201 | OS << " (direct reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 202 | } else if (ReferenceBinding) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 203 | OS << " (reference binding)"; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 204 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | PrintedSomething = true; |
| 206 | } |
| 207 | |
| 208 | if (Third != ICK_Identity) { |
| 209 | if (PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 210 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 212 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (!PrintedSomething) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 217 | OS << "No conversions required"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 222 | /// error. Useful for debugging overloading issues. |
| 223 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 224 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 225 | if (Before.First || Before.Second || Before.Third) { |
| 226 | Before.DebugPrint(); |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 227 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | } |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 229 | OS << "'" << ConversionFunction->getNameAsString() << "'"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 230 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 231 | OS << " -> "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | After.DebugPrint(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 237 | /// error. Useful for debugging overloading issues. |
| 238 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 239 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 240 | switch (ConversionKind) { |
| 241 | case StandardConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 242 | OS << "Standard conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 243 | Standard.DebugPrint(); |
| 244 | break; |
| 245 | case UserDefinedConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 246 | OS << "User-defined conversion: "; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 247 | UserDefined.DebugPrint(); |
| 248 | break; |
| 249 | case EllipsisConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 250 | OS << "Ellipsis conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 251 | break; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 252 | case AmbiguousConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 253 | OS << "Ambiguous conversion"; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 254 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 255 | case BadConversion: |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 256 | OS << "Bad conversion"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | |
Daniel Dunbar | f3f91f3 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 260 | OS << "\n"; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 261 | } |
| 262 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 263 | void AmbiguousConversionSequence::construct() { |
| 264 | new (&conversions()) ConversionSet(); |
| 265 | } |
| 266 | |
| 267 | void AmbiguousConversionSequence::destruct() { |
| 268 | conversions().~ConversionSet(); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 273 | FromTypePtr = O.FromTypePtr; |
| 274 | ToTypePtr = O.ToTypePtr; |
| 275 | new (&conversions()) ConversionSet(O.conversions()); |
| 276 | } |
| 277 | |
| 278 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 279 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 280 | // overload of the declarations in Old. This routine returns false if |
| 281 | // New and Old cannot be overloaded, e.g., if New has the same |
| 282 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 283 | // declarations aren't functions (or function templates) at all. When |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 284 | // it does return false, MatchedDecl will point to the decl that New |
| 285 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 286 | // top of the underlying declaration. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 287 | // |
| 288 | // Example: Given the following input: |
| 289 | // |
| 290 | // void f(int, float); // #1 |
| 291 | // void f(int, int); // #2 |
| 292 | // int f(int, int); // #3 |
| 293 | // |
| 294 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 296 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 297 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 298 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 299 | // (since they have different signatures), so this routine returns |
| 300 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 301 | // |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 302 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 303 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 304 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 305 | // identical (return types of functions are not part of the |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 306 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 307 | // point to the FunctionDecl for #2. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 308 | Sema::OverloadKind |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 309 | Sema::CheckOverload(FunctionDecl *New, const LookupResult &Old, |
| 310 | NamedDecl *&Match) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 311 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 312 | I != E; ++I) { |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 313 | NamedDecl *OldD = (*I)->getUnderlyingDecl(); |
| 314 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 315 | if (!IsOverload(New, OldT->getTemplatedDecl())) { |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 316 | Match = *I; |
| 317 | return Ovl_Match; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 318 | } |
John McCall | 51fa86f | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 319 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 320 | if (!IsOverload(New, OldF)) { |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 321 | Match = *I; |
| 322 | return Ovl_Match; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 323 | } |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 324 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 325 | // We can overload with these, which can show up when doing |
| 326 | // redeclaration checks for UsingDecls. |
| 327 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 328 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 329 | // Optimistically assume that an unresolved using decl will |
| 330 | // overload; if it doesn't, we'll have to diagnose during |
| 331 | // template instantiation. |
| 332 | } else { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 333 | // (C++ 13p1): |
| 334 | // Only function declarations can be overloaded; object and type |
| 335 | // declarations cannot be overloaded. |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 336 | Match = *I; |
| 337 | return Ovl_NonFunction; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 338 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 339 | } |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 340 | |
John McCall | 871b2e7 | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 341 | return Ovl_Overload; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old) { |
| 345 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 346 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 347 | |
| 348 | // C++ [temp.fct]p2: |
| 349 | // A function template can be overloaded with other function templates |
| 350 | // and with normal (non-template) functions. |
| 351 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 352 | return true; |
| 353 | |
| 354 | // Is the function New an overload of the function Old? |
| 355 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 356 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 357 | |
| 358 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 359 | // determine whether they are overloads. If we find any mismatch |
| 360 | // in the signature, they are overloads. |
| 361 | |
| 362 | // If either of these functions is a K&R-style function (no |
| 363 | // prototype), then we consider them to have matching signatures. |
| 364 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 365 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 366 | return false; |
| 367 | |
| 368 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 369 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 370 | |
| 371 | // The signature of a function includes the types of its |
| 372 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 373 | // of the ellipsis; see C++ DR 357). |
| 374 | if (OldQType != NewQType && |
| 375 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 376 | OldType->isVariadic() != NewType->isVariadic() || |
| 377 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 378 | NewType->arg_type_begin()))) |
| 379 | return true; |
| 380 | |
| 381 | // C++ [temp.over.link]p4: |
| 382 | // The signature of a function template consists of its function |
| 383 | // signature, its return type and its template parameter list. The names |
| 384 | // of the template parameters are significant only for establishing the |
| 385 | // relationship between the template parameters and the rest of the |
| 386 | // signature. |
| 387 | // |
| 388 | // We check the return type and template parameter lists for function |
| 389 | // templates first; the remaining checks follow. |
| 390 | if (NewTemplate && |
| 391 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 392 | OldTemplate->getTemplateParameters(), |
| 393 | false, TPL_TemplateMatch) || |
| 394 | OldType->getResultType() != NewType->getResultType())) |
| 395 | return true; |
| 396 | |
| 397 | // If the function is a class member, its signature includes the |
| 398 | // cv-qualifiers (if any) on the function itself. |
| 399 | // |
| 400 | // As part of this, also check whether one of the member functions |
| 401 | // is static, in which case they are not overloads (C++ |
| 402 | // 13.1p2). While not part of the definition of the signature, |
| 403 | // this check is important to determine whether these functions |
| 404 | // can be overloaded. |
| 405 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 406 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 407 | if (OldMethod && NewMethod && |
| 408 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 409 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 410 | return true; |
| 411 | |
| 412 | // The signatures match; this is not an overload. |
| 413 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 416 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 417 | /// from the given expression (Expr) to the given type (ToType). This |
| 418 | /// function returns an implicit conversion sequence that can be used |
| 419 | /// to perform the initialization. Given |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 420 | /// |
| 421 | /// void f(float f); |
| 422 | /// void g(int i) { f(i); } |
| 423 | /// |
| 424 | /// this routine would produce an implicit conversion sequence to |
| 425 | /// describe the initialization of f from i, which will be a standard |
| 426 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 427 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 428 | // |
| 429 | /// Note that this routine only determines how the conversion can be |
| 430 | /// performed; it does not actually perform the conversion. As such, |
| 431 | /// it will not produce any diagnostics if no conversion is available, |
| 432 | /// but will instead return an implicit conversion sequence of kind |
| 433 | /// "BadConversion". |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 434 | /// |
| 435 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 436 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 437 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 438 | /// permitted. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 439 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 440 | /// no matter its actual lvalueness. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 441 | /// If @p UserCast, the implicit conversion is being done for a user-specified |
| 442 | /// cast. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 443 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 444 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 445 | bool SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 446 | bool AllowExplicit, bool ForceRValue, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 447 | bool InOverloadResolution, |
| 448 | bool UserCast) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 449 | ImplicitConversionSequence ICS; |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 450 | OverloadCandidateSet Conversions; |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 451 | OverloadingResult UserDefResult = OR_Success; |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 452 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 453 | ICS.setStandard(); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 454 | else if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 455 | (UserDefResult = IsUserDefinedConversion(From, ToType, |
| 456 | ICS.UserDefined, |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 457 | Conversions, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 458 | !SuppressUserConversions, AllowExplicit, |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 459 | ForceRValue, UserCast)) == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 460 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 461 | // C++ [over.ics.user]p4: |
| 462 | // A conversion of an expression of class type to the same class |
| 463 | // type is given Exact Match rank, and a conversion of an |
| 464 | // expression of class type to a base class of that type is |
| 465 | // given Conversion rank, in spite of the fact that a copy |
| 466 | // constructor (i.e., a user-defined conversion function) is |
| 467 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 469 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 471 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 472 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 473 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 0d6d12b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 474 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 475 | // Turn this into a "standard" conversion sequence, so that it |
| 476 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 477 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 478 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 479 | ICS.Standard.setFromType(From->getType()); |
| 480 | ICS.Standard.setToType(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 481 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 482 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 483 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 484 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 485 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 486 | |
| 487 | // C++ [over.best.ics]p4: |
| 488 | // However, when considering the argument of a user-defined |
| 489 | // conversion function that is a candidate by 13.3.1.3 when |
| 490 | // invoked for the copying of the temporary in the second step |
| 491 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 492 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 493 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 494 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 495 | ICS.setBad(); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 496 | ICS.Bad.init(BadConversionSequence::suppressed_user, From, ToType); |
| 497 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 498 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 499 | ICS.setAmbiguous(); |
| 500 | ICS.Ambiguous.setFromType(From->getType()); |
| 501 | ICS.Ambiguous.setToType(ToType); |
| 502 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 503 | Cand != Conversions.end(); ++Cand) |
| 504 | if (Cand->Viable) |
| 505 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 506 | } else { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 507 | ICS.setBad(); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 508 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 509 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 510 | |
| 511 | return ICS; |
| 512 | } |
| 513 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 514 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 515 | /// conversion that strips "noreturn" off the nested function type. |
| 516 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 517 | QualType ToType, QualType &ResultTy) { |
| 518 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 519 | return false; |
| 520 | |
| 521 | // Strip the noreturn off the type we're converting from; noreturn can |
| 522 | // safely be removed. |
| 523 | FromType = Context.getNoReturnType(FromType, false); |
| 524 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 525 | return false; |
| 526 | |
| 527 | ResultTy = FromType; |
| 528 | return true; |
| 529 | } |
| 530 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 531 | /// IsStandardConversion - Determines whether there is a standard |
| 532 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 533 | /// expression From to the type ToType. Standard conversion sequences |
| 534 | /// only consider non-class types; for conversions that involve class |
| 535 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 536 | /// contain the standard conversion sequence required to perform this |
| 537 | /// conversion and this routine will return true. Otherwise, this |
| 538 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | bool |
| 540 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 541 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 543 | QualType FromType = From->getType(); |
| 544 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 545 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 546 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 547 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 548 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 549 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 550 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 551 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 552 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 554 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 555 | if (getLangOptions().CPlusPlus) |
| 556 | return false; |
| 557 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 561 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 562 | // array-to-pointer conversion, or function-to-pointer conversion |
| 563 | // (C++ 4p1). |
| 564 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 566 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 567 | // converted to an rvalue. |
| 568 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 570 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 571 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 572 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 573 | |
| 574 | // If T is a non-class type, the type of the rvalue is the |
| 575 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 576 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 577 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 578 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 579 | } else if (FromType->isArrayType()) { |
| 580 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 581 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 582 | |
| 583 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 584 | // bound of T" can be converted to an rvalue of type "pointer to |
| 585 | // T" (C++ 4.2p1). |
| 586 | FromType = Context.getArrayDecayedType(FromType); |
| 587 | |
| 588 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 589 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 590 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 591 | |
| 592 | // For the purpose of ranking in overload resolution |
| 593 | // (13.3.3.1.1), this conversion is considered an |
| 594 | // array-to-pointer conversion followed by a qualification |
| 595 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 596 | SCS.Second = ICK_Identity; |
| 597 | SCS.Third = ICK_Qualification; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 598 | SCS.setToType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 599 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 600 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 601 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 602 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 603 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 604 | |
| 605 | // An lvalue of function type T can be converted to an rvalue of |
| 606 | // type "pointer to T." The result is a pointer to the |
| 607 | // function. (C++ 4.3p1). |
| 608 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | } else if (FunctionDecl *Fn |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 610 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 611 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 612 | SCS.First = ICK_Function_To_Pointer; |
| 613 | |
| 614 | // We were able to resolve the address of the overloaded function, |
| 615 | // so we can convert to the type of that function. |
| 616 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 617 | if (ToType->isLValueReferenceType()) |
| 618 | FromType = Context.getLValueReferenceType(FromType); |
| 619 | else if (ToType->isRValueReferenceType()) |
| 620 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 621 | else if (ToType->isMemberPointerType()) { |
| 622 | // Resolve address only succeeds if both sides are member pointers, |
| 623 | // but it doesn't have to be the same class. See DR 247. |
| 624 | // Note that this means that the type of &Derived::fn can be |
| 625 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 626 | // base class, even if it was brought into the derived class via a |
| 627 | // using declaration. The standard isn't clear on this issue at all. |
| 628 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 629 | FromType = Context.getMemberPointerType(FromType, |
| 630 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 631 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 632 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 633 | } else { |
| 634 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 635 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | // The second conversion can be an integral promotion, floating |
| 639 | // point promotion, integral conversion, floating point conversion, |
| 640 | // floating-integral conversion, pointer conversion, |
| 641 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 642 | // For overloading in C, this can also be a "compatible-type" |
| 643 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 644 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 645 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 646 | // The unqualified versions of the types are the same: there's no |
| 647 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 648 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 649 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 651 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 652 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 653 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 654 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 655 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 656 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 657 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 658 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 659 | SCS.Second = ICK_Complex_Promotion; |
| 660 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 661 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 662 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 663 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 664 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 665 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 666 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 667 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 668 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 669 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 670 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 671 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 672 | SCS.Second = ICK_Complex_Conversion; |
| 673 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 674 | } else if ((FromType->isFloatingType() && |
| 675 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 676 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 678 | ToType->isFloatingType())) { |
| 679 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 680 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 681 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 682 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 683 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 684 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 685 | SCS.Second = ICK_Complex_Real; |
| 686 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 687 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 688 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 689 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 690 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 691 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 692 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 693 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 694 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 695 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 696 | } else if (ToType->isBooleanType() && |
| 697 | (FromType->isArithmeticType() || |
| 698 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 699 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 700 | FromType->isBlockPointerType() || |
| 701 | FromType->isMemberPointerType() || |
| 702 | FromType->isNullPtrType())) { |
| 703 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 704 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 705 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 707 | Context.typesAreCompatible(ToType, FromType)) { |
| 708 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 709 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 710 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 711 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 712 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 713 | } else { |
| 714 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 715 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 718 | QualType CanonFrom; |
| 719 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 720 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 721 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 722 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 723 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 724 | CanonFrom = Context.getCanonicalType(FromType); |
| 725 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 726 | } else { |
| 727 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 728 | SCS.Third = ICK_Identity; |
| 729 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 731 | // [...] Any difference in top-level cv-qualification is |
| 732 | // subsumed by the initialization itself and does not constitute |
| 733 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 734 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 736 | if (CanonFrom.getLocalUnqualifiedType() |
| 737 | == CanonTo.getLocalUnqualifiedType() && |
| 738 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 739 | FromType = ToType; |
| 740 | CanonFrom = CanonTo; |
| 741 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // If we have not converted the argument type to the parameter type, |
| 745 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 746 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 747 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 748 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 749 | SCS.setToType(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 750 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 754 | /// expression From (whose potentially-adjusted type is FromType) to |
| 755 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 756 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 758 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 759 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 760 | if (!To) { |
| 761 | return false; |
| 762 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 763 | |
| 764 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 765 | // unsigned short int can be converted to an rvalue of type int if |
| 766 | // int can represent all the values of the source type; otherwise, |
| 767 | // the source rvalue can be converted to an rvalue of type unsigned |
| 768 | // int (C++ 4.5p1). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 769 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 770 | if (// We can promote any signed, promotable integer type to an int |
| 771 | (FromType->isSignedIntegerType() || |
| 772 | // We can promote any unsigned integer type whose size is |
| 773 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 775 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 776 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 779 | return To->getKind() == BuiltinType::UInt; |
| 780 | } |
| 781 | |
| 782 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 783 | // can be converted to an rvalue of the first of the following types |
| 784 | // that can represent all the values of its underlying type: int, |
| 785 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 786 | |
| 787 | // We pre-calculate the promotion type for enum types. |
| 788 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 789 | if (ToType->isIntegerType()) |
| 790 | return Context.hasSameUnqualifiedType(ToType, |
| 791 | FromEnumType->getDecl()->getPromotionType()); |
| 792 | |
| 793 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 794 | // Determine whether the type we're converting from is signed or |
| 795 | // unsigned. |
| 796 | bool FromIsSigned; |
| 797 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 798 | |
| 799 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 800 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 801 | |
| 802 | // The types we'll try to promote to, in the appropriate |
| 803 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | QualType PromoteTypes[6] = { |
| 805 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 806 | Context.LongTy, Context.UnsignedLongTy , |
| 807 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 808 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 809 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 810 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 811 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 813 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 814 | // We found the type that we can promote to. If this is the |
| 815 | // type we wanted, we have a promotion. Otherwise, no |
| 816 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 817 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 823 | // rvalue of type int if int can represent all the values of the |
| 824 | // bit-field; otherwise, it can be converted to unsigned int if |
| 825 | // unsigned int can represent all the values of the bit-field. If |
| 826 | // the bit-field is larger yet, no integral promotion applies to |
| 827 | // it. If the bit-field has an enumerated type, it is treated as any |
| 828 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 829 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 830 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 831 | using llvm::APSInt; |
| 832 | if (From) |
| 833 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 834 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 835 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 836 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 837 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 838 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 840 | // Are we promoting to an int from a bitfield that fits in an int? |
| 841 | if (BitWidth < ToSize || |
| 842 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 843 | return To->getKind() == BuiltinType::Int; |
| 844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 846 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 847 | // that fits into an unsigned int? |
| 848 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 849 | return To->getKind() == BuiltinType::UInt; |
| 850 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 852 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 853 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 854 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 856 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 857 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 858 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 859 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 860 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 861 | |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 866 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 867 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 869 | /// An rvalue of type float can be converted to an rvalue of type |
| 870 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 871 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 872 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 873 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 874 | ToBuiltin->getKind() == BuiltinType::Double) |
| 875 | return true; |
| 876 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 877 | // C99 6.3.1.5p1: |
| 878 | // When a float is promoted to double or long double, or a |
| 879 | // double is promoted to long double [...]. |
| 880 | if (!getLangOptions().CPlusPlus && |
| 881 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 882 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 883 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 884 | return true; |
| 885 | } |
| 886 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 887 | return false; |
| 888 | } |
| 889 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 890 | /// \brief Determine if a conversion is a complex promotion. |
| 891 | /// |
| 892 | /// A complex promotion is defined as a complex -> complex conversion |
| 893 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 894 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 895 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 896 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 897 | if (!FromComplex) |
| 898 | return false; |
| 899 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 900 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 901 | if (!ToComplex) |
| 902 | return false; |
| 903 | |
| 904 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 905 | ToComplex->getElementType()) || |
| 906 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 907 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 910 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 911 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 912 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 913 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 914 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | static QualType |
| 916 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 917 | QualType ToPointee, QualType ToType, |
| 918 | ASTContext &Context) { |
| 919 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 920 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 921 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
| 923 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 924 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 925 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 926 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 927 | return ToType; |
| 928 | |
| 929 | // Build a pointer to ToPointee. It has the right qualifiers |
| 930 | // already. |
| 931 | return Context.getPointerType(ToPointee); |
| 932 | } |
| 933 | |
| 934 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 935 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 936 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 937 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 940 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 941 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 942 | /// not have the right set of qualifiers. |
| 943 | static QualType |
| 944 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 945 | QualType ToType, |
| 946 | ASTContext &Context) { |
| 947 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 948 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 949 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 950 | |
| 951 | // Exact qualifier match -> return the pointer type we're converting to. |
| 952 | if (CanonToType.getLocalQualifiers() == Quals) |
| 953 | return ToType; |
| 954 | |
| 955 | // Just build a canonical type that has the right qualifiers. |
| 956 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 957 | } |
| 958 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 960 | bool InOverloadResolution, |
| 961 | ASTContext &Context) { |
| 962 | // Handle value-dependent integral null pointer constants correctly. |
| 963 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 964 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 965 | Expr->getType()->isIntegralType()) |
| 966 | return !InOverloadResolution; |
| 967 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 968 | return Expr->isNullPointerConstant(Context, |
| 969 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 970 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 973 | /// IsPointerConversion - Determines whether the conversion of the |
| 974 | /// expression From, which has the (possibly adjusted) type FromType, |
| 975 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 976 | /// 4.10). If so, returns true and places the converted type (that |
| 977 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 978 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 979 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 980 | /// This routine also supports conversions to and from block pointers |
| 981 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 982 | /// pointers to interfaces. FIXME: Once we've determined the |
| 983 | /// appropriate overloading rules for Objective-C, we may want to |
| 984 | /// split the Objective-C checks into a different routine; however, |
| 985 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 986 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 987 | /// set if the conversion is an allowed Objective-C conversion that |
| 988 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 989 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 990 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 991 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 993 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 994 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 995 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 996 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 998 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 999 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1000 | ConvertedType = ToType; |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1004 | // Blocks: Block pointers can be converted to void*. |
| 1005 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1006 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1007 | ConvertedType = ToType; |
| 1008 | return true; |
| 1009 | } |
| 1010 | // Blocks: A null pointer constant can be converted to a block |
| 1011 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1013 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1014 | ConvertedType = ToType; |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1018 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1019 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1021 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1022 | ConvertedType = ToType; |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1026 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1027 | if (!ToTypePtr) |
| 1028 | return false; |
| 1029 | |
| 1030 | // 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] | 1031 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1032 | ConvertedType = ToType; |
| 1033 | return true; |
| 1034 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1035 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1036 | // Beyond this point, both types need to be pointers |
| 1037 | // , including objective-c pointers. |
| 1038 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1039 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1040 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1041 | ToType, Context); |
| 1042 | return true; |
| 1043 | |
| 1044 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1045 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1046 | if (!FromTypePtr) |
| 1047 | return false; |
| 1048 | |
| 1049 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1051 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1052 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1053 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1054 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1056 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1057 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1058 | return true; |
| 1059 | } |
| 1060 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1061 | // When we're overloading in C, we allow a special kind of pointer |
| 1062 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1064 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1066 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1068 | return true; |
| 1069 | } |
| 1070 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1071 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1073 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1074 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1075 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1076 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1077 | // necessitates this conversion is ill-formed. The result of the |
| 1078 | // conversion is a pointer to the base class sub-object of the |
| 1079 | // derived class object. The null pointer value is converted to |
| 1080 | // the null pointer value of the destination type. |
| 1081 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1082 | // Note that we do not check for ambiguity or inaccessibility |
| 1083 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1084 | if (getLangOptions().CPlusPlus && |
| 1085 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1086 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1087 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1089 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1090 | ToType, Context); |
| 1091 | return true; |
| 1092 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1094 | return false; |
| 1095 | } |
| 1096 | |
| 1097 | /// isObjCPointerConversion - Determines whether this is an |
| 1098 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1099 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1101 | QualType& ConvertedType, |
| 1102 | bool &IncompatibleObjC) { |
| 1103 | if (!getLangOptions().ObjC1) |
| 1104 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1105 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1106 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1107 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1109 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1110 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1111 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1112 | // 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] | 1113 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1114 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1115 | ConvertedType = ToType; |
| 1116 | return true; |
| 1117 | } |
| 1118 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1120 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1122 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1123 | ConvertedType = ToType; |
| 1124 | return true; |
| 1125 | } |
| 1126 | // Objective C++: We're able to convert from a pointer to an |
| 1127 | // interface to a pointer to a different interface. |
| 1128 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1129 | ConvertedType = ToType; |
| 1130 | return true; |
| 1131 | } |
| 1132 | |
| 1133 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1134 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1135 | // interfaces, which is permitted. However, we're going to |
| 1136 | // complain about it. |
| 1137 | IncompatibleObjC = true; |
| 1138 | ConvertedType = FromType; |
| 1139 | return true; |
| 1140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1142 | // 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] | 1143 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1144 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1145 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1146 | else if (const BlockPointerType *ToBlockPtr = |
| 1147 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1148 | // 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] | 1149 | // to a block pointer type. |
| 1150 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1151 | ConvertedType = ToType; |
| 1152 | return true; |
| 1153 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1154 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1155 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1156 | else if (FromType->getAs<BlockPointerType>() && |
| 1157 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1158 | // 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] | 1159 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1160 | ConvertedType = ToType; |
| 1161 | return true; |
| 1162 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1163 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1164 | return false; |
| 1165 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1166 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1167 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1168 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1169 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1170 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1171 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1174 | // If we have pointers to pointers, recursively check whether this |
| 1175 | // is an Objective-C conversion. |
| 1176 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1177 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1178 | IncompatibleObjC)) { |
| 1179 | // We always complain about this conversion. |
| 1180 | IncompatibleObjC = true; |
| 1181 | ConvertedType = ToType; |
| 1182 | return true; |
| 1183 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1184 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1185 | // as in I* to id. |
| 1186 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1187 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1188 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1189 | IncompatibleObjC)) { |
| 1190 | ConvertedType = ToType; |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1194 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1195 | // differences in the argument and result types are in Objective-C |
| 1196 | // pointer conversions. If so, we permit the conversion (but |
| 1197 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1199 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1200 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1201 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1202 | if (FromFunctionType && ToFunctionType) { |
| 1203 | // If the function types are exactly the same, this isn't an |
| 1204 | // Objective-C pointer conversion. |
| 1205 | if (Context.getCanonicalType(FromPointeeType) |
| 1206 | == Context.getCanonicalType(ToPointeeType)) |
| 1207 | return false; |
| 1208 | |
| 1209 | // Perform the quick checks that will tell us whether these |
| 1210 | // function types are obviously different. |
| 1211 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1212 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1213 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1214 | return false; |
| 1215 | |
| 1216 | bool HasObjCConversion = false; |
| 1217 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1218 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1219 | // Okay, the types match exactly. Nothing to do. |
| 1220 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1221 | ToFunctionType->getResultType(), |
| 1222 | ConvertedType, IncompatibleObjC)) { |
| 1223 | // Okay, we have an Objective-C pointer conversion. |
| 1224 | HasObjCConversion = true; |
| 1225 | } else { |
| 1226 | // Function types are too different. Abort. |
| 1227 | return false; |
| 1228 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1230 | // Check argument types. |
| 1231 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1232 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1233 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1234 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1235 | if (Context.getCanonicalType(FromArgType) |
| 1236 | == Context.getCanonicalType(ToArgType)) { |
| 1237 | // Okay, the types match exactly. Nothing to do. |
| 1238 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1239 | ConvertedType, IncompatibleObjC)) { |
| 1240 | // Okay, we have an Objective-C pointer conversion. |
| 1241 | HasObjCConversion = true; |
| 1242 | } else { |
| 1243 | // Argument types are too different. Abort. |
| 1244 | return false; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | if (HasObjCConversion) { |
| 1249 | // We had an Objective-C conversion. Allow this pointer |
| 1250 | // conversion, but complain about it. |
| 1251 | ConvertedType = ToType; |
| 1252 | IncompatibleObjC = true; |
| 1253 | return true; |
| 1254 | } |
| 1255 | } |
| 1256 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1257 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1260 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1261 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1262 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1263 | /// conversions for which IsPointerConversion has already returned |
| 1264 | /// true. It returns true and produces a diagnostic if there was an |
| 1265 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1266 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1267 | CastExpr::CastKind &Kind, |
| 1268 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1269 | QualType FromType = From->getType(); |
| 1270 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1271 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1272 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1273 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1274 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1276 | if (FromPointeeType->isRecordType() && |
| 1277 | ToPointeeType->isRecordType()) { |
| 1278 | // We must have a derived-to-base conversion. Check an |
| 1279 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1280 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1281 | From->getExprLoc(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1282 | From->getSourceRange(), |
| 1283 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1284 | return true; |
| 1285 | |
| 1286 | // The conversion was successful. |
| 1287 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1291 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1293 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1294 | // Objective-C++ conversions are always okay. |
| 1295 | // FIXME: We should have a different class of conversions for the |
| 1296 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1297 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1298 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1299 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1300 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
| 1303 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1304 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1305 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1306 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1307 | /// If so, returns true and places the converted type (that might differ from |
| 1308 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1309 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1310 | QualType ToType, |
| 1311 | bool InOverloadResolution, |
| 1312 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1313 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1314 | if (!ToTypePtr) |
| 1315 | return false; |
| 1316 | |
| 1317 | // 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] | 1318 | if (From->isNullPointerConstant(Context, |
| 1319 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1320 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1321 | ConvertedType = ToType; |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1326 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1327 | if (!FromTypePtr) |
| 1328 | return false; |
| 1329 | |
| 1330 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1331 | // where D is derived from B (C++ 4.11p2). |
| 1332 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1333 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1334 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1335 | |
| 1336 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1337 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1338 | ToClass.getTypePtr()); |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
| 1342 | return false; |
| 1343 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1344 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1345 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1346 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1347 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1348 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1349 | /// true and produces a diagnostic if there was an error, or returns false |
| 1350 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1352 | CastExpr::CastKind &Kind, |
| 1353 | bool IgnoreBaseAccess) { |
| 1354 | (void)IgnoreBaseAccess; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1355 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1356 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1357 | if (!FromPtrType) { |
| 1358 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1359 | assert(From->isNullPointerConstant(Context, |
| 1360 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1361 | "Expr must be null pointer constant!"); |
| 1362 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1363 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1364 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1365 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1366 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1367 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1368 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1369 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1370 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1371 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1372 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1373 | // FIXME: What about dependent types? |
| 1374 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1375 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1377 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1378 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1379 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1380 | assert(DerivationOkay && |
| 1381 | "Should not have been called if derivation isn't OK."); |
| 1382 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1383 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1384 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1385 | getUnqualifiedType())) { |
| 1386 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1387 | Paths.clear(); |
| 1388 | Paths.setRecordingPaths(true); |
| 1389 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1390 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1391 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1392 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1393 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1394 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1395 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1396 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1397 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1398 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1399 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1400 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1401 | << FromClass << ToClass << QualType(VBase, 0) |
| 1402 | << From->getSourceRange(); |
| 1403 | return true; |
| 1404 | } |
| 1405 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1406 | // Must be a base to derived member conversion. |
| 1407 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1408 | return false; |
| 1409 | } |
| 1410 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1411 | /// IsQualificationConversion - Determines whether the conversion from |
| 1412 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1413 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | bool |
| 1415 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1416 | FromType = Context.getCanonicalType(FromType); |
| 1417 | ToType = Context.getCanonicalType(ToType); |
| 1418 | |
| 1419 | // If FromType and ToType are the same type, this is not a |
| 1420 | // qualification conversion. |
| 1421 | if (FromType == ToType) |
| 1422 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1424 | // (C++ 4.4p4): |
| 1425 | // A conversion can add cv-qualifiers at levels other than the first |
| 1426 | // in multi-level pointers, subject to the following rules: [...] |
| 1427 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1428 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1429 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1430 | // Within each iteration of the loop, we check the qualifiers to |
| 1431 | // determine if this still looks like a qualification |
| 1432 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1433 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1434 | // until there are no more pointers or pointers-to-members left to |
| 1435 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1436 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1437 | |
| 1438 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1439 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1440 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1441 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1443 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1444 | // every cv for 0 < k < j. |
| 1445 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1446 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1447 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1449 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1450 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1452 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1453 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1454 | |
| 1455 | // We are left with FromType and ToType being the pointee types |
| 1456 | // after unwrapping the original FromType and ToType the same number |
| 1457 | // of types. If we unwrapped any pointers, and if FromType and |
| 1458 | // ToType have the same unqualified type (since we checked |
| 1459 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1460 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1463 | /// Determines whether there is a user-defined conversion sequence |
| 1464 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1465 | /// ToType. If such a conversion exists, User will contain the |
| 1466 | /// user-defined conversion sequence that performs such a conversion |
| 1467 | /// and this routine will return true. Otherwise, this routine returns |
| 1468 | /// false and User is unspecified. |
| 1469 | /// |
| 1470 | /// \param AllowConversionFunctions true if the conversion should |
| 1471 | /// consider conversion functions at all. If false, only constructors |
| 1472 | /// will be considered. |
| 1473 | /// |
| 1474 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1475 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1476 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1477 | /// |
| 1478 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1479 | /// for overload resolution. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1480 | /// \param UserCast true if looking for user defined conversion for a static |
| 1481 | /// cast. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1482 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1483 | UserDefinedConversionSequence& User, |
| 1484 | OverloadCandidateSet& CandidateSet, |
| 1485 | bool AllowConversionFunctions, |
| 1486 | bool AllowExplicit, |
| 1487 | bool ForceRValue, |
| 1488 | bool UserCast) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1489 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1490 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1491 | // We're not going to find any constructors. |
| 1492 | } else if (CXXRecordDecl *ToRecordDecl |
| 1493 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1494 | // C++ [over.match.ctor]p1: |
| 1495 | // When objects of class type are direct-initialized (8.5), or |
| 1496 | // copy-initialized from an expression of the same or a |
| 1497 | // derived class type (8.5), overload resolution selects the |
| 1498 | // constructor. [...] For copy-initialization, the candidate |
| 1499 | // functions are all the converting constructors (12.3.1) of |
| 1500 | // that class. The argument list is the expression-list within |
| 1501 | // the parentheses of the initializer. |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1502 | bool SuppressUserConversions = !UserCast; |
| 1503 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1504 | IsDerivedFrom(From->getType(), ToType)) { |
| 1505 | SuppressUserConversions = false; |
| 1506 | AllowConversionFunctions = false; |
| 1507 | } |
| 1508 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1510 | = Context.DeclarationNames.getCXXConstructorName( |
| 1511 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1512 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1514 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1515 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1516 | // Find the constructor (which may be a template). |
| 1517 | CXXConstructorDecl *Constructor = 0; |
| 1518 | FunctionTemplateDecl *ConstructorTmpl |
| 1519 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1520 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1522 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1523 | else |
| 1524 | Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1525 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1526 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1527 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1528 | if (ConstructorTmpl) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1529 | AddTemplateOverloadCandidate(ConstructorTmpl, /*ExplicitArgs*/ 0, |
| 1530 | &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1531 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1532 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1533 | // Allow one user-defined conversion when user specifies a |
| 1534 | // From->ToType conversion via an static cast (c-style, etc). |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1535 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1536 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1537 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1538 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1542 | if (!AllowConversionFunctions) { |
| 1543 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1545 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1546 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1547 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1549 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1551 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1552 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1553 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1554 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1555 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1556 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1557 | NamedDecl *D = *I; |
| 1558 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1559 | if (isa<UsingShadowDecl>(D)) |
| 1560 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1561 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1562 | CXXConversionDecl *Conv; |
| 1563 | FunctionTemplateDecl *ConvTemplate; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1564 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(*I))) |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1565 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1566 | else |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1567 | Conv = dyn_cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1568 | |
| 1569 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1570 | if (ConvTemplate) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1571 | AddTemplateConversionCandidate(ConvTemplate, ActingContext, |
| 1572 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1573 | else |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1574 | AddConversionCandidate(Conv, ActingContext, From, ToType, |
| 1575 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1579 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1580 | |
| 1581 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1582 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1583 | case OR_Success: |
| 1584 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1586 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1587 | // C++ [over.ics.user]p1: |
| 1588 | // If the user-defined conversion is specified by a |
| 1589 | // constructor (12.3.1), the initial standard conversion |
| 1590 | // sequence converts the source type to the type required by |
| 1591 | // the argument of the constructor. |
| 1592 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1593 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1594 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1595 | User.EllipsisConversion = true; |
| 1596 | else { |
| 1597 | User.Before = Best->Conversions[0].Standard; |
| 1598 | User.EllipsisConversion = false; |
| 1599 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1600 | User.ConversionFunction = Constructor; |
| 1601 | User.After.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1602 | User.After.setFromType( |
| 1603 | ThisType->getAs<PointerType>()->getPointeeType()); |
| 1604 | User.After.setToType(ToType); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1605 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1606 | } else if (CXXConversionDecl *Conversion |
| 1607 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1608 | // C++ [over.ics.user]p1: |
| 1609 | // |
| 1610 | // [...] If the user-defined conversion is specified by a |
| 1611 | // conversion function (12.3.2), the initial standard |
| 1612 | // conversion sequence converts the source type to the |
| 1613 | // implicit object parameter of the conversion function. |
| 1614 | User.Before = Best->Conversions[0].Standard; |
| 1615 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1616 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | |
| 1618 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1619 | // The second standard conversion sequence converts the |
| 1620 | // result of the user-defined conversion to the target type |
| 1621 | // for the sequence. Since an implicit conversion sequence |
| 1622 | // is an initialization, the special rules for |
| 1623 | // initialization by user-defined conversion apply when |
| 1624 | // selecting the best user-defined conversion for a |
| 1625 | // user-defined conversion sequence (see 13.3.3 and |
| 1626 | // 13.3.3.1). |
| 1627 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1628 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1629 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1630 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1631 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1632 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1634 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1635 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1636 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1637 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1638 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1639 | |
| 1640 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1641 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1644 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1645 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1646 | |
| 1647 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1648 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1649 | ImplicitConversionSequence ICS; |
| 1650 | OverloadCandidateSet CandidateSet; |
| 1651 | OverloadingResult OvResult = |
| 1652 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1653 | CandidateSet, true, false, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1654 | if (OvResult == OR_Ambiguous) |
| 1655 | Diag(From->getSourceRange().getBegin(), |
| 1656 | diag::err_typecheck_ambiguous_condition) |
| 1657 | << From->getType() << ToType << From->getSourceRange(); |
| 1658 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1659 | Diag(From->getSourceRange().getBegin(), |
| 1660 | diag::err_typecheck_nonviable_condition) |
| 1661 | << From->getType() << ToType << From->getSourceRange(); |
| 1662 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1663 | return false; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1664 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1665 | return true; |
| 1666 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1667 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1668 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1669 | /// conversion sequences to determine whether one is better than the |
| 1670 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1672 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1673 | const ImplicitConversionSequence& ICS2) |
| 1674 | { |
| 1675 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1676 | // conversion sequences (as defined in 13.3.3.1) |
| 1677 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1678 | // conversion sequence than a user-defined conversion sequence or |
| 1679 | // an ellipsis conversion sequence, and |
| 1680 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1681 | // conversion sequence than an ellipsis conversion sequence |
| 1682 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1684 | // C++0x [over.best.ics]p10: |
| 1685 | // For the purpose of ranking implicit conversion sequences as |
| 1686 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1687 | // treated as a user-defined sequence that is indistinguishable |
| 1688 | // from any other user-defined conversion sequence. |
| 1689 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1690 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1691 | return ImplicitConversionSequence::Better; |
| 1692 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1693 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1694 | return ImplicitConversionSequence::Worse; |
| 1695 | } |
| 1696 | |
| 1697 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1698 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Two implicit conversion sequences of the same form are |
| 1701 | // indistinguishable conversion sequences unless one of the |
| 1702 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1703 | if (ICS1.isStandard()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1704 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1705 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1706 | // User-defined conversion sequence U1 is a better conversion |
| 1707 | // sequence than another user-defined conversion sequence U2 if |
| 1708 | // they contain the same user-defined conversion function or |
| 1709 | // constructor and if the second standard conversion sequence of |
| 1710 | // U1 is better than the second standard conversion sequence of |
| 1711 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1713 | ICS2.UserDefined.ConversionFunction) |
| 1714 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1715 | ICS2.UserDefined.After); |
| 1716 | } |
| 1717 | |
| 1718 | return ImplicitConversionSequence::Indistinguishable; |
| 1719 | } |
| 1720 | |
| 1721 | /// CompareStandardConversionSequences - Compare two standard |
| 1722 | /// conversion sequences to determine whether one is better than the |
| 1723 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1725 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1726 | const StandardConversionSequence& SCS2) |
| 1727 | { |
| 1728 | // Standard conversion sequence S1 is a better conversion sequence |
| 1729 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1730 | |
| 1731 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1732 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1733 | // excluding any Lvalue Transformation; the identity conversion |
| 1734 | // sequence is considered to be a subsequence of any |
| 1735 | // non-identity conversion sequence) or, if not that, |
| 1736 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1737 | // Neither is a proper subsequence of the other. Do nothing. |
| 1738 | ; |
| 1739 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1740 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1742 | SCS1.Third == ICK_Identity)) |
| 1743 | // SCS1 is a proper subsequence of SCS2. |
| 1744 | return ImplicitConversionSequence::Better; |
| 1745 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1746 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1748 | SCS2.Third == ICK_Identity)) |
| 1749 | // SCS2 is a proper subsequence of SCS1. |
| 1750 | return ImplicitConversionSequence::Worse; |
| 1751 | |
| 1752 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1753 | // defined below), or, if not that, |
| 1754 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1755 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1756 | if (Rank1 < Rank2) |
| 1757 | return ImplicitConversionSequence::Better; |
| 1758 | else if (Rank2 < Rank1) |
| 1759 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1760 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1761 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1762 | // are indistinguishable unless one of the following rules |
| 1763 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1765 | // A conversion that is not a conversion of a pointer, or |
| 1766 | // pointer to member, to bool is better than another conversion |
| 1767 | // that is such a conversion. |
| 1768 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1769 | return SCS2.isPointerConversionToBool() |
| 1770 | ? ImplicitConversionSequence::Better |
| 1771 | : ImplicitConversionSequence::Worse; |
| 1772 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1773 | // C++ [over.ics.rank]p4b2: |
| 1774 | // |
| 1775 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1776 | // conversion of B* to A* is better than conversion of B* to |
| 1777 | // void*, and conversion of A* to void* is better than conversion |
| 1778 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1780 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1782 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1783 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1784 | // Exactly one of the conversion sequences is a conversion to |
| 1785 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1786 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1787 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1788 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1789 | // Neither conversion sequence converts to a void pointer; compare |
| 1790 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1791 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1792 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1793 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1794 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1795 | // Both conversion sequences are conversions to void |
| 1796 | // pointers. Compare the source types to determine if there's an |
| 1797 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1798 | QualType FromType1 = SCS1.getFromType(); |
| 1799 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1800 | |
| 1801 | // Adjust the types we're converting from via the array-to-pointer |
| 1802 | // conversion, if we need to. |
| 1803 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1804 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1805 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1806 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1807 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1808 | QualType FromPointee1 |
| 1809 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1810 | QualType FromPointee2 |
| 1811 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1812 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1813 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1814 | return ImplicitConversionSequence::Better; |
| 1815 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1816 | return ImplicitConversionSequence::Worse; |
| 1817 | |
| 1818 | // Objective-C++: If one interface is more specific than the |
| 1819 | // other, it is the better one. |
| 1820 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1821 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1822 | if (FromIface1 && FromIface1) { |
| 1823 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1824 | return ImplicitConversionSequence::Better; |
| 1825 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1826 | return ImplicitConversionSequence::Worse; |
| 1827 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1828 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1829 | |
| 1830 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1831 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1833 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1834 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1836 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1837 | // C++0x [over.ics.rank]p3b4: |
| 1838 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1839 | // implicit object parameter of a non-static member function declared |
| 1840 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1841 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1842 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1843 | // or if the member function in this case has a ref qualifier. |
| 1844 | // (Of course, we don't have ref qualifiers yet.) |
| 1845 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1846 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1847 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1848 | |
| 1849 | // C++ [over.ics.rank]p3b4: |
| 1850 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1851 | // which the references refer are the same type except for |
| 1852 | // top-level cv-qualifiers, and the type to which the reference |
| 1853 | // initialized by S2 refers is more cv-qualified than the type |
| 1854 | // to which the reference initialized by S1 refers. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1855 | QualType T1 = SCS1.getToType(); |
| 1856 | QualType T2 = SCS2.getToType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1857 | T1 = Context.getCanonicalType(T1); |
| 1858 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1859 | Qualifiers T1Quals, T2Quals; |
| 1860 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1861 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1862 | if (UnqualT1 == UnqualT2) { |
| 1863 | // If the type is an array type, promote the element qualifiers to the type |
| 1864 | // for comparison. |
| 1865 | if (isa<ArrayType>(T1) && T1Quals) |
| 1866 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1867 | if (isa<ArrayType>(T2) && T2Quals) |
| 1868 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1869 | if (T2.isMoreQualifiedThan(T1)) |
| 1870 | return ImplicitConversionSequence::Better; |
| 1871 | else if (T1.isMoreQualifiedThan(T2)) |
| 1872 | return ImplicitConversionSequence::Worse; |
| 1873 | } |
| 1874 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1875 | |
| 1876 | return ImplicitConversionSequence::Indistinguishable; |
| 1877 | } |
| 1878 | |
| 1879 | /// CompareQualificationConversions - Compares two standard conversion |
| 1880 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1882 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1883 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1885 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1886 | // -- S1 and S2 differ only in their qualification conversion and |
| 1887 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1888 | // cv-qualification signature of type T1 is a proper subset of |
| 1889 | // the cv-qualification signature of type T2, and S1 is not the |
| 1890 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1891 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1892 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1893 | return ImplicitConversionSequence::Indistinguishable; |
| 1894 | |
| 1895 | // FIXME: the example in the standard doesn't use a qualification |
| 1896 | // conversion (!) |
| 1897 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1898 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1899 | T1 = Context.getCanonicalType(T1); |
| 1900 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1901 | Qualifiers T1Quals, T2Quals; |
| 1902 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1903 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1904 | |
| 1905 | // If the types are the same, we won't learn anything by unwrapped |
| 1906 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1907 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1908 | return ImplicitConversionSequence::Indistinguishable; |
| 1909 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1910 | // If the type is an array type, promote the element qualifiers to the type |
| 1911 | // for comparison. |
| 1912 | if (isa<ArrayType>(T1) && T1Quals) |
| 1913 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1914 | if (isa<ArrayType>(T2) && T2Quals) |
| 1915 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1916 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1918 | = ImplicitConversionSequence::Indistinguishable; |
| 1919 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1920 | // Within each iteration of the loop, we check the qualifiers to |
| 1921 | // determine if this still looks like a qualification |
| 1922 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1923 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1924 | // until there are no more pointers or pointers-to-members left |
| 1925 | // to unwrap. This essentially mimics what |
| 1926 | // IsQualificationConversion does, but here we're checking for a |
| 1927 | // strict subset of qualifiers. |
| 1928 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1929 | // The qualifiers are the same, so this doesn't tell us anything |
| 1930 | // about how the sequences rank. |
| 1931 | ; |
| 1932 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1933 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1934 | if (Result == ImplicitConversionSequence::Worse) |
| 1935 | // Neither has qualifiers that are a subset of the other's |
| 1936 | // qualifiers. |
| 1937 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1939 | Result = ImplicitConversionSequence::Better; |
| 1940 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1941 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1942 | if (Result == ImplicitConversionSequence::Better) |
| 1943 | // Neither has qualifiers that are a subset of the other's |
| 1944 | // qualifiers. |
| 1945 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1947 | Result = ImplicitConversionSequence::Worse; |
| 1948 | } else { |
| 1949 | // Qualifiers are disjoint. |
| 1950 | return ImplicitConversionSequence::Indistinguishable; |
| 1951 | } |
| 1952 | |
| 1953 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1954 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1955 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1958 | // Check that the winning standard conversion sequence isn't using |
| 1959 | // the deprecated string literal array to pointer conversion. |
| 1960 | switch (Result) { |
| 1961 | case ImplicitConversionSequence::Better: |
| 1962 | if (SCS1.Deprecated) |
| 1963 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1964 | break; |
| 1965 | |
| 1966 | case ImplicitConversionSequence::Indistinguishable: |
| 1967 | break; |
| 1968 | |
| 1969 | case ImplicitConversionSequence::Worse: |
| 1970 | if (SCS2.Deprecated) |
| 1971 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1972 | break; |
| 1973 | } |
| 1974 | |
| 1975 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1978 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1979 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1980 | /// various kinds of derived-to-base conversions (C++ |
| 1981 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1982 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1983 | ImplicitConversionSequence::CompareKind |
| 1984 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1985 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1986 | QualType FromType1 = SCS1.getFromType(); |
| 1987 | QualType ToType1 = SCS1.getToType(); |
| 1988 | QualType FromType2 = SCS2.getFromType(); |
| 1989 | QualType ToType2 = SCS2.getToType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1990 | |
| 1991 | // Adjust the types we're converting from via the array-to-pointer |
| 1992 | // conversion, if we need to. |
| 1993 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1994 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1995 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1996 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1997 | |
| 1998 | // Canonicalize all of the types. |
| 1999 | FromType1 = Context.getCanonicalType(FromType1); |
| 2000 | ToType1 = Context.getCanonicalType(ToType1); |
| 2001 | FromType2 = Context.getCanonicalType(FromType2); |
| 2002 | ToType2 = Context.getCanonicalType(ToType2); |
| 2003 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2004 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2005 | // |
| 2006 | // If class B is derived directly or indirectly from class A and |
| 2007 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2008 | // |
| 2009 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2010 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2014 | SCS2.Second == ICK_Pointer_Conversion && |
| 2015 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2016 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2017 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2019 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2021 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2022 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2023 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2024 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2025 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2026 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2027 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2028 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2029 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2030 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2032 | // -- 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] | 2033 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2034 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2035 | return ImplicitConversionSequence::Better; |
| 2036 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2037 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2038 | |
| 2039 | if (ToIface1 && ToIface2) { |
| 2040 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2041 | return ImplicitConversionSequence::Better; |
| 2042 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2043 | return ImplicitConversionSequence::Worse; |
| 2044 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2045 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2046 | |
| 2047 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2048 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2049 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2050 | return ImplicitConversionSequence::Better; |
| 2051 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2052 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2054 | if (FromIface1 && FromIface2) { |
| 2055 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2056 | return ImplicitConversionSequence::Better; |
| 2057 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2058 | return ImplicitConversionSequence::Worse; |
| 2059 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2060 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2063 | // Compare based on reference bindings. |
| 2064 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 2065 | SCS1.Second == ICK_Derived_To_Base) { |
| 2066 | // -- binding of an expression of type C to a reference of type |
| 2067 | // B& is better than binding an expression of type C to a |
| 2068 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2069 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2070 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2071 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2072 | return ImplicitConversionSequence::Better; |
| 2073 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2074 | return ImplicitConversionSequence::Worse; |
| 2075 | } |
| 2076 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2077 | // -- binding of an expression of type B to a reference of type |
| 2078 | // A& is better than binding an expression of type C to a |
| 2079 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2080 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2081 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2082 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2083 | return ImplicitConversionSequence::Better; |
| 2084 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2085 | return ImplicitConversionSequence::Worse; |
| 2086 | } |
| 2087 | } |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2088 | |
| 2089 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2090 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2091 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2092 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2093 | const MemberPointerType * FromMemPointer1 = |
| 2094 | FromType1->getAs<MemberPointerType>(); |
| 2095 | const MemberPointerType * ToMemPointer1 = |
| 2096 | ToType1->getAs<MemberPointerType>(); |
| 2097 | const MemberPointerType * FromMemPointer2 = |
| 2098 | FromType2->getAs<MemberPointerType>(); |
| 2099 | const MemberPointerType * ToMemPointer2 = |
| 2100 | ToType2->getAs<MemberPointerType>(); |
| 2101 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2102 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2103 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2104 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2105 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2106 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2107 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2108 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2109 | // 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] | 2110 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2111 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2112 | return ImplicitConversionSequence::Worse; |
| 2113 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2114 | return ImplicitConversionSequence::Better; |
| 2115 | } |
| 2116 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2117 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2118 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2119 | return ImplicitConversionSequence::Better; |
| 2120 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2121 | return ImplicitConversionSequence::Worse; |
| 2122 | } |
| 2123 | } |
| 2124 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2125 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 2126 | SCS1.Second == ICK_Derived_To_Base) { |
| 2127 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2128 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2129 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2130 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2131 | return ImplicitConversionSequence::Better; |
| 2132 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2133 | return ImplicitConversionSequence::Worse; |
| 2134 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2136 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2137 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2138 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2139 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2140 | return ImplicitConversionSequence::Better; |
| 2141 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2142 | return ImplicitConversionSequence::Worse; |
| 2143 | } |
| 2144 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2146 | return ImplicitConversionSequence::Indistinguishable; |
| 2147 | } |
| 2148 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2149 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2150 | /// ToType from the expression From. Return the implicit conversion |
| 2151 | /// sequence required to pass this argument, which may be a bad |
| 2152 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2153 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2154 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2155 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | ImplicitConversionSequence |
| 2157 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2158 | bool SuppressUserConversions, bool ForceRValue, |
| 2159 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2160 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2161 | ImplicitConversionSequence ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2162 | ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2164 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2165 | SuppressUserConversions, |
| 2166 | /*AllowExplicit=*/false, |
| 2167 | ForceRValue, |
| 2168 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2169 | return ICS; |
| 2170 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2172 | SuppressUserConversions, |
| 2173 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2174 | ForceRValue, |
| 2175 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2179 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2180 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2181 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2182 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2183 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2185 | AssignmentAction Action, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2186 | if (!getLangOptions().CPlusPlus) { |
| 2187 | // In C, argument passing is the same as performing an assignment. |
| 2188 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2190 | AssignConvertType ConvTy = |
| 2191 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2192 | if (ConvTy != Compatible && |
| 2193 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2194 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2196 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2197 | FromType, From, Action); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2198 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2199 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2200 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2201 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2202 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2203 | /*SuppressUserConversions=*/false, |
| 2204 | /*AllowExplicit=*/false, |
| 2205 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2206 | |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2207 | if (!PerformImplicitConversion(From, ToType, Action, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2208 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2209 | return false; |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2210 | if (!DiagnoseMultipleUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2211 | return Diag(From->getSourceRange().getBegin(), |
| 2212 | diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2213 | << ToType << From->getType() << Action << From->getSourceRange(); |
Fariborz Jahanian | 455acd9 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2214 | return true; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2217 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2218 | /// parameter of the given member function (@c Method) from the |
| 2219 | /// expression @p From. |
| 2220 | ImplicitConversionSequence |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2221 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2222 | CXXMethodDecl *Method, |
| 2223 | CXXRecordDecl *ActingContext) { |
| 2224 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2225 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2226 | // const volatile object. |
| 2227 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2228 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2229 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2230 | |
| 2231 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2232 | // to exit early. |
| 2233 | ImplicitConversionSequence ICS; |
| 2234 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2235 | ICS.setBad(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2236 | |
| 2237 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2238 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2239 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2240 | FromType = PT->getPointeeType(); |
| 2241 | |
| 2242 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2243 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2244 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2245 | // where X is the class of which the function is a member |
| 2246 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2247 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2249 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2250 | // reference binding here, that allows class rvalues to bind to |
| 2251 | // non-constant references. |
| 2252 | |
| 2253 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2254 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2255 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2256 | if (ImplicitParamType.getCVRQualifiers() |
| 2257 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2258 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2259 | ICS.Bad.init(BadConversionSequence::bad_qualifiers, |
| 2260 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2261 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2262 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2263 | |
| 2264 | // Check that we have either the same type or a derived type. It |
| 2265 | // affects the conversion rank. |
| 2266 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2267 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2268 | ICS.Standard.Second = ICK_Identity; |
| 2269 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2270 | ICS.Standard.Second = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2271 | else { |
| 2272 | ICS.Bad.init(BadConversionSequence::unrelated_class, FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2273 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2274 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2275 | |
| 2276 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2277 | ICS.setStandard(); |
| 2278 | ICS.Standard.setFromType(FromType); |
| 2279 | ICS.Standard.setToType(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2280 | ICS.Standard.ReferenceBinding = true; |
| 2281 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2282 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2283 | return ICS; |
| 2284 | } |
| 2285 | |
| 2286 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2287 | /// the implicit object parameter for the given Method with the given |
| 2288 | /// expression. |
| 2289 | bool |
| 2290 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2291 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2293 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2295 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2296 | FromRecordType = PT->getPointeeType(); |
| 2297 | DestType = Method->getThisType(Context); |
| 2298 | } else { |
| 2299 | FromRecordType = From->getType(); |
| 2300 | DestType = ImplicitParamRecordType; |
| 2301 | } |
| 2302 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2303 | // Note that we always use the true parent context when performing |
| 2304 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | ImplicitConversionSequence ICS |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2306 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2307 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2308 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2309 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2310 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2311 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2313 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2314 | CheckDerivedToBaseConversion(FromRecordType, |
| 2315 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2316 | From->getSourceRange().getBegin(), |
| 2317 | From->getSourceRange())) |
| 2318 | return true; |
| 2319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2321 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2322 | return false; |
| 2323 | } |
| 2324 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2325 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2326 | /// expression From to bool (C++0x [conv]p3). |
| 2327 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2329 | // FIXME: Are these flags correct? |
| 2330 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2331 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2332 | /*ForceRValue=*/false, |
| 2333 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2337 | /// of the expression From to bool (C++0x [conv]p3). |
| 2338 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2339 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2340 | if (!ICS.isBad()) |
| 2341 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2342 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2343 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2344 | return Diag(From->getSourceRange().getBegin(), |
| 2345 | diag::err_typecheck_bool_condition) |
| 2346 | << From->getType() << From->getSourceRange(); |
| 2347 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2350 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2351 | /// candidate functions, using the given function call arguments. If |
| 2352 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2353 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2354 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2355 | /// hacky way to implement the overloading rules for elidable copy |
| 2356 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2357 | /// |
| 2358 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2359 | /// based on an incomplete set of function arguments. This feature is used by |
| 2360 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | void |
| 2362 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2363 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2364 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2365 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2366 | bool ForceRValue, |
| 2367 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2369 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2370 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2372 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2374 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2375 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2376 | // If we get here, it's because we're calling a member function |
| 2377 | // that is named without a member access expression (e.g., |
| 2378 | // "this->f") that was either written explicitly or created |
| 2379 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2380 | // function, e.g., X::f(). We use an empty type for the implied |
| 2381 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2382 | // is irrelevant. |
| 2383 | AddMethodCandidate(Method, Method->getParent(), |
| 2384 | QualType(), Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2385 | SuppressUserConversions, ForceRValue); |
| 2386 | return; |
| 2387 | } |
| 2388 | // We treat a constructor like a non-member function, since its object |
| 2389 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2392 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2393 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2394 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2395 | // Overload resolution is always an unevaluated context. |
| 2396 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2397 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2398 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2399 | // C++ [class.copy]p3: |
| 2400 | // A member function template is never instantiated to perform the copy |
| 2401 | // of a class object to an object of its class type. |
| 2402 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2403 | if (NumArgs == 1 && |
| 2404 | Constructor->isCopyConstructorLikeSpecialization() && |
| 2405 | Context.hasSameUnqualifiedType(ClassType, Args[0]->getType())) |
| 2406 | return; |
| 2407 | } |
| 2408 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2409 | // Add this candidate |
| 2410 | CandidateSet.push_back(OverloadCandidate()); |
| 2411 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2412 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2413 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2414 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2415 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2416 | |
| 2417 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2418 | |
| 2419 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2420 | // parameters is viable only if it has an ellipsis in its parameter |
| 2421 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2422 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2423 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2424 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2425 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2426 | return; |
| 2427 | } |
| 2428 | |
| 2429 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2430 | // is viable only if the (m+1)st parameter has a default argument |
| 2431 | // (8.3.6). For the purposes of overload resolution, the |
| 2432 | // parameter list is truncated on the right, so that there are |
| 2433 | // exactly m parameters. |
| 2434 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2435 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2436 | // Not enough arguments. |
| 2437 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2438 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2439 | return; |
| 2440 | } |
| 2441 | |
| 2442 | // Determine the implicit conversion sequences for each of the |
| 2443 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2444 | Candidate.Conversions.resize(NumArgs); |
| 2445 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2446 | if (ArgIdx < NumArgsInProto) { |
| 2447 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2448 | // exist for each argument an implicit conversion sequence |
| 2449 | // (13.3.3.1) that converts that argument to the corresponding |
| 2450 | // parameter of F. |
| 2451 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | Candidate.Conversions[ArgIdx] |
| 2453 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2454 | SuppressUserConversions, ForceRValue, |
| 2455 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2456 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2457 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2458 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2459 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2460 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2461 | } else { |
| 2462 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2463 | // argument for which there is no corresponding parameter is |
| 2464 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2465 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2466 | } |
| 2467 | } |
| 2468 | } |
| 2469 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2470 | /// \brief Add all of the function declarations in the given function set to |
| 2471 | /// the overload canddiate set. |
| 2472 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2473 | Expr **Args, unsigned NumArgs, |
| 2474 | OverloadCandidateSet& CandidateSet, |
| 2475 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2476 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2477 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2478 | F != FEnd; ++F) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2479 | // FIXME: using declarations |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2480 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2481 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
| 2482 | AddMethodCandidate(cast<CXXMethodDecl>(FD), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2483 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2484 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2485 | CandidateSet, SuppressUserConversions); |
| 2486 | else |
| 2487 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 2488 | SuppressUserConversions); |
| 2489 | } else { |
| 2490 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2491 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2492 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
| 2493 | AddMethodTemplateCandidate(FunTmpl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2494 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2495 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2496 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2497 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2498 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2499 | else |
| 2500 | AddTemplateOverloadCandidate(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2501 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2502 | Args, NumArgs, CandidateSet, |
| 2503 | SuppressUserConversions); |
| 2504 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2505 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2508 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2509 | /// method) as a method candidate to the given overload set. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2510 | void Sema::AddMethodCandidate(NamedDecl *Decl, |
| 2511 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2512 | Expr **Args, unsigned NumArgs, |
| 2513 | OverloadCandidateSet& CandidateSet, |
| 2514 | bool SuppressUserConversions, bool ForceRValue) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2515 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2516 | |
| 2517 | if (isa<UsingShadowDecl>(Decl)) |
| 2518 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2519 | |
| 2520 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2521 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2522 | "Expected a member function template"); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2523 | AddMethodTemplateCandidate(TD, ActingContext, /*ExplicitArgs*/ 0, |
| 2524 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2525 | CandidateSet, |
| 2526 | SuppressUserConversions, |
| 2527 | ForceRValue); |
| 2528 | } else { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2529 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), ActingContext, |
| 2530 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2531 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2532 | } |
| 2533 | } |
| 2534 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2535 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2536 | /// of candidate functions, using the given function call arguments |
| 2537 | /// and the object argument (@c Object). For example, in a call |
| 2538 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2539 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2540 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2541 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2542 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2543 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | void |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2545 | Sema::AddMethodCandidate(CXXMethodDecl *Method, CXXRecordDecl *ActingContext, |
| 2546 | QualType ObjectType, Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2547 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | bool SuppressUserConversions, bool ForceRValue) { |
| 2549 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2550 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2551 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2552 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2553 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2555 | if (!CandidateSet.isNewCandidate(Method)) |
| 2556 | return; |
| 2557 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2558 | // Overload resolution is always an unevaluated context. |
| 2559 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2560 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2561 | // Add this candidate |
| 2562 | CandidateSet.push_back(OverloadCandidate()); |
| 2563 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2564 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2565 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2566 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2567 | |
| 2568 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2569 | |
| 2570 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2571 | // parameters is viable only if it has an ellipsis in its parameter |
| 2572 | // list (8.3.5). |
| 2573 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2574 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2575 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2576 | return; |
| 2577 | } |
| 2578 | |
| 2579 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2580 | // is viable only if the (m+1)st parameter has a default argument |
| 2581 | // (8.3.6). For the purposes of overload resolution, the |
| 2582 | // parameter list is truncated on the right, so that there are |
| 2583 | // exactly m parameters. |
| 2584 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2585 | if (NumArgs < MinRequiredArgs) { |
| 2586 | // Not enough arguments. |
| 2587 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2588 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2589 | return; |
| 2590 | } |
| 2591 | |
| 2592 | Candidate.Viable = true; |
| 2593 | Candidate.Conversions.resize(NumArgs + 1); |
| 2594 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2595 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2596 | // The implicit object argument is ignored. |
| 2597 | Candidate.IgnoreObjectArgument = true; |
| 2598 | else { |
| 2599 | // Determine the implicit conversion sequence for the object |
| 2600 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2601 | Candidate.Conversions[0] |
| 2602 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2603 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2604 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2605 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | // Determine the implicit conversion sequences for each of the |
| 2611 | // arguments. |
| 2612 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2613 | if (ArgIdx < NumArgsInProto) { |
| 2614 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2615 | // exist for each argument an implicit conversion sequence |
| 2616 | // (13.3.3.1) that converts that argument to the corresponding |
| 2617 | // parameter of F. |
| 2618 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2619 | Candidate.Conversions[ArgIdx + 1] |
| 2620 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2621 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2622 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2623 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2624 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2625 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2626 | break; |
| 2627 | } |
| 2628 | } else { |
| 2629 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2630 | // argument for which there is no corresponding parameter is |
| 2631 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2632 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2637 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2638 | /// set, using template argument deduction to produce an appropriate member |
| 2639 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2641 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2642 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2643 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2644 | QualType ObjectType, |
| 2645 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2646 | OverloadCandidateSet& CandidateSet, |
| 2647 | bool SuppressUserConversions, |
| 2648 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2649 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2650 | return; |
| 2651 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2652 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2654 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | // 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] | 2656 | // candidate functions in the usual way.113) A given name can refer to one |
| 2657 | // or more function templates and also to a set of overloaded non-template |
| 2658 | // functions. In such a case, the candidate functions generated from each |
| 2659 | // function template are combined with the set of non-template candidate |
| 2660 | // functions. |
| 2661 | TemplateDeductionInfo Info(Context); |
| 2662 | FunctionDecl *Specialization = 0; |
| 2663 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2664 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2665 | Args, NumArgs, Specialization, Info)) { |
| 2666 | // FIXME: Record what happened with template argument deduction, so |
| 2667 | // that we can give the user a beautiful diagnostic. |
| 2668 | (void)Result; |
| 2669 | return; |
| 2670 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2672 | // Add the function template specialization produced by template argument |
| 2673 | // deduction as a candidate. |
| 2674 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2676 | "Specialization is not a member function?"); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2677 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), ActingContext, |
| 2678 | ObjectType, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2679 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2680 | } |
| 2681 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2682 | /// \brief Add a C++ function template specialization as a candidate |
| 2683 | /// in the candidate set, using template argument deduction to produce |
| 2684 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2686 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2687 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2688 | Expr **Args, unsigned NumArgs, |
| 2689 | OverloadCandidateSet& CandidateSet, |
| 2690 | bool SuppressUserConversions, |
| 2691 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2692 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2693 | return; |
| 2694 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2695 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2697 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | // 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] | 2699 | // candidate functions in the usual way.113) A given name can refer to one |
| 2700 | // or more function templates and also to a set of overloaded non-template |
| 2701 | // functions. In such a case, the candidate functions generated from each |
| 2702 | // function template are combined with the set of non-template candidate |
| 2703 | // functions. |
| 2704 | TemplateDeductionInfo Info(Context); |
| 2705 | FunctionDecl *Specialization = 0; |
| 2706 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2707 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2708 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2709 | // FIXME: Record what happened with template argument deduction, so |
| 2710 | // that we can give the user a beautiful diagnostic. |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2711 | (void) Result; |
| 2712 | |
| 2713 | CandidateSet.push_back(OverloadCandidate()); |
| 2714 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 2715 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 2716 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2717 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2718 | Candidate.IsSurrogate = false; |
| 2719 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2723 | // Add the function template specialization produced by template argument |
| 2724 | // deduction as a candidate. |
| 2725 | assert(Specialization && "Missing function template specialization?"); |
| 2726 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2727 | SuppressUserConversions, ForceRValue); |
| 2728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2730 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2731 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2732 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | /// 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] | 2734 | /// (which may or may not be the same type as the type that the |
| 2735 | /// conversion function produces). |
| 2736 | void |
| 2737 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2738 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2739 | Expr *From, QualType ToType, |
| 2740 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2741 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2742 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2743 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2744 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2745 | return; |
| 2746 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2747 | // Overload resolution is always an unevaluated context. |
| 2748 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2749 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2750 | // Add this candidate |
| 2751 | CandidateSet.push_back(OverloadCandidate()); |
| 2752 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2753 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2754 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2755 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2756 | Candidate.FinalConversion.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2757 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
| 2758 | Candidate.FinalConversion.setToType(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2759 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2760 | // Determine the implicit conversion sequence for the implicit |
| 2761 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2762 | Candidate.Viable = true; |
| 2763 | Candidate.Conversions.resize(1); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2764 | Candidate.Conversions[0] |
| 2765 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2766 | ActingContext); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2767 | // Conversion functions to a different type in the base class is visible in |
| 2768 | // the derived class. So, a derived to base conversion should not participate |
| 2769 | // in overload resolution. |
| 2770 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2771 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2772 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2773 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2774 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2775 | return; |
| 2776 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2777 | |
| 2778 | // We won't go through a user-define type conversion function to convert a |
| 2779 | // derived to base as such conversions are given Conversion Rank. They only |
| 2780 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2781 | QualType FromCanon |
| 2782 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2783 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2784 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2785 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2786 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2787 | return; |
| 2788 | } |
| 2789 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2790 | |
| 2791 | // To determine what the conversion from the result of calling the |
| 2792 | // conversion function to the type we're eventually trying to |
| 2793 | // convert to (ToType), we need to synthesize a call to the |
| 2794 | // conversion function and attempt copy initialization from it. This |
| 2795 | // makes sure that we get the right semantics with respect to |
| 2796 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2797 | // call on the stack and we don't need its arguments to be |
| 2798 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2800 | From->getLocStart()); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2801 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2802 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2803 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
| 2805 | // 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] | 2806 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2807 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2809 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2810 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | ImplicitConversionSequence ICS = |
| 2812 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2813 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2814 | /*ForceRValue=*/false, |
| 2815 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2817 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2818 | case ImplicitConversionSequence::StandardConversion: |
| 2819 | Candidate.FinalConversion = ICS.Standard; |
| 2820 | break; |
| 2821 | |
| 2822 | case ImplicitConversionSequence::BadConversion: |
| 2823 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2824 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2825 | break; |
| 2826 | |
| 2827 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2829 | "Can only end up with a standard conversion sequence or failure"); |
| 2830 | } |
| 2831 | } |
| 2832 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2833 | /// \brief Adds a conversion function template specialization |
| 2834 | /// candidate to the overload set, using template argument deduction |
| 2835 | /// to deduce the template arguments of the conversion function |
| 2836 | /// template from the type that we are converting to (C++ |
| 2837 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2839 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2840 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2841 | Expr *From, QualType ToType, |
| 2842 | OverloadCandidateSet &CandidateSet) { |
| 2843 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2844 | "Only conversion function templates permitted here"); |
| 2845 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2846 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2847 | return; |
| 2848 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2849 | TemplateDeductionInfo Info(Context); |
| 2850 | CXXConversionDecl *Specialization = 0; |
| 2851 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2853 | Specialization, Info)) { |
| 2854 | // FIXME: Record what happened with template argument deduction, so |
| 2855 | // that we can give the user a beautiful diagnostic. |
| 2856 | (void)Result; |
| 2857 | return; |
| 2858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2860 | // Add the conversion function template specialization produced by |
| 2861 | // template argument deduction as a candidate. |
| 2862 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2863 | AddConversionCandidate(Specialization, ActingDC, From, ToType, CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2866 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2867 | /// converts the given @c Object to a function pointer via the |
| 2868 | /// conversion function @c Conversion, and then attempts to call it |
| 2869 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2870 | /// the type of function that we'll eventually be calling. |
| 2871 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2872 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2873 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2874 | QualType ObjectType, |
| 2875 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2876 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2877 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2878 | return; |
| 2879 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2880 | // Overload resolution is always an unevaluated context. |
| 2881 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2882 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2883 | CandidateSet.push_back(OverloadCandidate()); |
| 2884 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2885 | Candidate.Function = 0; |
| 2886 | Candidate.Surrogate = Conversion; |
| 2887 | Candidate.Viable = true; |
| 2888 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2889 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2890 | Candidate.Conversions.resize(NumArgs + 1); |
| 2891 | |
| 2892 | // Determine the implicit conversion sequence for the implicit |
| 2893 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | ImplicitConversionSequence ObjectInit |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2895 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2896 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2897 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2898 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2899 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2900 | return; |
| 2901 | } |
| 2902 | |
| 2903 | // The first conversion is actually a user-defined conversion whose |
| 2904 | // first conversion is ObjectInit's standard conversion (which is |
| 2905 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2906 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2907 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2908 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2909 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2911 | = Candidate.Conversions[0].UserDefined.Before; |
| 2912 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2913 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2915 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2916 | |
| 2917 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2918 | // parameters is viable only if it has an ellipsis in its parameter |
| 2919 | // list (8.3.5). |
| 2920 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2921 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2922 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | // Function types don't have any default arguments, so just check if |
| 2927 | // we have enough arguments. |
| 2928 | if (NumArgs < NumArgsInProto) { |
| 2929 | // Not enough arguments. |
| 2930 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2931 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2932 | return; |
| 2933 | } |
| 2934 | |
| 2935 | // Determine the implicit conversion sequences for each of the |
| 2936 | // arguments. |
| 2937 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2938 | if (ArgIdx < NumArgsInProto) { |
| 2939 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2940 | // exist for each argument an implicit conversion sequence |
| 2941 | // (13.3.3.1) that converts that argument to the corresponding |
| 2942 | // parameter of F. |
| 2943 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | Candidate.Conversions[ArgIdx + 1] |
| 2945 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2946 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2947 | /*ForceRValue=*/false, |
| 2948 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2949 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2950 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2951 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2952 | break; |
| 2953 | } |
| 2954 | } else { |
| 2955 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2956 | // argument for which there is no corresponding parameter is |
| 2957 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2958 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2963 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2964 | // operator overloading logic over to the scheme used by binary operators, which |
| 2965 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2966 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2967 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2968 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2969 | OverloadCandidateSet& CandidateSet, |
| 2970 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2971 | FunctionSet Functions; |
| 2972 | |
| 2973 | QualType T1 = Args[0]->getType(); |
| 2974 | QualType T2; |
| 2975 | if (NumArgs > 1) |
| 2976 | T2 = Args[1]->getType(); |
| 2977 | |
| 2978 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2979 | if (S) |
| 2980 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 2981 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, NumArgs, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2982 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2983 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2984 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | /// \brief Add overload candidates for overloaded operators that are |
| 2988 | /// member functions. |
| 2989 | /// |
| 2990 | /// Add the overloaded operator candidates that are member functions |
| 2991 | /// for the operator Op that was used in an operator expression such |
| 2992 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2993 | /// CandidateSet will store the added overload candidates. (C++ |
| 2994 | /// [over.match.oper]). |
| 2995 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2996 | SourceLocation OpLoc, |
| 2997 | Expr **Args, unsigned NumArgs, |
| 2998 | OverloadCandidateSet& CandidateSet, |
| 2999 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3000 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3001 | |
| 3002 | // C++ [over.match.oper]p3: |
| 3003 | // For a unary operator @ with an operand of a type whose |
| 3004 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3005 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3006 | // a right operand of a type whose cv-unqualified version is T2, |
| 3007 | // three sets of candidate functions, designated member |
| 3008 | // candidates, non-member candidates and built-in candidates, are |
| 3009 | // constructed as follows: |
| 3010 | QualType T1 = Args[0]->getType(); |
| 3011 | QualType T2; |
| 3012 | if (NumArgs > 1) |
| 3013 | T2 = Args[1]->getType(); |
| 3014 | |
| 3015 | // -- If T1 is a class type, the set of member candidates is the |
| 3016 | // result of the qualified lookup of T1::operator@ |
| 3017 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3018 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3019 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3020 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3021 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3022 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3024 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3025 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3026 | Operators.suppressDiagnostics(); |
| 3027 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3029 | OperEnd = Operators.end(); |
| 3030 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3031 | ++Oper) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3032 | AddMethodCandidate(*Oper, Args[0]->getType(), |
| 3033 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3034 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3035 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3038 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3039 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3040 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3041 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3042 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3043 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3044 | /// (at the beginning of the argument list) that will be contextually |
| 3045 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3047 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3048 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3049 | bool IsAssignmentOperator, |
| 3050 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3051 | // Overload resolution is always an unevaluated context. |
| 3052 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3053 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3054 | // Add this candidate |
| 3055 | CandidateSet.push_back(OverloadCandidate()); |
| 3056 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 3057 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3058 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3059 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3060 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3061 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3062 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3063 | |
| 3064 | // Determine the implicit conversion sequences for each of the |
| 3065 | // arguments. |
| 3066 | Candidate.Viable = true; |
| 3067 | Candidate.Conversions.resize(NumArgs); |
| 3068 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3069 | // C++ [over.match.oper]p4: |
| 3070 | // For the built-in assignment operators, conversions of the |
| 3071 | // left operand are restricted as follows: |
| 3072 | // -- no temporaries are introduced to hold the left operand, and |
| 3073 | // -- no user-defined conversions are applied to the left |
| 3074 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3076 | // |
| 3077 | // We block these conversions by turning off user-defined |
| 3078 | // conversions, since that is the only way that initialization of |
| 3079 | // a reference to a non-class type can occur from something that |
| 3080 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3081 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3083 | "Contextual conversion to bool requires bool type"); |
| 3084 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3085 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | Candidate.Conversions[ArgIdx] |
| 3087 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3088 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3089 | /*ForceRValue=*/false, |
| 3090 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3091 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3092 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3093 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3094 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3095 | break; |
| 3096 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3101 | /// candidate operator functions for built-in operators (C++ |
| 3102 | /// [over.built]). The types are separated into pointer types and |
| 3103 | /// enumeration types. |
| 3104 | class BuiltinCandidateTypeSet { |
| 3105 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3106 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3107 | |
| 3108 | /// PointerTypes - The set of pointer types that will be used in the |
| 3109 | /// built-in candidates. |
| 3110 | TypeSet PointerTypes; |
| 3111 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3112 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3113 | /// used in the built-in candidates. |
| 3114 | TypeSet MemberPointerTypes; |
| 3115 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3116 | /// EnumerationTypes - The set of enumeration types that will be |
| 3117 | /// used in the built-in candidates. |
| 3118 | TypeSet EnumerationTypes; |
| 3119 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3120 | /// Sema - The semantic analysis instance where we are building the |
| 3121 | /// candidate type set. |
| 3122 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3124 | /// Context - The AST context in which we will build the type sets. |
| 3125 | ASTContext &Context; |
| 3126 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3127 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3128 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3129 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3130 | |
| 3131 | public: |
| 3132 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3133 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3134 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3136 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3137 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3138 | void AddTypesConvertedFrom(QualType Ty, |
| 3139 | SourceLocation Loc, |
| 3140 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3141 | bool AllowExplicitConversions, |
| 3142 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3143 | |
| 3144 | /// pointer_begin - First pointer type found; |
| 3145 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3146 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3147 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3148 | iterator pointer_end() { return PointerTypes.end(); } |
| 3149 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3150 | /// member_pointer_begin - First member pointer type found; |
| 3151 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3152 | |
| 3153 | /// member_pointer_end - Past the last member pointer type found; |
| 3154 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3155 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3156 | /// enumeration_begin - First enumeration type found; |
| 3157 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3158 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3159 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3160 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3161 | }; |
| 3162 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3163 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3164 | /// the set of pointer types along with any more-qualified variants of |
| 3165 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3166 | /// will add "int const *", "int const volatile *", "int const |
| 3167 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3168 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3169 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3170 | /// |
| 3171 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3172 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3173 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3174 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3176 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3177 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3178 | return false; |
| 3179 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3180 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3181 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3182 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3183 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3184 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3185 | // (the qualifier would sink to the element type), and for another, the |
| 3186 | // only overload situation where it matters is subscript or pointer +- int, |
| 3187 | // and those shouldn't have qualifier variants anyway. |
| 3188 | if (PointeeTy->isArrayType()) |
| 3189 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3190 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3191 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3192 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3193 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3194 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3195 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3196 | // Iterate through all strict supersets of BaseCVR. |
| 3197 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3198 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3199 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3200 | // in the types. |
| 3201 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3202 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3203 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3204 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | return true; |
| 3208 | } |
| 3209 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3210 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3211 | /// to the set of pointer types along with any more-qualified variants of |
| 3212 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3213 | /// will add "int const *", "int const volatile *", "int const |
| 3214 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3215 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3216 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3217 | /// |
| 3218 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3219 | bool |
| 3220 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3221 | QualType Ty) { |
| 3222 | // Insert this type. |
| 3223 | if (!MemberPointerTypes.insert(Ty)) |
| 3224 | return false; |
| 3225 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3226 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3227 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3228 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3229 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3230 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3231 | // (the qualifier would sink to the element type), and for another, the |
| 3232 | // only overload situation where it matters is subscript or pointer +- int, |
| 3233 | // and those shouldn't have qualifier variants anyway. |
| 3234 | if (PointeeTy->isArrayType()) |
| 3235 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3236 | const Type *ClassTy = PointerTy->getClass(); |
| 3237 | |
| 3238 | // Iterate through all strict supersets of the pointee type's CVR |
| 3239 | // qualifiers. |
| 3240 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3241 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3242 | if ((CVR | BaseCVR) != CVR) continue; |
| 3243 | |
| 3244 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3245 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | return true; |
| 3249 | } |
| 3250 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3251 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3252 | /// 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] | 3253 | /// primarily interested in pointer types and enumeration types. We also |
| 3254 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3255 | /// AllowUserConversions is true if we should look at the conversion |
| 3256 | /// functions of a class type, and AllowExplicitConversions if we |
| 3257 | /// should also include the explicit conversion functions of a class |
| 3258 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3259 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3260 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3261 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3262 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3263 | bool AllowExplicitConversions, |
| 3264 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3265 | // Only deal with canonical types. |
| 3266 | Ty = Context.getCanonicalType(Ty); |
| 3267 | |
| 3268 | // Look through reference types; they aren't part of the type of an |
| 3269 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3270 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3271 | Ty = RefTy->getPointeeType(); |
| 3272 | |
| 3273 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3274 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3275 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3276 | // If we're dealing with an array type, decay to the pointer. |
| 3277 | if (Ty->isArrayType()) |
| 3278 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3279 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3280 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3281 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3282 | |
| 3283 | // Insert our type, and its more-qualified variants, into the set |
| 3284 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3285 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3286 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3287 | } else if (Ty->isMemberPointerType()) { |
| 3288 | // Member pointers are far easier, since the pointee can't be converted. |
| 3289 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3290 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3291 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3292 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3293 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3294 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3295 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3296 | // No conversion functions in incomplete types. |
| 3297 | return; |
| 3298 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3300 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3301 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3302 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3303 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3304 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3305 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3307 | // about which builtin types we can convert to. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3308 | if (isa<FunctionTemplateDecl>(*I)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3309 | continue; |
| 3310 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3311 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3312 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3313 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3314 | VisibleQuals); |
| 3315 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3316 | } |
| 3317 | } |
| 3318 | } |
| 3319 | } |
| 3320 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3321 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3322 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3323 | /// given type to the candidate set. |
| 3324 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3325 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3327 | unsigned NumArgs, |
| 3328 | OverloadCandidateSet &CandidateSet) { |
| 3329 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3330 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3331 | // T& operator=(T&, T) |
| 3332 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3333 | ParamTypes[1] = T; |
| 3334 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3335 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3337 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3338 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3339 | ParamTypes[0] |
| 3340 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3341 | ParamTypes[1] = T; |
| 3342 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3344 | } |
| 3345 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3347 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3348 | /// 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] | 3349 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3350 | Qualifiers VRQuals; |
| 3351 | const RecordType *TyRec; |
| 3352 | if (const MemberPointerType *RHSMPType = |
| 3353 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3354 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3355 | else |
| 3356 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3357 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3358 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3359 | VRQuals.addVolatile(); |
| 3360 | VRQuals.addRestrict(); |
| 3361 | return VRQuals; |
| 3362 | } |
| 3363 | |
| 3364 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3365 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3366 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3367 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3368 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3369 | E = Conversions->end(); I != E; ++I) { |
| 3370 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*I)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3371 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3372 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3373 | CanTy = ResTypeRef->getPointeeType(); |
| 3374 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3375 | // as see them. |
| 3376 | bool done = false; |
| 3377 | while (!done) { |
| 3378 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3379 | CanTy = ResTypePtr->getPointeeType(); |
| 3380 | else if (const MemberPointerType *ResTypeMPtr = |
| 3381 | CanTy->getAs<MemberPointerType>()) |
| 3382 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3383 | else |
| 3384 | done = true; |
| 3385 | if (CanTy.isVolatileQualified()) |
| 3386 | VRQuals.addVolatile(); |
| 3387 | if (CanTy.isRestrictQualified()) |
| 3388 | VRQuals.addRestrict(); |
| 3389 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3390 | return VRQuals; |
| 3391 | } |
| 3392 | } |
| 3393 | } |
| 3394 | return VRQuals; |
| 3395 | } |
| 3396 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3397 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3398 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3399 | /// on the operator @p Op and the arguments given. For example, if the |
| 3400 | /// operator is a binary '+', this routine might add "int |
| 3401 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3402 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3404 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3405 | Expr **Args, unsigned NumArgs, |
| 3406 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3407 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3408 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3409 | // that the first few of these types are the promoted integral |
| 3410 | // types; these types need to be first. |
| 3411 | // FIXME: What about complex? |
| 3412 | const unsigned FirstIntegralType = 0; |
| 3413 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3415 | LastPromotedIntegralType = 13; |
| 3416 | const unsigned FirstPromotedArithmeticType = 7, |
| 3417 | LastPromotedArithmeticType = 16; |
| 3418 | const unsigned NumArithmeticTypes = 16; |
| 3419 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3421 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3422 | Context.SignedCharTy, Context.ShortTy, |
| 3423 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3424 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3425 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3426 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3427 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3428 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3429 | "Invalid first promoted integral type"); |
| 3430 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3431 | == Context.UnsignedLongLongTy && |
| 3432 | "Invalid last promoted integral type"); |
| 3433 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3434 | "Invalid first promoted arithmetic type"); |
| 3435 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3436 | == Context.LongDoubleTy && |
| 3437 | "Invalid last promoted arithmetic type"); |
| 3438 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3439 | // Find all of the types that the arguments can convert to, but only |
| 3440 | // if the operator we're looking at has built-in operator candidates |
| 3441 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3442 | Qualifiers VisibleTypeConversionsQuals; |
| 3443 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3444 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3445 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3446 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3447 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3448 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3449 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3450 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3451 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3452 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3453 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3454 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3455 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3456 | OpLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3457 | true, |
| 3458 | (Op == OO_Exclaim || |
| 3459 | Op == OO_AmpAmp || |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3460 | Op == OO_PipePipe), |
| 3461 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | bool isComparison = false; |
| 3465 | switch (Op) { |
| 3466 | case OO_None: |
| 3467 | case NUM_OVERLOADED_OPERATORS: |
| 3468 | assert(false && "Expected an overloaded operator"); |
| 3469 | break; |
| 3470 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3471 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3473 | goto UnaryStar; |
| 3474 | else |
| 3475 | goto BinaryStar; |
| 3476 | break; |
| 3477 | |
| 3478 | case OO_Plus: // '+' is either unary or binary |
| 3479 | if (NumArgs == 1) |
| 3480 | goto UnaryPlus; |
| 3481 | else |
| 3482 | goto BinaryPlus; |
| 3483 | break; |
| 3484 | |
| 3485 | case OO_Minus: // '-' is either unary or binary |
| 3486 | if (NumArgs == 1) |
| 3487 | goto UnaryMinus; |
| 3488 | else |
| 3489 | goto BinaryMinus; |
| 3490 | break; |
| 3491 | |
| 3492 | case OO_Amp: // '&' is either unary or binary |
| 3493 | if (NumArgs == 1) |
| 3494 | goto UnaryAmp; |
| 3495 | else |
| 3496 | goto BinaryAmp; |
| 3497 | |
| 3498 | case OO_PlusPlus: |
| 3499 | case OO_MinusMinus: |
| 3500 | // C++ [over.built]p3: |
| 3501 | // |
| 3502 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3503 | // is either volatile or empty, there exist candidate operator |
| 3504 | // functions of the form |
| 3505 | // |
| 3506 | // VQ T& operator++(VQ T&); |
| 3507 | // T operator++(VQ T&, int); |
| 3508 | // |
| 3509 | // C++ [over.built]p4: |
| 3510 | // |
| 3511 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3512 | // than bool, and VQ is either volatile or empty, there exist |
| 3513 | // candidate operator functions of the form |
| 3514 | // |
| 3515 | // VQ T& operator--(VQ T&); |
| 3516 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3517 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3518 | Arith < NumArithmeticTypes; ++Arith) { |
| 3519 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3521 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3522 | |
| 3523 | // Non-volatile version. |
| 3524 | if (NumArgs == 1) |
| 3525 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3526 | else |
| 3527 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3528 | // heuristic to reduce number of builtin candidates in the set. |
| 3529 | // Add volatile version only if there are conversions to a volatile type. |
| 3530 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3531 | // Volatile version |
| 3532 | ParamTypes[0] |
| 3533 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3534 | if (NumArgs == 1) |
| 3535 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3536 | else |
| 3537 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3538 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | // C++ [over.built]p5: |
| 3542 | // |
| 3543 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3544 | // cv-unqualified object type, and VQ is either volatile or |
| 3545 | // empty, there exist candidate operator functions of the form |
| 3546 | // |
| 3547 | // T*VQ& operator++(T*VQ&); |
| 3548 | // T*VQ& operator--(T*VQ&); |
| 3549 | // T* operator++(T*VQ&, int); |
| 3550 | // T* operator--(T*VQ&, int); |
| 3551 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3552 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3553 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3554 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3555 | continue; |
| 3556 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | QualType ParamTypes[2] = { |
| 3558 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3559 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3561 | // Without volatile |
| 3562 | if (NumArgs == 1) |
| 3563 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3564 | else |
| 3565 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3566 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3567 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3568 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3569 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3570 | ParamTypes[0] |
| 3571 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3572 | if (NumArgs == 1) |
| 3573 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3574 | else |
| 3575 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3576 | } |
| 3577 | } |
| 3578 | break; |
| 3579 | |
| 3580 | UnaryStar: |
| 3581 | // C++ [over.built]p6: |
| 3582 | // For every cv-qualified or cv-unqualified object type T, there |
| 3583 | // exist candidate operator functions of the form |
| 3584 | // |
| 3585 | // T& operator*(T*); |
| 3586 | // |
| 3587 | // C++ [over.built]p7: |
| 3588 | // For every function type T, there exist candidate operator |
| 3589 | // functions of the form |
| 3590 | // T& operator*(T*); |
| 3591 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3592 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3593 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3594 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3596 | &ParamTy, Args, 1, CandidateSet); |
| 3597 | } |
| 3598 | break; |
| 3599 | |
| 3600 | UnaryPlus: |
| 3601 | // C++ [over.built]p8: |
| 3602 | // For every type T, there exist candidate operator functions of |
| 3603 | // the form |
| 3604 | // |
| 3605 | // T* operator+(T*); |
| 3606 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3607 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3608 | QualType ParamTy = *Ptr; |
| 3609 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3612 | // Fall through |
| 3613 | |
| 3614 | UnaryMinus: |
| 3615 | // C++ [over.built]p9: |
| 3616 | // For every promoted arithmetic type T, there exist candidate |
| 3617 | // operator functions of the form |
| 3618 | // |
| 3619 | // T operator+(T); |
| 3620 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3622 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3623 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3624 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3625 | } |
| 3626 | break; |
| 3627 | |
| 3628 | case OO_Tilde: |
| 3629 | // C++ [over.built]p10: |
| 3630 | // For every promoted integral type T, there exist candidate |
| 3631 | // operator functions of the form |
| 3632 | // |
| 3633 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3635 | Int < LastPromotedIntegralType; ++Int) { |
| 3636 | QualType IntTy = ArithmeticTypes[Int]; |
| 3637 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3638 | } |
| 3639 | break; |
| 3640 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3641 | case OO_New: |
| 3642 | case OO_Delete: |
| 3643 | case OO_Array_New: |
| 3644 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3645 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3646 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3647 | break; |
| 3648 | |
| 3649 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3650 | UnaryAmp: |
| 3651 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3652 | // C++ [over.match.oper]p3: |
| 3653 | // -- For the operator ',', the unary operator '&', or the |
| 3654 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3655 | break; |
| 3656 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3657 | case OO_EqualEqual: |
| 3658 | case OO_ExclaimEqual: |
| 3659 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | // For every pointer to member type T, there exist candidate operator |
| 3661 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3662 | // |
| 3663 | // bool operator==(T,T); |
| 3664 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3666 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3667 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3668 | MemPtr != MemPtrEnd; |
| 3669 | ++MemPtr) { |
| 3670 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3671 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3672 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3674 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3676 | case OO_Less: |
| 3677 | case OO_Greater: |
| 3678 | case OO_LessEqual: |
| 3679 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3680 | // C++ [over.built]p15: |
| 3681 | // |
| 3682 | // For every pointer or enumeration type T, there exist |
| 3683 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3685 | // bool operator<(T, T); |
| 3686 | // bool operator>(T, T); |
| 3687 | // bool operator<=(T, T); |
| 3688 | // bool operator>=(T, T); |
| 3689 | // bool operator==(T, T); |
| 3690 | // bool operator!=(T, T); |
| 3691 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3692 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3693 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3694 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3695 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3697 | = CandidateTypes.enumeration_begin(); |
| 3698 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3699 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3700 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3701 | } |
| 3702 | |
| 3703 | // Fall through. |
| 3704 | isComparison = true; |
| 3705 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3706 | BinaryPlus: |
| 3707 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3708 | if (!isComparison) { |
| 3709 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3710 | |
| 3711 | // C++ [over.built]p13: |
| 3712 | // |
| 3713 | // For every cv-qualified or cv-unqualified object type T |
| 3714 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3715 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3716 | // T* operator+(T*, ptrdiff_t); |
| 3717 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3718 | // T* operator-(T*, ptrdiff_t); |
| 3719 | // T* operator+(ptrdiff_t, T*); |
| 3720 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3721 | // |
| 3722 | // C++ [over.built]p14: |
| 3723 | // |
| 3724 | // For every T, where T is a pointer to object type, there |
| 3725 | // exist candidate operator functions of the form |
| 3726 | // |
| 3727 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3729 | = CandidateTypes.pointer_begin(); |
| 3730 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3731 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3732 | |
| 3733 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3734 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3735 | |
| 3736 | if (Op == OO_Plus) { |
| 3737 | // T* operator+(ptrdiff_t, T*); |
| 3738 | ParamTypes[0] = ParamTypes[1]; |
| 3739 | ParamTypes[1] = *Ptr; |
| 3740 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3741 | } else { |
| 3742 | // ptrdiff_t operator-(T, T); |
| 3743 | ParamTypes[1] = *Ptr; |
| 3744 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3745 | Args, 2, CandidateSet); |
| 3746 | } |
| 3747 | } |
| 3748 | } |
| 3749 | // Fall through |
| 3750 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3751 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3752 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3753 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3754 | // C++ [over.built]p12: |
| 3755 | // |
| 3756 | // For every pair of promoted arithmetic types L and R, there |
| 3757 | // exist candidate operator functions of the form |
| 3758 | // |
| 3759 | // LR operator*(L, R); |
| 3760 | // LR operator/(L, R); |
| 3761 | // LR operator+(L, R); |
| 3762 | // LR operator-(L, R); |
| 3763 | // bool operator<(L, R); |
| 3764 | // bool operator>(L, R); |
| 3765 | // bool operator<=(L, R); |
| 3766 | // bool operator>=(L, R); |
| 3767 | // bool operator==(L, R); |
| 3768 | // bool operator!=(L, R); |
| 3769 | // |
| 3770 | // where LR is the result of the usual arithmetic conversions |
| 3771 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3772 | // |
| 3773 | // C++ [over.built]p24: |
| 3774 | // |
| 3775 | // For every pair of promoted arithmetic types L and R, there exist |
| 3776 | // candidate operator functions of the form |
| 3777 | // |
| 3778 | // LR operator?(bool, L, R); |
| 3779 | // |
| 3780 | // where LR is the result of the usual arithmetic conversions |
| 3781 | // between types L and R. |
| 3782 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3784 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3785 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3786 | Right < LastPromotedArithmeticType; ++Right) { |
| 3787 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3788 | QualType Result |
| 3789 | = isComparison |
| 3790 | ? Context.BoolTy |
| 3791 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3792 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3793 | } |
| 3794 | } |
| 3795 | break; |
| 3796 | |
| 3797 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3798 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3799 | case OO_Caret: |
| 3800 | case OO_Pipe: |
| 3801 | case OO_LessLess: |
| 3802 | case OO_GreaterGreater: |
| 3803 | // C++ [over.built]p17: |
| 3804 | // |
| 3805 | // For every pair of promoted integral types L and R, there |
| 3806 | // exist candidate operator functions of the form |
| 3807 | // |
| 3808 | // LR operator%(L, R); |
| 3809 | // LR operator&(L, R); |
| 3810 | // LR operator^(L, R); |
| 3811 | // LR operator|(L, R); |
| 3812 | // L operator<<(L, R); |
| 3813 | // L operator>>(L, R); |
| 3814 | // |
| 3815 | // where LR is the result of the usual arithmetic conversions |
| 3816 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3818 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3820 | Right < LastPromotedIntegralType; ++Right) { |
| 3821 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3822 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3823 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3824 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3825 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3826 | } |
| 3827 | } |
| 3828 | break; |
| 3829 | |
| 3830 | case OO_Equal: |
| 3831 | // C++ [over.built]p20: |
| 3832 | // |
| 3833 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3834 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3835 | // empty, there exist candidate operator functions of the form |
| 3836 | // |
| 3837 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3838 | for (BuiltinCandidateTypeSet::iterator |
| 3839 | Enum = CandidateTypes.enumeration_begin(), |
| 3840 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3841 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3843 | CandidateSet); |
| 3844 | for (BuiltinCandidateTypeSet::iterator |
| 3845 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3846 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3847 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3849 | CandidateSet); |
| 3850 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3851 | |
| 3852 | case OO_PlusEqual: |
| 3853 | case OO_MinusEqual: |
| 3854 | // C++ [over.built]p19: |
| 3855 | // |
| 3856 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3857 | // volatile or empty, there exist candidate operator functions |
| 3858 | // of the form |
| 3859 | // |
| 3860 | // T*VQ& operator=(T*VQ&, T*); |
| 3861 | // |
| 3862 | // C++ [over.built]p21: |
| 3863 | // |
| 3864 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3865 | // cv-unqualified object type and VQ is either volatile or |
| 3866 | // empty, there exist candidate operator functions of the form |
| 3867 | // |
| 3868 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3869 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3870 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3871 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3872 | QualType ParamTypes[2]; |
| 3873 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3874 | |
| 3875 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3876 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3877 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3878 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3879 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3880 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3881 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3882 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3883 | ParamTypes[0] |
| 3884 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3885 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3886 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3887 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3888 | } |
| 3889 | // Fall through. |
| 3890 | |
| 3891 | case OO_StarEqual: |
| 3892 | case OO_SlashEqual: |
| 3893 | // C++ [over.built]p18: |
| 3894 | // |
| 3895 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3896 | // VQ is either volatile or empty, and R is a promoted |
| 3897 | // arithmetic type, there exist candidate operator functions of |
| 3898 | // the form |
| 3899 | // |
| 3900 | // VQ L& operator=(VQ L&, R); |
| 3901 | // VQ L& operator*=(VQ L&, R); |
| 3902 | // VQ L& operator/=(VQ L&, R); |
| 3903 | // VQ L& operator+=(VQ L&, R); |
| 3904 | // VQ L& operator-=(VQ L&, R); |
| 3905 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3907 | Right < LastPromotedArithmeticType; ++Right) { |
| 3908 | QualType ParamTypes[2]; |
| 3909 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3910 | |
| 3911 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3912 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3913 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3914 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3915 | |
| 3916 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3917 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3918 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3919 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3920 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3921 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3922 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | break; |
| 3926 | |
| 3927 | case OO_PercentEqual: |
| 3928 | case OO_LessLessEqual: |
| 3929 | case OO_GreaterGreaterEqual: |
| 3930 | case OO_AmpEqual: |
| 3931 | case OO_CaretEqual: |
| 3932 | case OO_PipeEqual: |
| 3933 | // C++ [over.built]p22: |
| 3934 | // |
| 3935 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3936 | // is either volatile or empty, and R is a promoted integral |
| 3937 | // type, there exist candidate operator functions of the form |
| 3938 | // |
| 3939 | // VQ L& operator%=(VQ L&, R); |
| 3940 | // VQ L& operator<<=(VQ L&, R); |
| 3941 | // VQ L& operator>>=(VQ L&, R); |
| 3942 | // VQ L& operator&=(VQ L&, R); |
| 3943 | // VQ L& operator^=(VQ L&, R); |
| 3944 | // VQ L& operator|=(VQ L&, R); |
| 3945 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3947 | Right < LastPromotedIntegralType; ++Right) { |
| 3948 | QualType ParamTypes[2]; |
| 3949 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3950 | |
| 3951 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3952 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3953 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3954 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3955 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3956 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3957 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3958 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3959 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3960 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3961 | } |
| 3962 | } |
| 3963 | break; |
| 3964 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3965 | case OO_Exclaim: { |
| 3966 | // C++ [over.operator]p23: |
| 3967 | // |
| 3968 | // There also exist candidate operator functions of the form |
| 3969 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3971 | // bool operator&&(bool, bool); [BELOW] |
| 3972 | // bool operator||(bool, bool); [BELOW] |
| 3973 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3974 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3975 | /*IsAssignmentOperator=*/false, |
| 3976 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3977 | break; |
| 3978 | } |
| 3979 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3980 | case OO_AmpAmp: |
| 3981 | case OO_PipePipe: { |
| 3982 | // C++ [over.operator]p23: |
| 3983 | // |
| 3984 | // There also exist candidate operator functions of the form |
| 3985 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3986 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3987 | // bool operator&&(bool, bool); |
| 3988 | // bool operator||(bool, bool); |
| 3989 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3990 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3991 | /*IsAssignmentOperator=*/false, |
| 3992 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3993 | break; |
| 3994 | } |
| 3995 | |
| 3996 | case OO_Subscript: |
| 3997 | // C++ [over.built]p13: |
| 3998 | // |
| 3999 | // For every cv-qualified or cv-unqualified object type T there |
| 4000 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4002 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4003 | // T& operator[](T*, ptrdiff_t); |
| 4004 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4005 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4006 | // T& operator[](ptrdiff_t, T*); |
| 4007 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4008 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4009 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4010 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4011 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4012 | |
| 4013 | // T& operator[](T*, ptrdiff_t) |
| 4014 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4015 | |
| 4016 | // T& operator[](ptrdiff_t, T*); |
| 4017 | ParamTypes[0] = ParamTypes[1]; |
| 4018 | ParamTypes[1] = *Ptr; |
| 4019 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4020 | } |
| 4021 | break; |
| 4022 | |
| 4023 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4024 | // C++ [over.built]p11: |
| 4025 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4026 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4027 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4028 | // there exist candidate operator functions of the form |
| 4029 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4030 | // where CV12 is the union of CV1 and CV2. |
| 4031 | { |
| 4032 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4033 | CandidateTypes.pointer_begin(); |
| 4034 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4035 | QualType C1Ty = (*Ptr); |
| 4036 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4037 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4038 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4039 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4040 | if (!isa<RecordType>(C1)) |
| 4041 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4042 | // heuristic to reduce number of builtin candidates in the set. |
| 4043 | // Add volatile/restrict version only if there are conversions to a |
| 4044 | // volatile/restrict type. |
| 4045 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4046 | continue; |
| 4047 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4048 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4049 | } |
| 4050 | for (BuiltinCandidateTypeSet::iterator |
| 4051 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4052 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4053 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4054 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4055 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4056 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4057 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4058 | break; |
| 4059 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4060 | // build CV12 T& |
| 4061 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4062 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4063 | T.isVolatileQualified()) |
| 4064 | continue; |
| 4065 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4066 | T.isRestrictQualified()) |
| 4067 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4068 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4069 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4070 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4071 | } |
| 4072 | } |
| 4073 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4074 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4075 | |
| 4076 | case OO_Conditional: |
| 4077 | // Note that we don't consider the first argument, since it has been |
| 4078 | // contextually converted to bool long ago. The candidates below are |
| 4079 | // therefore added as binary. |
| 4080 | // |
| 4081 | // C++ [over.built]p24: |
| 4082 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4083 | // there exist candidate operator functions of the form |
| 4084 | // |
| 4085 | // T operator?(bool, T, T); |
| 4086 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4087 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4088 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4089 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4090 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4091 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4092 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4093 | CandidateTypes.member_pointer_begin(), |
| 4094 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4095 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4096 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4097 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4098 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4102 | /// \brief Add function candidates found via argument-dependent lookup |
| 4103 | /// to the set of overloading candidates. |
| 4104 | /// |
| 4105 | /// This routine performs argument-dependent name lookup based on the |
| 4106 | /// given function name (which may also be an operator name) and adds |
| 4107 | /// all of the overload candidates found by ADL to the overload |
| 4108 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4109 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4110 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 4111 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4112 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4113 | OverloadCandidateSet& CandidateSet, |
| 4114 | bool PartialOverloading) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4115 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4117 | // FIXME: Should we be trafficking in canonical function decls throughout? |
| 4118 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4119 | // Record all of the function candidates that we've already |
| 4120 | // added to the overload set, so that we don't add those same |
| 4121 | // candidates a second time. |
| 4122 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4123 | CandEnd = CandidateSet.end(); |
| 4124 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4125 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4126 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4127 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4128 | Functions.insert(FunTmpl); |
| 4129 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4130 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4131 | // FIXME: Pass in the explicit template arguments? |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 4132 | ArgumentDependentLookup(Name, /*Operator*/false, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4134 | // Erase all of the candidates we already knew about. |
| 4135 | // FIXME: This is suboptimal. Is there a better way? |
| 4136 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4137 | CandEnd = CandidateSet.end(); |
| 4138 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4139 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4140 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4141 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 4142 | Functions.erase(FunTmpl); |
| 4143 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4144 | |
| 4145 | // For each of the ADL candidates we found, add it to the overload |
| 4146 | // set. |
| 4147 | for (FunctionSet::iterator Func = Functions.begin(), |
| 4148 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4149 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4150 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4151 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4152 | continue; |
| 4153 | |
| 4154 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
| 4155 | false, false, PartialOverloading); |
| 4156 | } else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4158 | ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4159 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4160 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4163 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4164 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4166 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4167 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4168 | // Define viable functions to be better candidates than non-viable |
| 4169 | // functions. |
| 4170 | if (!Cand2.Viable) |
| 4171 | return Cand1.Viable; |
| 4172 | else if (!Cand1.Viable) |
| 4173 | return false; |
| 4174 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4175 | // C++ [over.match.best]p1: |
| 4176 | // |
| 4177 | // -- if F is a static member function, ICS1(F) is defined such |
| 4178 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4179 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4180 | // better nor worse than ICS1(F). |
| 4181 | unsigned StartArg = 0; |
| 4182 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4183 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4184 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4185 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | // A viable function F1 is defined to be a better function than another |
| 4187 | // 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] | 4188 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4189 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4190 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4191 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4192 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4193 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4194 | Cand2.Conversions[ArgIdx])) { |
| 4195 | case ImplicitConversionSequence::Better: |
| 4196 | // Cand1 has a better conversion sequence. |
| 4197 | HasBetterConversion = true; |
| 4198 | break; |
| 4199 | |
| 4200 | case ImplicitConversionSequence::Worse: |
| 4201 | // Cand1 can't be better than Cand2. |
| 4202 | return false; |
| 4203 | |
| 4204 | case ImplicitConversionSequence::Indistinguishable: |
| 4205 | // Do nothing. |
| 4206 | break; |
| 4207 | } |
| 4208 | } |
| 4209 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4211 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4212 | if (HasBetterConversion) |
| 4213 | return true; |
| 4214 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4216 | // specialization, or, if not that, |
| 4217 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4218 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4219 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | |
| 4221 | // -- F1 and F2 are function template specializations, and the function |
| 4222 | // template for F1 is more specialized than the template for F2 |
| 4223 | // 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] | 4224 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4225 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4226 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4227 | if (FunctionTemplateDecl *BetterTemplate |
| 4228 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4229 | Cand2.Function->getPrimaryTemplate(), |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4230 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4231 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4232 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4233 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4234 | // -- the context is an initialization by user-defined conversion |
| 4235 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4236 | // from the return type of F1 to the destination type (i.e., |
| 4237 | // the type of the entity being initialized) is a better |
| 4238 | // conversion sequence than the standard conversion sequence |
| 4239 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4240 | if (Cand1.Function && Cand2.Function && |
| 4241 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4242 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4243 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4244 | Cand2.FinalConversion)) { |
| 4245 | case ImplicitConversionSequence::Better: |
| 4246 | // Cand1 has a better conversion sequence. |
| 4247 | return true; |
| 4248 | |
| 4249 | case ImplicitConversionSequence::Worse: |
| 4250 | // Cand1 can't be better than Cand2. |
| 4251 | return false; |
| 4252 | |
| 4253 | case ImplicitConversionSequence::Indistinguishable: |
| 4254 | // Do nothing |
| 4255 | break; |
| 4256 | } |
| 4257 | } |
| 4258 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4259 | return false; |
| 4260 | } |
| 4261 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4263 | /// within an overload candidate set. |
| 4264 | /// |
| 4265 | /// \param CandidateSet the set of candidate functions. |
| 4266 | /// |
| 4267 | /// \param Loc the location of the function name (or operator symbol) for |
| 4268 | /// which overload resolution occurs. |
| 4269 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4271 | /// function, Best points to the candidate function found. |
| 4272 | /// |
| 4273 | /// \returns The result of overload resolution. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4274 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4275 | SourceLocation Loc, |
| 4276 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4277 | // Find the best viable function. |
| 4278 | Best = CandidateSet.end(); |
| 4279 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4280 | Cand != CandidateSet.end(); ++Cand) { |
| 4281 | if (Cand->Viable) { |
| 4282 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 4283 | Best = Cand; |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | // If we didn't find any viable functions, abort. |
| 4288 | if (Best == CandidateSet.end()) |
| 4289 | return OR_No_Viable_Function; |
| 4290 | |
| 4291 | // Make sure that this function is better than every other viable |
| 4292 | // function. If not, we have an ambiguity. |
| 4293 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4294 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4295 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4296 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4297 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 4298 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4299 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4300 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4303 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4304 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4306 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4307 | return OR_Deleted; |
| 4308 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4309 | // C++ [basic.def.odr]p2: |
| 4310 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4312 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4313 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4314 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4315 | if (Best->Function) |
| 4316 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4317 | return OR_Success; |
| 4318 | } |
| 4319 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4320 | namespace { |
| 4321 | |
| 4322 | enum OverloadCandidateKind { |
| 4323 | oc_function, |
| 4324 | oc_method, |
| 4325 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4326 | oc_function_template, |
| 4327 | oc_method_template, |
| 4328 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4329 | oc_implicit_default_constructor, |
| 4330 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4331 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4332 | }; |
| 4333 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4334 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4335 | FunctionDecl *Fn, |
| 4336 | std::string &Description) { |
| 4337 | bool isTemplate = false; |
| 4338 | |
| 4339 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4340 | isTemplate = true; |
| 4341 | Description = S.getTemplateArgumentBindingsText( |
| 4342 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4343 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4344 | |
| 4345 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4346 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4347 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4348 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4349 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4350 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4354 | // This actually gets spelled 'candidate function' for now, but |
| 4355 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4356 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4357 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4358 | |
| 4359 | assert(Meth->isCopyAssignment() |
| 4360 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4361 | return oc_implicit_copy_assignment; |
| 4362 | } |
| 4363 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4364 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | } // end anonymous namespace |
| 4368 | |
| 4369 | // Notes the location of an overload candidate. |
| 4370 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4371 | std::string FnDesc; |
| 4372 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4373 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4374 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4377 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4378 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4379 | /// target types of the conversion. |
| 4380 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4381 | SourceLocation CaretLoc, |
| 4382 | const PartialDiagnostic &PDiag) { |
| 4383 | Diag(CaretLoc, PDiag) |
| 4384 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4385 | for (AmbiguousConversionSequence::const_iterator |
| 4386 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4387 | NoteOverloadCandidate(*I); |
| 4388 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4391 | namespace { |
| 4392 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4393 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4394 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4395 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4396 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4397 | FunctionDecl *Fn = Cand->Function; |
| 4398 | |
| 4399 | // There's a conversion slot for the object argument if this is a |
| 4400 | // non-constructor method. Note that 'I' corresponds the |
| 4401 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4402 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4403 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4404 | if (I == 0) |
| 4405 | isObjectArgument = true; |
| 4406 | else |
| 4407 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4410 | std::string FnDesc; |
| 4411 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4412 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4413 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4414 | QualType FromTy = Conv.Bad.getFromType(); |
| 4415 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4416 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4417 | // Do some hand-waving analysis to see if the non-viability is due |
| 4418 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4419 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4420 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4421 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4422 | CToTy = RT->getPointeeType(); |
| 4423 | else { |
| 4424 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4425 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4426 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4427 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4428 | } |
| 4429 | |
| 4430 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4431 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4432 | // It is dumb that we have to do this here. |
| 4433 | while (isa<ArrayType>(CFromTy)) |
| 4434 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4435 | while (isa<ArrayType>(CToTy)) |
| 4436 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4437 | |
| 4438 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4439 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4440 | |
| 4441 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4442 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4443 | << (unsigned) FnKind << FnDesc |
| 4444 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4445 | << FromTy |
| 4446 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4447 | << (unsigned) isObjectArgument << I+1; |
| 4448 | return; |
| 4449 | } |
| 4450 | |
| 4451 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4452 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4453 | |
| 4454 | if (isObjectArgument) { |
| 4455 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4456 | << (unsigned) FnKind << FnDesc |
| 4457 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4458 | << FromTy << (CVR - 1); |
| 4459 | } else { |
| 4460 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4461 | << (unsigned) FnKind << FnDesc |
| 4462 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4463 | << FromTy << (CVR - 1) << I+1; |
| 4464 | } |
| 4465 | return; |
| 4466 | } |
| 4467 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4468 | // Diagnose references or pointers to incomplete types differently, |
| 4469 | // since it's far from impossible that the incompleteness triggered |
| 4470 | // the failure. |
| 4471 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 4472 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 4473 | TempFromTy = PTy->getPointeeType(); |
| 4474 | if (TempFromTy->isIncompleteType()) { |
| 4475 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 4476 | << (unsigned) FnKind << FnDesc |
| 4477 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4478 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4479 | return; |
| 4480 | } |
| 4481 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4482 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4483 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4484 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4485 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 4486 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4490 | unsigned NumFormalArgs) { |
| 4491 | // TODO: treat calls to a missing default constructor as a special case |
| 4492 | |
| 4493 | FunctionDecl *Fn = Cand->Function; |
| 4494 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4495 | |
| 4496 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4497 | |
| 4498 | // at least / at most / exactly |
| 4499 | unsigned mode, modeCount; |
| 4500 | if (NumFormalArgs < MinParams) { |
| 4501 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4502 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4503 | mode = 0; // "at least" |
| 4504 | else |
| 4505 | mode = 2; // "exactly" |
| 4506 | modeCount = MinParams; |
| 4507 | } else { |
| 4508 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4509 | if (MinParams != FnTy->getNumArgs()) |
| 4510 | mode = 1; // "at most" |
| 4511 | else |
| 4512 | mode = 2; // "exactly" |
| 4513 | modeCount = FnTy->getNumArgs(); |
| 4514 | } |
| 4515 | |
| 4516 | std::string Description; |
| 4517 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4518 | |
| 4519 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4520 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4524 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4525 | FunctionDecl *Fn = Cand->Function; |
| 4526 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4527 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4528 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4529 | std::string FnDesc; |
| 4530 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4531 | |
| 4532 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4533 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4534 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4537 | // We don't really have anything else to say about viable candidates. |
| 4538 | if (Cand->Viable) { |
| 4539 | S.NoteOverloadCandidate(Fn); |
| 4540 | return; |
| 4541 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4542 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4543 | switch (Cand->FailureKind) { |
| 4544 | case ovl_fail_too_many_arguments: |
| 4545 | case ovl_fail_too_few_arguments: |
| 4546 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4547 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4548 | case ovl_fail_bad_deduction: |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4549 | case ovl_fail_trivial_conversion: |
| 4550 | case ovl_fail_bad_final_conversion: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4551 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4552 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4553 | case ovl_fail_bad_conversion: |
| 4554 | for (unsigned I = 0, N = Cand->Conversions.size(); I != N; ++I) |
| 4555 | if (Cand->Conversions[I].isBad()) |
| 4556 | return DiagnoseBadConversion(S, Cand, I); |
| 4557 | |
| 4558 | // FIXME: this currently happens when we're called from SemaInit |
| 4559 | // when user-conversion overload fails. Figure out how to handle |
| 4560 | // those conditions and diagnose them well. |
| 4561 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4562 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4566 | // Desugar the type of the surrogate down to a function type, |
| 4567 | // retaining as many typedefs as possible while still showing |
| 4568 | // the function type (and, therefore, its parameter types). |
| 4569 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4570 | bool isLValueReference = false; |
| 4571 | bool isRValueReference = false; |
| 4572 | bool isPointer = false; |
| 4573 | if (const LValueReferenceType *FnTypeRef = |
| 4574 | FnType->getAs<LValueReferenceType>()) { |
| 4575 | FnType = FnTypeRef->getPointeeType(); |
| 4576 | isLValueReference = true; |
| 4577 | } else if (const RValueReferenceType *FnTypeRef = |
| 4578 | FnType->getAs<RValueReferenceType>()) { |
| 4579 | FnType = FnTypeRef->getPointeeType(); |
| 4580 | isRValueReference = true; |
| 4581 | } |
| 4582 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4583 | FnType = FnTypePtr->getPointeeType(); |
| 4584 | isPointer = true; |
| 4585 | } |
| 4586 | // Desugar down to a function type. |
| 4587 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4588 | // Reconstruct the pointer/reference as appropriate. |
| 4589 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4590 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4591 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4592 | |
| 4593 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4594 | << FnType; |
| 4595 | } |
| 4596 | |
| 4597 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 4598 | const char *Opc, |
| 4599 | SourceLocation OpLoc, |
| 4600 | OverloadCandidate *Cand) { |
| 4601 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 4602 | std::string TypeStr("operator"); |
| 4603 | TypeStr += Opc; |
| 4604 | TypeStr += "("; |
| 4605 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4606 | if (Cand->Conversions.size() == 1) { |
| 4607 | TypeStr += ")"; |
| 4608 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 4609 | } else { |
| 4610 | TypeStr += ", "; |
| 4611 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4612 | TypeStr += ")"; |
| 4613 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 4614 | } |
| 4615 | } |
| 4616 | |
| 4617 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 4618 | OverloadCandidate *Cand) { |
| 4619 | unsigned NoOperands = Cand->Conversions.size(); |
| 4620 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 4621 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4622 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 4623 | if (!ICS.isAmbiguous()) continue; |
| 4624 | |
| 4625 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
| 4626 | PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
| 4629 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4630 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 4631 | if (Cand->Function) |
| 4632 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 4633 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4634 | return Cand->Surrogate->getLocation(); |
| 4635 | return SourceLocation(); |
| 4636 | } |
| 4637 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4638 | struct CompareOverloadCandidatesForDisplay { |
| 4639 | Sema &S; |
| 4640 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4641 | |
| 4642 | bool operator()(const OverloadCandidate *L, |
| 4643 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 4644 | // Fast-path this check. |
| 4645 | if (L == R) return false; |
| 4646 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4647 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4648 | if (L->Viable) { |
| 4649 | if (!R->Viable) return true; |
| 4650 | |
| 4651 | // TODO: introduce a tri-valued comparison for overload |
| 4652 | // candidates. Would be more worthwhile if we had a sort |
| 4653 | // that could exploit it. |
| 4654 | if (S.isBetterOverloadCandidate(*L, *R)) return true; |
| 4655 | if (S.isBetterOverloadCandidate(*R, *L)) return false; |
| 4656 | } else if (R->Viable) |
| 4657 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4658 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4659 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4660 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4661 | // Criteria by which we can sort non-viable candidates: |
| 4662 | if (!L->Viable) { |
| 4663 | // 1. Arity mismatches come after other candidates. |
| 4664 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 4665 | L->FailureKind == ovl_fail_too_few_arguments) |
| 4666 | return false; |
| 4667 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 4668 | R->FailureKind == ovl_fail_too_few_arguments) |
| 4669 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4670 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4671 | // 2. Bad conversions come first and are ordered by the number |
| 4672 | // of bad conversions and quality of good conversions. |
| 4673 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 4674 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 4675 | return true; |
| 4676 | |
| 4677 | // If there's any ordering between the defined conversions... |
| 4678 | // FIXME: this might not be transitive. |
| 4679 | assert(L->Conversions.size() == R->Conversions.size()); |
| 4680 | |
| 4681 | int leftBetter = 0; |
| 4682 | for (unsigned I = 0, E = L->Conversions.size(); I != E; ++I) { |
| 4683 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 4684 | R->Conversions[I])) { |
| 4685 | case ImplicitConversionSequence::Better: |
| 4686 | leftBetter++; |
| 4687 | break; |
| 4688 | |
| 4689 | case ImplicitConversionSequence::Worse: |
| 4690 | leftBetter--; |
| 4691 | break; |
| 4692 | |
| 4693 | case ImplicitConversionSequence::Indistinguishable: |
| 4694 | break; |
| 4695 | } |
| 4696 | } |
| 4697 | if (leftBetter > 0) return true; |
| 4698 | if (leftBetter < 0) return false; |
| 4699 | |
| 4700 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 4701 | return false; |
| 4702 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4703 | // TODO: others? |
| 4704 | } |
| 4705 | |
| 4706 | // Sort everything else by location. |
| 4707 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 4708 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 4709 | |
| 4710 | // Put candidates without locations (e.g. builtins) at the end. |
| 4711 | if (LLoc.isInvalid()) return false; |
| 4712 | if (RLoc.isInvalid()) return true; |
| 4713 | |
| 4714 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4715 | } |
| 4716 | }; |
| 4717 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4718 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 4719 | /// computes up to the first |
| 4720 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 4721 | Expr **Args, unsigned NumArgs) { |
| 4722 | assert(!Cand->Viable); |
| 4723 | |
| 4724 | // Don't do anything on failures other than bad conversion. |
| 4725 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 4726 | |
| 4727 | // Skip forward to the first bad conversion. |
| 4728 | unsigned ConvIdx = 0; |
| 4729 | unsigned ConvCount = Cand->Conversions.size(); |
| 4730 | while (true) { |
| 4731 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 4732 | ConvIdx++; |
| 4733 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 4734 | break; |
| 4735 | } |
| 4736 | |
| 4737 | if (ConvIdx == ConvCount) |
| 4738 | return; |
| 4739 | |
| 4740 | // FIXME: these should probably be preserved from the overload |
| 4741 | // operation somehow. |
| 4742 | bool SuppressUserConversions = false; |
| 4743 | bool ForceRValue = false; |
| 4744 | |
| 4745 | const FunctionProtoType* Proto; |
| 4746 | unsigned ArgIdx = ConvIdx; |
| 4747 | |
| 4748 | if (Cand->IsSurrogate) { |
| 4749 | QualType ConvType |
| 4750 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 4751 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4752 | ConvType = ConvPtrType->getPointeeType(); |
| 4753 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 4754 | ArgIdx--; |
| 4755 | } else if (Cand->Function) { |
| 4756 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 4757 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 4758 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 4759 | ArgIdx--; |
| 4760 | } else { |
| 4761 | // Builtin binary operator with a bad first conversion. |
| 4762 | assert(ConvCount <= 3); |
| 4763 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 4764 | Cand->Conversions[ConvIdx] |
| 4765 | = S.TryCopyInitialization(Args[ConvIdx], |
| 4766 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 4767 | SuppressUserConversions, ForceRValue, |
| 4768 | /*InOverloadResolution*/ true); |
| 4769 | return; |
| 4770 | } |
| 4771 | |
| 4772 | // Fill in the rest of the conversions. |
| 4773 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4774 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 4775 | if (ArgIdx < NumArgsInProto) |
| 4776 | Cand->Conversions[ConvIdx] |
| 4777 | = S.TryCopyInitialization(Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 4778 | SuppressUserConversions, ForceRValue, |
| 4779 | /*InOverloadResolution=*/true); |
| 4780 | else |
| 4781 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 4782 | } |
| 4783 | } |
| 4784 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4785 | } // end anonymous namespace |
| 4786 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4787 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4788 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4789 | /// set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4790 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4791 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4792 | OverloadCandidateDisplayKind OCD, |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 4793 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4794 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4795 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4796 | // Sort the candidates by viability and position. Sorting directly would |
| 4797 | // be prohibitive, so we make a set of pointers and sort those. |
| 4798 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 4799 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 4800 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4801 | LastCand = CandidateSet.end(); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4802 | Cand != LastCand; ++Cand) { |
| 4803 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4804 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4805 | else if (OCD == OCD_AllCandidates) { |
| 4806 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 4807 | Cands.push_back(Cand); |
| 4808 | } |
| 4809 | } |
| 4810 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4811 | std::sort(Cands.begin(), Cands.end(), |
| 4812 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4813 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4814 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4815 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4816 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 4817 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 4818 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4819 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4820 | if (Cand->Function) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4821 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4822 | else if (Cand->IsSurrogate) |
| 4823 | NoteSurrogateCandidate(*this, Cand); |
| 4824 | |
| 4825 | // This a builtin candidate. We do not, in general, want to list |
| 4826 | // every possible builtin candidate. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4827 | else if (Cand->Viable) { |
| 4828 | // Generally we only see ambiguities including viable builtin |
| 4829 | // operators if overload resolution got screwed up by an |
| 4830 | // ambiguous user-defined conversion. |
| 4831 | // |
| 4832 | // FIXME: It's quite possible for different conversions to see |
| 4833 | // different ambiguities, though. |
| 4834 | if (!ReportedAmbiguousConversions) { |
| 4835 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 4836 | ReportedAmbiguousConversions = true; |
| 4837 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4838 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4839 | // If this is a viable builtin, print it. |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4840 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4841 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4842 | } |
| 4843 | } |
| 4844 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4845 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4846 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4847 | /// expression with overloaded function type and @p ToType is the type |
| 4848 | /// we're trying to resolve to. For example: |
| 4849 | /// |
| 4850 | /// @code |
| 4851 | /// int f(double); |
| 4852 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4853 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4854 | /// int (*pfd)(double) = f; // selects f(double) |
| 4855 | /// @endcode |
| 4856 | /// |
| 4857 | /// This routine returns the resulting FunctionDecl if it could be |
| 4858 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4859 | /// routine will emit diagnostics if there is an error. |
| 4860 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4861 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4862 | bool Complain) { |
| 4863 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4864 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4865 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4866 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4867 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4868 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4869 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4870 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4871 | FunctionType = MemTypePtr->getPointeeType(); |
| 4872 | IsMember = true; |
| 4873 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4874 | |
| 4875 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4876 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4877 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4878 | return 0; |
| 4879 | |
| 4880 | // Find the actual overloaded function declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4881 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4882 | // C++ [over.over]p1: |
| 4883 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4884 | // overloaded function name is ignored (5.1). ] |
| 4885 | Expr *OvlExpr = From->IgnoreParens(); |
| 4886 | |
| 4887 | // C++ [over.over]p1: |
| 4888 | // [...] The overloaded function name can be preceded by the & |
| 4889 | // operator. |
| 4890 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 4891 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 4892 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 4893 | } |
| 4894 | |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4895 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4896 | TemplateArgumentListInfo ExplicitTemplateArgs; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4897 | |
| 4898 | llvm::SmallVector<NamedDecl*,8> Fns; |
Anders Carlsson | 7053485 | 2009-10-20 22:53:47 +0000 | [diff] [blame] | 4899 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4900 | // Look into the overloaded expression. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4901 | if (UnresolvedLookupExpr *UL |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4902 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 4903 | Fns.append(UL->decls_begin(), UL->decls_end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 4904 | if (UL->hasExplicitTemplateArgs()) { |
| 4905 | HasExplicitTemplateArgs = true; |
| 4906 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 4907 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4908 | } else if (UnresolvedMemberExpr *ME |
| 4909 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 4910 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 4911 | if (ME->hasExplicitTemplateArgs()) { |
| 4912 | HasExplicitTemplateArgs = true; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4913 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4914 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4915 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4916 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4917 | // If we didn't actually find anything, we're done. |
| 4918 | if (Fns.empty()) |
| 4919 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4920 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4921 | // Look through all of the overloaded functions, searching for one |
| 4922 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4923 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4924 | bool FoundNonTemplateFunction = false; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4925 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 4926 | E = Fns.end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4927 | // Look through any using declarations to find the underlying function. |
| 4928 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 4929 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4930 | // C++ [over.over]p3: |
| 4931 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 4932 | // targets of type "pointer-to-function" or "reference-to-function." |
| 4933 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4934 | // type "pointer-to-member-function." |
| 4935 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4936 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4938 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4940 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4942 | // static when converting to member pointer. |
| 4943 | if (Method->isStatic() == IsMember) |
| 4944 | continue; |
| 4945 | } else if (IsMember) |
| 4946 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4948 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4949 | // If the name is a function template, template argument deduction is |
| 4950 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 4951 | // resulting template argument list is used to generate a single |
| 4952 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4953 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4954 | // 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] | 4955 | FunctionDecl *Specialization = 0; |
| 4956 | TemplateDeductionInfo Info(Context); |
| 4957 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4958 | = DeduceTemplateArguments(FunctionTemplate, |
| 4959 | (HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4960 | FunctionType, Specialization, Info)) { |
| 4961 | // FIXME: make a note of the failed deduction for diagnostics. |
| 4962 | (void)Result; |
| 4963 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4964 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 4965 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4966 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4967 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4968 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4969 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4970 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4971 | |
| 4972 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4975 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4976 | // Skip non-static functions when converting to pointer, and static |
| 4977 | // when converting to member pointer. |
| 4978 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4979 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 4980 | |
| 4981 | // If we have explicit template arguments, skip non-templates. |
| 4982 | if (HasExplicitTemplateArgs) |
| 4983 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4984 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4985 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4986 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4987 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 4988 | QualType ResultTy; |
| 4989 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 4990 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 4991 | ResultTy)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4992 | Matches.insert(cast<FunctionDecl>(FunDecl->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4993 | FoundNonTemplateFunction = true; |
| 4994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4995 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4996 | } |
| 4997 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4998 | // If there were 0 or 1 matches, we're done. |
| 4999 | if (Matches.empty()) |
| 5000 | return 0; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5001 | else if (Matches.size() == 1) { |
| 5002 | FunctionDecl *Result = *Matches.begin(); |
| 5003 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 5004 | return Result; |
| 5005 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5006 | |
| 5007 | // C++ [over.over]p4: |
| 5008 | // If more than one function is selected, [...] |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5009 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5010 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5011 | // [...] and any given function template specialization F1 is |
| 5012 | // eliminated if the set contains a second function template |
| 5013 | // specialization whose function template is more specialized |
| 5014 | // than the function template of F1 according to the partial |
| 5015 | // ordering rules of 14.5.5.2. |
| 5016 | |
| 5017 | // The algorithm specified above is quadratic. We instead use a |
| 5018 | // two-pass algorithm (similar to the one used to identify the |
| 5019 | // best viable function in an overload set) that identifies the |
| 5020 | // best function template (if it exists). |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5021 | llvm::SmallVector<FunctionDecl *, 8> TemplateMatches(Matches.begin(), |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5022 | Matches.end()); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5023 | FunctionDecl *Result = |
| 5024 | getMostSpecialized(TemplateMatches.data(), TemplateMatches.size(), |
| 5025 | TPOC_Other, From->getLocStart(), |
| 5026 | PDiag(), |
| 5027 | PDiag(diag::err_addr_ovl_ambiguous) |
| 5028 | << TemplateMatches[0]->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5029 | PDiag(diag::note_ovl_candidate) |
| 5030 | << (unsigned) oc_function_template); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5031 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 5032 | return Result; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5033 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5034 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5035 | // [...] any function template specializations in the set are |
| 5036 | // eliminated if the set also contains a non-template function, [...] |
| 5037 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
| 5038 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
| 5039 | if ((*M)->getPrimaryTemplate() == 0) |
| 5040 | RemainingMatches.push_back(*M); |
| 5041 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5042 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5043 | // selected function. |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5044 | if (RemainingMatches.size() == 1) { |
| 5045 | FunctionDecl *Result = RemainingMatches.front(); |
| 5046 | MarkDeclarationReferenced(From->getLocStart(), Result); |
| 5047 | return Result; |
| 5048 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5049 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5050 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5051 | // returns (even if we issue the diagnostics here). |
| 5052 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 5053 | << RemainingMatches[0]->getDeclName(); |
| 5054 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5055 | NoteOverloadCandidate(RemainingMatches[I]); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5056 | return 0; |
| 5057 | } |
| 5058 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5059 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5060 | /// resolve that overloaded function expression down to a single function. |
| 5061 | /// |
| 5062 | /// This routine can only resolve template-ids that refer to a single function |
| 5063 | /// template, where that template-id refers to a single template whose template |
| 5064 | /// arguments are either provided by the template-id or have defaults, |
| 5065 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5066 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5067 | // C++ [over.over]p1: |
| 5068 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5069 | // overloaded function name is ignored (5.1). ] |
| 5070 | Expr *OvlExpr = From->IgnoreParens(); |
| 5071 | |
| 5072 | // C++ [over.over]p1: |
| 5073 | // [...] The overloaded function name can be preceded by the & |
| 5074 | // operator. |
| 5075 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 5076 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 5077 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 5078 | } |
| 5079 | |
| 5080 | bool HasExplicitTemplateArgs = false; |
| 5081 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5082 | |
| 5083 | llvm::SmallVector<NamedDecl*,8> Fns; |
| 5084 | |
| 5085 | // Look into the overloaded expression. |
| 5086 | if (UnresolvedLookupExpr *UL |
| 5087 | = dyn_cast<UnresolvedLookupExpr>(OvlExpr)) { |
| 5088 | Fns.append(UL->decls_begin(), UL->decls_end()); |
| 5089 | if (UL->hasExplicitTemplateArgs()) { |
| 5090 | HasExplicitTemplateArgs = true; |
| 5091 | UL->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 5092 | } |
| 5093 | } else if (UnresolvedMemberExpr *ME |
| 5094 | = dyn_cast<UnresolvedMemberExpr>(OvlExpr)) { |
| 5095 | Fns.append(ME->decls_begin(), ME->decls_end()); |
| 5096 | if (ME->hasExplicitTemplateArgs()) { |
| 5097 | HasExplicitTemplateArgs = true; |
| 5098 | ME->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | // If we didn't actually find any template-ids, we're done. |
| 5103 | if (Fns.empty() || !HasExplicitTemplateArgs) |
| 5104 | return 0; |
| 5105 | |
| 5106 | // Look through all of the overloaded functions, searching for one |
| 5107 | // whose type matches exactly. |
| 5108 | FunctionDecl *Matched = 0; |
| 5109 | for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Fns.begin(), |
| 5110 | E = Fns.end(); I != E; ++I) { |
| 5111 | // C++0x [temp.arg.explicit]p3: |
| 5112 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5113 | // where deduction is not done, if a template argument list is |
| 5114 | // specified and it, along with any default template arguments, |
| 5115 | // identifies a single function template specialization, then the |
| 5116 | // template-id is an lvalue for the function template specialization. |
| 5117 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5118 | |
| 5119 | // C++ [over.over]p2: |
| 5120 | // If the name is a function template, template argument deduction is |
| 5121 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5122 | // resulting template argument list is used to generate a single |
| 5123 | // function template specialization, which is added to the set of |
| 5124 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5125 | FunctionDecl *Specialization = 0; |
| 5126 | TemplateDeductionInfo Info(Context); |
| 5127 | if (TemplateDeductionResult Result |
| 5128 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5129 | Specialization, Info)) { |
| 5130 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5131 | (void)Result; |
| 5132 | continue; |
| 5133 | } |
| 5134 | |
| 5135 | // Multiple matches; we can't resolve to a single declaration. |
| 5136 | if (Matched) |
| 5137 | return 0; |
| 5138 | |
| 5139 | Matched = Specialization; |
| 5140 | } |
| 5141 | |
| 5142 | return Matched; |
| 5143 | } |
| 5144 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5145 | /// \brief Add a single candidate to the overload set. |
| 5146 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5147 | NamedDecl *Callee, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5148 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5149 | Expr **Args, unsigned NumArgs, |
| 5150 | OverloadCandidateSet &CandidateSet, |
| 5151 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5152 | if (isa<UsingShadowDecl>(Callee)) |
| 5153 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5154 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5155 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5156 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5157 | S.AddOverloadCandidate(Func, Args, NumArgs, CandidateSet, false, false, |
| 5158 | PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5159 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | if (FunctionTemplateDecl *FuncTemplate |
| 5163 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5164 | S.AddTemplateOverloadCandidate(FuncTemplate, ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5165 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5166 | return; |
| 5167 | } |
| 5168 | |
| 5169 | assert(false && "unhandled case in overloaded call candidate"); |
| 5170 | |
| 5171 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5175 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5176 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5177 | Expr **Args, unsigned NumArgs, |
| 5178 | OverloadCandidateSet &CandidateSet, |
| 5179 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5180 | |
| 5181 | #ifndef NDEBUG |
| 5182 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5183 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5184 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5185 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5186 | // and let Y be the lookup set produced by argument dependent |
| 5187 | // lookup (defined as follows). If X contains |
| 5188 | // |
| 5189 | // -- a declaration of a class member, or |
| 5190 | // |
| 5191 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5192 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5193 | // |
| 5194 | // -- a declaration that is neither a function or a function |
| 5195 | // template |
| 5196 | // |
| 5197 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5198 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5199 | if (ULE->requiresADL()) { |
| 5200 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5201 | E = ULE->decls_end(); I != E; ++I) { |
| 5202 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5203 | assert(isa<UsingShadowDecl>(*I) || |
| 5204 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5205 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5206 | } |
| 5207 | } |
| 5208 | #endif |
| 5209 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5210 | // It would be nice to avoid this copy. |
| 5211 | TemplateArgumentListInfo TABuffer; |
| 5212 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5213 | if (ULE->hasExplicitTemplateArgs()) { |
| 5214 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5215 | ExplicitTemplateArgs = &TABuffer; |
| 5216 | } |
| 5217 | |
| 5218 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5219 | E = ULE->decls_end(); I != E; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5220 | AddOverloadedCallCandidate(*this, *I, ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5221 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5222 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5223 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5224 | if (ULE->requiresADL()) |
| 5225 | AddArgumentDependentLookupCandidates(ULE->getName(), Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5226 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5227 | CandidateSet, |
| 5228 | PartialOverloading); |
| 5229 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5230 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5231 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5232 | Expr **Args, unsigned NumArgs) { |
| 5233 | Fn->Destroy(SemaRef.Context); |
| 5234 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5235 | Args[Arg]->Destroy(SemaRef.Context); |
| 5236 | return SemaRef.ExprError(); |
| 5237 | } |
| 5238 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5239 | /// Attempts to recover from a call where no functions were found. |
| 5240 | /// |
| 5241 | /// Returns true if new candidates were found. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5242 | static Sema::OwningExprResult |
| 5243 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 5244 | UnresolvedLookupExpr *ULE, |
| 5245 | SourceLocation LParenLoc, |
| 5246 | Expr **Args, unsigned NumArgs, |
| 5247 | SourceLocation *CommaLocs, |
| 5248 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5249 | |
| 5250 | CXXScopeSpec SS; |
| 5251 | if (ULE->getQualifier()) { |
| 5252 | SS.setScopeRep(ULE->getQualifier()); |
| 5253 | SS.setRange(ULE->getQualifierRange()); |
| 5254 | } |
| 5255 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5256 | TemplateArgumentListInfo TABuffer; |
| 5257 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5258 | if (ULE->hasExplicitTemplateArgs()) { |
| 5259 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5260 | ExplicitTemplateArgs = &TABuffer; |
| 5261 | } |
| 5262 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5263 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5264 | Sema::LookupOrdinaryName); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 5265 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5266 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5267 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5268 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5269 | |
| 5270 | // Build an implicit member call if appropriate. Just drop the |
| 5271 | // casts and such from the call, we don't really care. |
| 5272 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5273 | if ((*R.begin())->isCXXClassMember()) |
| 5274 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5275 | else if (ExplicitTemplateArgs) |
| 5276 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5277 | else |
| 5278 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5279 | |
| 5280 | if (NewFn.isInvalid()) |
| 5281 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5282 | |
| 5283 | Fn->Destroy(SemaRef.Context); |
| 5284 | |
| 5285 | // This shouldn't cause an infinite loop because we're giving it |
| 5286 | // an expression with non-empty lookup results, which should never |
| 5287 | // end up here. |
| 5288 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5289 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5290 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5291 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5293 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5294 | /// (which eventually refers to the declaration Func) and the call |
| 5295 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5296 | /// to a specific function. If overload resolution succeeds, returns |
| 5297 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 5298 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5299 | /// arguments and Fn, and returns NULL. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5300 | Sema::OwningExprResult |
| 5301 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 5302 | SourceLocation LParenLoc, |
| 5303 | Expr **Args, unsigned NumArgs, |
| 5304 | SourceLocation *CommaLocs, |
| 5305 | SourceLocation RParenLoc) { |
| 5306 | #ifndef NDEBUG |
| 5307 | if (ULE->requiresADL()) { |
| 5308 | // To do ADL, we must have found an unqualified name. |
| 5309 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5310 | |
| 5311 | // We don't perform ADL for implicit declarations of builtins. |
| 5312 | // Verify that this was correctly set up. |
| 5313 | FunctionDecl *F; |
| 5314 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5315 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5316 | F->getBuiltinID() && F->isImplicit()) |
| 5317 | assert(0 && "performing ADL for builtin"); |
| 5318 | |
| 5319 | // We don't perform ADL in C. |
| 5320 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5321 | } |
| 5322 | #endif |
| 5323 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5324 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5325 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5326 | // Add the functions denoted by the callee to the set of candidate |
| 5327 | // functions, including those from argument-dependent lookup. |
| 5328 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5329 | |
| 5330 | // If we found nothing, try to recover. |
| 5331 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5332 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5333 | if (CandidateSet.empty()) |
| 5334 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5335 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5336 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5337 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5338 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5339 | case OR_Success: { |
| 5340 | FunctionDecl *FDecl = Best->Function; |
| 5341 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 5342 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5343 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5344 | |
| 5345 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5346 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5347 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5348 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5349 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5350 | break; |
| 5351 | |
| 5352 | case OR_Ambiguous: |
| 5353 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5354 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5355 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5356 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5357 | |
| 5358 | case OR_Deleted: |
| 5359 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5360 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5361 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5362 | << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5363 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5364 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5368 | // return NULL. |
| 5369 | Fn->Destroy(Context); |
| 5370 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5371 | Args[Arg]->Destroy(Context); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5372 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5375 | static bool IsOverloaded(const Sema::FunctionSet &Functions) { |
| 5376 | return Functions.size() > 1 || |
| 5377 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5378 | } |
| 5379 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5380 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5381 | /// operator. |
| 5382 | /// |
| 5383 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5384 | /// |
| 5385 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5386 | /// operator. |
| 5387 | /// |
| 5388 | /// \param Functions The set of non-member functions that will be |
| 5389 | /// considered by overload resolution. The caller needs to build this |
| 5390 | /// set based on the context using, e.g., |
| 5391 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5392 | /// set should not contain any member functions; those will be added |
| 5393 | /// by CreateOverloadedUnaryOp(). |
| 5394 | /// |
| 5395 | /// \param input The input argument. |
| 5396 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 5397 | unsigned OpcIn, |
| 5398 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5399 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5400 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5401 | Expr *Input = (Expr *)input.get(); |
| 5402 | |
| 5403 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5404 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5405 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5406 | |
| 5407 | Expr *Args[2] = { Input, 0 }; |
| 5408 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5410 | // For post-increment and post-decrement, add the implicit '0' as |
| 5411 | // the second argument, so that we know this is a post-increment or |
| 5412 | // post-decrement. |
| 5413 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5414 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5416 | SourceLocation()); |
| 5417 | NumArgs = 2; |
| 5418 | } |
| 5419 | |
| 5420 | if (Input->isTypeDependent()) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5421 | UnresolvedLookupExpr *Fn |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5422 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5423 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5424 | /*ADL*/ true, IsOverloaded(Functions)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5426 | FuncEnd = Functions.end(); |
| 5427 | Func != FuncEnd; ++Func) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5428 | Fn->addDecl(*Func); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5430 | input.release(); |
| 5431 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5432 | &Args[0], NumArgs, |
| 5433 | Context.DependentTy, |
| 5434 | OpLoc)); |
| 5435 | } |
| 5436 | |
| 5437 | // Build an empty overload set. |
| 5438 | OverloadCandidateSet CandidateSet; |
| 5439 | |
| 5440 | // Add the candidates from the given function set. |
| 5441 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 5442 | |
| 5443 | // Add operator candidates that are member functions. |
| 5444 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5445 | |
| 5446 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5447 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5448 | |
| 5449 | // Perform overload resolution. |
| 5450 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5451 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5452 | case OR_Success: { |
| 5453 | // We found a built-in operator or an overloaded operator. |
| 5454 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5456 | if (FnDecl) { |
| 5457 | // We matched an overloaded operator. Build a call to that |
| 5458 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5459 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5460 | // Convert the arguments. |
| 5461 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5462 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 5463 | return ExprError(); |
| 5464 | } else { |
| 5465 | // Convert the arguments. |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5466 | OwningExprResult InputInit |
| 5467 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5468 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5469 | SourceLocation(), |
| 5470 | move(input)); |
| 5471 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5472 | return ExprError(); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5473 | |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5474 | input = move(InputInit); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5475 | Input = (Expr *)input.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | // Determine the result type |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5479 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5480 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5481 | // Build the actual expression node. |
| 5482 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5483 | SourceLocation()); |
| 5484 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5485 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5486 | input.release(); |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5487 | Args[0] = Input; |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5488 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5489 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5490 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5491 | |
| 5492 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5493 | FnDecl)) |
| 5494 | return ExprError(); |
| 5495 | |
| 5496 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5497 | } else { |
| 5498 | // We matched a built-in operator. Convert the arguments, then |
| 5499 | // break out so that we will build the appropriate built-in |
| 5500 | // operator node. |
| 5501 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5502 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5503 | return ExprError(); |
| 5504 | |
| 5505 | break; |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | case OR_No_Viable_Function: |
| 5510 | // No viable function; fall through to handling this as a |
| 5511 | // built-in operator, which will produce an error message for us. |
| 5512 | break; |
| 5513 | |
| 5514 | case OR_Ambiguous: |
| 5515 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5516 | << UnaryOperator::getOpcodeStr(Opc) |
| 5517 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5518 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5519 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5520 | return ExprError(); |
| 5521 | |
| 5522 | case OR_Deleted: |
| 5523 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5524 | << Best->Function->isDeleted() |
| 5525 | << UnaryOperator::getOpcodeStr(Opc) |
| 5526 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5527 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5528 | return ExprError(); |
| 5529 | } |
| 5530 | |
| 5531 | // Either we found no viable overloaded operator or we matched a |
| 5532 | // built-in operator. In either case, fall through to trying to |
| 5533 | // build a built-in operation. |
| 5534 | input.release(); |
| 5535 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5536 | } |
| 5537 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5538 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5539 | /// operator. |
| 5540 | /// |
| 5541 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5542 | /// |
| 5543 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5544 | /// operator. |
| 5545 | /// |
| 5546 | /// \param Functions The set of non-member functions that will be |
| 5547 | /// considered by overload resolution. The caller needs to build this |
| 5548 | /// set based on the context using, e.g., |
| 5549 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5550 | /// set should not contain any member functions; those will be added |
| 5551 | /// by CreateOverloadedBinOp(). |
| 5552 | /// |
| 5553 | /// \param LHS Left-hand argument. |
| 5554 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5555 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5556 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5557 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5558 | FunctionSet &Functions, |
| 5559 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5560 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5561 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5562 | |
| 5563 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5564 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5565 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5566 | |
| 5567 | // If either side is type-dependent, create an appropriate dependent |
| 5568 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5569 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5570 | if (Functions.empty()) { |
| 5571 | // If there are no functions to store, just build a dependent |
| 5572 | // BinaryOperator or CompoundAssignment. |
| 5573 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5574 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5575 | Context.DependentTy, OpLoc)); |
| 5576 | |
| 5577 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5578 | Context.DependentTy, |
| 5579 | Context.DependentTy, |
| 5580 | Context.DependentTy, |
| 5581 | OpLoc)); |
| 5582 | } |
| 5583 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5584 | UnresolvedLookupExpr *Fn |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5585 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5586 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5587 | /* ADL */ true, IsOverloaded(Functions)); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5588 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5589 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5590 | FuncEnd = Functions.end(); |
| 5591 | Func != FuncEnd; ++Func) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5592 | Fn->addDecl(*Func); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5593 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5594 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5595 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5596 | Context.DependentTy, |
| 5597 | OpLoc)); |
| 5598 | } |
| 5599 | |
| 5600 | // If this is the .* operator, which is not overloadable, just |
| 5601 | // create a built-in binary operator. |
| 5602 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5603 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5604 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 5605 | // If this is the assignment operator, we only perform overload resolution |
| 5606 | // if the left-hand side is a class or enumeration type. This is actually |
| 5607 | // a hack. The standard requires that we do overload resolution between the |
| 5608 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5609 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5610 | // Note that we go the traditional code path for compound assignment forms. |
| 5611 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5612 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5613 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5614 | // Build an empty overload set. |
| 5615 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5616 | |
| 5617 | // Add the candidates from the given function set. |
| 5618 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 5619 | |
| 5620 | // Add operator candidates that are member functions. |
| 5621 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5622 | |
| 5623 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5624 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5625 | |
| 5626 | // Perform overload resolution. |
| 5627 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5628 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5629 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5630 | // We found a built-in operator or an overloaded operator. |
| 5631 | FunctionDecl *FnDecl = Best->Function; |
| 5632 | |
| 5633 | if (FnDecl) { |
| 5634 | // We matched an overloaded operator. Build a call to that |
| 5635 | // operator. |
| 5636 | |
| 5637 | // Convert the arguments. |
| 5638 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5639 | OwningExprResult Arg1 |
| 5640 | = PerformCopyInitialization( |
| 5641 | InitializedEntity::InitializeParameter( |
| 5642 | FnDecl->getParamDecl(0)), |
| 5643 | SourceLocation(), |
| 5644 | Owned(Args[1])); |
| 5645 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5646 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5647 | |
| 5648 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5649 | return ExprError(); |
| 5650 | |
| 5651 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5652 | } else { |
| 5653 | // Convert the arguments. |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5654 | OwningExprResult Arg0 |
| 5655 | = PerformCopyInitialization( |
| 5656 | InitializedEntity::InitializeParameter( |
| 5657 | FnDecl->getParamDecl(0)), |
| 5658 | SourceLocation(), |
| 5659 | Owned(Args[0])); |
| 5660 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5661 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5662 | |
| 5663 | OwningExprResult Arg1 |
| 5664 | = PerformCopyInitialization( |
| 5665 | InitializedEntity::InitializeParameter( |
| 5666 | FnDecl->getParamDecl(1)), |
| 5667 | SourceLocation(), |
| 5668 | Owned(Args[1])); |
| 5669 | if (Arg1.isInvalid()) |
| 5670 | return ExprError(); |
| 5671 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5672 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | // Determine the result type |
| 5676 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5677 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5678 | ResultTy = ResultTy.getNonReferenceType(); |
| 5679 | |
| 5680 | // Build the actual expression node. |
| 5681 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 5682 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5683 | UsualUnaryConversions(FnExpr); |
| 5684 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5685 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5686 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5687 | Args, 2, ResultTy, |
| 5688 | OpLoc)); |
| 5689 | |
| 5690 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5691 | FnDecl)) |
| 5692 | return ExprError(); |
| 5693 | |
| 5694 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5695 | } else { |
| 5696 | // We matched a built-in operator. Convert the arguments, then |
| 5697 | // break out so that we will build the appropriate built-in |
| 5698 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5699 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5700 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5701 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5702 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5703 | return ExprError(); |
| 5704 | |
| 5705 | break; |
| 5706 | } |
| 5707 | } |
| 5708 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5709 | case OR_No_Viable_Function: { |
| 5710 | // C++ [over.match.oper]p9: |
| 5711 | // If the operator is the operator , [...] and there are no |
| 5712 | // viable functions, then the operator is assumed to be the |
| 5713 | // built-in operator and interpreted according to clause 5. |
| 5714 | if (Opc == BinaryOperator::Comma) |
| 5715 | break; |
| 5716 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5717 | // For class as left operand for assignment or compound assigment operator |
| 5718 | // do not fall through to handling in built-in, but report that no overloaded |
| 5719 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5720 | OwningExprResult Result = ExprError(); |
| 5721 | if (Args[0]->getType()->isRecordType() && |
| 5722 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5723 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5724 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5725 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5726 | } else { |
| 5727 | // No viable function; try to create a built-in operation, which will |
| 5728 | // produce an error. Then, show the non-viable candidates. |
| 5729 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5730 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5731 | assert(Result.isInvalid() && |
| 5732 | "C++ binary operator overloading is missing candidates!"); |
| 5733 | if (Result.isInvalid()) |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5734 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5735 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5736 | return move(Result); |
| 5737 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5738 | |
| 5739 | case OR_Ambiguous: |
| 5740 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5741 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5742 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5743 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5744 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5745 | return ExprError(); |
| 5746 | |
| 5747 | case OR_Deleted: |
| 5748 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5749 | << Best->Function->isDeleted() |
| 5750 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5751 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5752 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5753 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5754 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5755 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5756 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5757 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5760 | Action::OwningExprResult |
| 5761 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5762 | SourceLocation RLoc, |
| 5763 | ExprArg Base, ExprArg Idx) { |
| 5764 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5765 | static_cast<Expr*>(Idx.get()) }; |
| 5766 | DeclarationName OpName = |
| 5767 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5768 | |
| 5769 | // If either side is type-dependent, create an appropriate dependent |
| 5770 | // expression. |
| 5771 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5772 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5773 | UnresolvedLookupExpr *Fn |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5774 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, |
| 5775 | 0, SourceRange(), OpName, LLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5776 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5777 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5778 | |
| 5779 | Base.release(); |
| 5780 | Idx.release(); |
| 5781 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5782 | Args, 2, |
| 5783 | Context.DependentTy, |
| 5784 | RLoc)); |
| 5785 | } |
| 5786 | |
| 5787 | // Build an empty overload set. |
| 5788 | OverloadCandidateSet CandidateSet; |
| 5789 | |
| 5790 | // Subscript can only be overloaded as a member function. |
| 5791 | |
| 5792 | // Add operator candidates that are member functions. |
| 5793 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5794 | |
| 5795 | // Add builtin operator candidates. |
| 5796 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5797 | |
| 5798 | // Perform overload resolution. |
| 5799 | OverloadCandidateSet::iterator Best; |
| 5800 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5801 | case OR_Success: { |
| 5802 | // We found a built-in operator or an overloaded operator. |
| 5803 | FunctionDecl *FnDecl = Best->Function; |
| 5804 | |
| 5805 | if (FnDecl) { |
| 5806 | // We matched an overloaded operator. Build a call to that |
| 5807 | // operator. |
| 5808 | |
| 5809 | // Convert the arguments. |
| 5810 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5811 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 5812 | PerformCopyInitialization(Args[1], |
| 5813 | FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5814 | AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5815 | return ExprError(); |
| 5816 | |
| 5817 | // Determine the result type |
| 5818 | QualType ResultTy |
| 5819 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5820 | ResultTy = ResultTy.getNonReferenceType(); |
| 5821 | |
| 5822 | // Build the actual expression node. |
| 5823 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5824 | LLoc); |
| 5825 | UsualUnaryConversions(FnExpr); |
| 5826 | |
| 5827 | Base.release(); |
| 5828 | Idx.release(); |
| 5829 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5830 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5831 | FnExpr, Args, 2, |
| 5832 | ResultTy, RLoc)); |
| 5833 | |
| 5834 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5835 | FnDecl)) |
| 5836 | return ExprError(); |
| 5837 | |
| 5838 | return MaybeBindToTemporary(TheCall.release()); |
| 5839 | } else { |
| 5840 | // We matched a built-in operator. Convert the arguments, then |
| 5841 | // break out so that we will build the appropriate built-in |
| 5842 | // operator node. |
| 5843 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5844 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5845 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5846 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5847 | return ExprError(); |
| 5848 | |
| 5849 | break; |
| 5850 | } |
| 5851 | } |
| 5852 | |
| 5853 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5854 | if (CandidateSet.empty()) |
| 5855 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5856 | << Args[0]->getType() << /*subscript*/ 0 |
| 5857 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5858 | else |
| 5859 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5860 | << Args[0]->getType() |
| 5861 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5862 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5863 | "[]", LLoc); |
| 5864 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | case OR_Ambiguous: |
| 5868 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5869 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5870 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5871 | "[]", LLoc); |
| 5872 | return ExprError(); |
| 5873 | |
| 5874 | case OR_Deleted: |
| 5875 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5876 | << Best->Function->isDeleted() << "[]" |
| 5877 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5878 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5879 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5880 | return ExprError(); |
| 5881 | } |
| 5882 | |
| 5883 | // We matched a built-in operator; build it. |
| 5884 | Base.release(); |
| 5885 | Idx.release(); |
| 5886 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5887 | Owned(Args[1]), RLoc); |
| 5888 | } |
| 5889 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5890 | /// BuildCallToMemberFunction - Build a call to a member |
| 5891 | /// function. MemExpr is the expression that refers to the member |
| 5892 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5893 | /// arguments to the function call (not including the object |
| 5894 | /// parameter). The caller needs to validate that the member |
| 5895 | /// expression refers to a member function or an overloaded member |
| 5896 | /// function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5897 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5898 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5899 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5900 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5901 | SourceLocation RParenLoc) { |
| 5902 | // Dig out the member expression. This holds both the object |
| 5903 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5904 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 5905 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5906 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5907 | CXXMethodDecl *Method = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5908 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 5909 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5910 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 5911 | } else { |
| 5912 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5913 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5914 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5915 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5916 | // Add overload candidates |
| 5917 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5918 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5919 | // FIXME: avoid copy. |
| 5920 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 5921 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 5922 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 5923 | TemplateArgs = &TemplateArgsBuffer; |
| 5924 | } |
| 5925 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5926 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 5927 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 5928 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5929 | NamedDecl *Func = *I; |
| 5930 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 5931 | if (isa<UsingShadowDecl>(Func)) |
| 5932 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 5933 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5934 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5935 | // If explicit template arguments were provided, we can't call a |
| 5936 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5937 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5938 | continue; |
| 5939 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5940 | AddMethodCandidate(Method, ActingDC, ObjectType, Args, NumArgs, |
| 5941 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5942 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5943 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 5944 | ActingDC, TemplateArgs, |
| 5945 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5946 | CandidateSet, |
| 5947 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5948 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 5949 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5950 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5951 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 5952 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5953 | OverloadCandidateSet::iterator Best; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5954 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5955 | case OR_Success: |
| 5956 | Method = cast<CXXMethodDecl>(Best->Function); |
| 5957 | break; |
| 5958 | |
| 5959 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5960 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5961 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5962 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5963 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5964 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5965 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5966 | |
| 5967 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5968 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5969 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5970 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5971 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5972 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5973 | |
| 5974 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5975 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5976 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 5977 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5978 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5979 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5980 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5981 | } |
| 5982 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 5983 | MemExprE = FixOverloadedFunctionReference(MemExprE, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5984 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5985 | // If overload resolution picked a static member, build a |
| 5986 | // non-member call based on that function. |
| 5987 | if (Method->isStatic()) { |
| 5988 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 5989 | Args, NumArgs, RParenLoc); |
| 5990 | } |
| 5991 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5992 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
| 5995 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5997 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5998 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5999 | Method->getResultType().getNonReferenceType(), |
| 6000 | RParenLoc)); |
| 6001 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6002 | // Check for a valid return type. |
| 6003 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6004 | TheCall.get(), Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6005 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6007 | // Convert the object argument (for a non-static member function call). |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6008 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6009 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6010 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6011 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6012 | MemExpr->setBase(ObjectArg); |
| 6013 | |
| 6014 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6015 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6016 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6017 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6018 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6019 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6020 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6021 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 6022 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6023 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6026 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6027 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6028 | /// overloaded function call operator (@c operator()) or performing a |
| 6029 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6030 | Sema::ExprResult |
| 6031 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 6032 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6033 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6034 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6035 | SourceLocation RParenLoc) { |
| 6036 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6037 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6039 | // C++ [over.call.object]p1: |
| 6040 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6041 | // 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] | 6042 | // candidate functions includes at least the function call |
| 6043 | // operators of T. The function call operators of T are obtained by |
| 6044 | // ordinary lookup of the name operator() in the context of |
| 6045 | // (E).operator(). |
| 6046 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6047 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6048 | |
| 6049 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 6050 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 6051 | << Object->getSourceRange())) |
| 6052 | return true; |
| 6053 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6054 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6055 | LookupQualifiedName(R, Record->getDecl()); |
| 6056 | R.suppressDiagnostics(); |
| 6057 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6058 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6059 | Oper != OperEnd; ++Oper) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6060 | AddMethodCandidate(*Oper, Object->getType(), Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6061 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6062 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6063 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6064 | // C++ [over.call.object]p2: |
| 6065 | // In addition, for each conversion function declared in T of the |
| 6066 | // form |
| 6067 | // |
| 6068 | // operator conversion-type-id () cv-qualifier; |
| 6069 | // |
| 6070 | // where cv-qualifier is the same cv-qualification as, or a |
| 6071 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 6072 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6073 | // R", or the type "reference to pointer to function of |
| 6074 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6075 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6076 | // is also considered as a candidate function. Similarly, |
| 6077 | // surrogate call functions are added to the set of candidate |
| 6078 | // functions for each conversion function declared in an |
| 6079 | // accessible base class provided the function is not hidden |
| 6080 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6081 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 6082 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6083 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6084 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6085 | NamedDecl *D = *I; |
| 6086 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6087 | if (isa<UsingShadowDecl>(D)) |
| 6088 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6089 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6090 | // Skip over templated conversion functions; they aren't |
| 6091 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6092 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6093 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6094 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6095 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6096 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6097 | // Strip the reference type (if any) and then the pointer type (if |
| 6098 | // any) to get down to what might be a function type. |
| 6099 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6100 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6101 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6102 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6103 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6104 | AddSurrogateCandidate(Conv, ActingContext, Proto, |
| 6105 | Object->getType(), Args, NumArgs, |
| 6106 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6108 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6109 | // Perform overload resolution. |
| 6110 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6111 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6112 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6113 | // Overload resolution succeeded; we'll build the appropriate call |
| 6114 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6115 | break; |
| 6116 | |
| 6117 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6118 | if (CandidateSet.empty()) |
| 6119 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6120 | << Object->getType() << /*call*/ 1 |
| 6121 | << Object->getSourceRange(); |
| 6122 | else |
| 6123 | Diag(Object->getSourceRange().getBegin(), |
| 6124 | diag::err_ovl_no_viable_object_call) |
| 6125 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6126 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6127 | break; |
| 6128 | |
| 6129 | case OR_Ambiguous: |
| 6130 | Diag(Object->getSourceRange().getBegin(), |
| 6131 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6132 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6133 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6134 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6135 | |
| 6136 | case OR_Deleted: |
| 6137 | Diag(Object->getSourceRange().getBegin(), |
| 6138 | diag::err_ovl_deleted_object_call) |
| 6139 | << Best->Function->isDeleted() |
| 6140 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6141 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6142 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6144 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6145 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6146 | // We had an error; delete all of the subexpressions and return |
| 6147 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6148 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6149 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6150 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6151 | return true; |
| 6152 | } |
| 6153 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6154 | if (Best->Function == 0) { |
| 6155 | // Since there is no function declaration, this is one of the |
| 6156 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6157 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6158 | = cast<CXXConversionDecl>( |
| 6159 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6160 | |
| 6161 | // We selected one of the surrogate functions that converts the |
| 6162 | // object parameter to a function pointer. Perform the conversion |
| 6163 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6164 | |
| 6165 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6166 | // and then call it. |
Eli Friedman | c8c771e | 2009-12-09 04:52:43 +0000 | [diff] [blame] | 6167 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6168 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6169 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6170 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 6171 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6175 | // that calls this method, using Object for the implicit object |
| 6176 | // parameter and passing along the remaining arguments. |
| 6177 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6178 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6179 | |
| 6180 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6181 | unsigned NumArgsToCheck = NumArgs; |
| 6182 | |
| 6183 | // Build the full argument list for the method call (the |
| 6184 | // implicit object parameter is placed at the beginning of the |
| 6185 | // list). |
| 6186 | Expr **MethodArgs; |
| 6187 | if (NumArgs < NumArgsInProto) { |
| 6188 | NumArgsToCheck = NumArgsInProto; |
| 6189 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6190 | } else { |
| 6191 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6192 | } |
| 6193 | MethodArgs[0] = Object; |
| 6194 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6195 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6196 | |
| 6197 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6198 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6199 | UsualUnaryConversions(NewFn); |
| 6200 | |
| 6201 | // Once we've built TheCall, all of the expressions are properly |
| 6202 | // owned. |
| 6203 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6204 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6205 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6206 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6207 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6208 | delete [] MethodArgs; |
| 6209 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6210 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6211 | Method)) |
| 6212 | return true; |
| 6213 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6214 | // We may have default arguments. If so, we need to allocate more |
| 6215 | // slots in the call for them. |
| 6216 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6217 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6218 | else if (NumArgs > NumArgsInProto) |
| 6219 | NumArgsToCheck = NumArgsInProto; |
| 6220 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6221 | bool IsError = false; |
| 6222 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6223 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6224 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6225 | TheCall->setArg(0, Object); |
| 6226 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6227 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6228 | // Check the argument types. |
| 6229 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6230 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6231 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6232 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6233 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6234 | // Pass the argument. |
| 6235 | QualType ProtoArgType = Proto->getArgType(i); |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6236 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, AA_Passing); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6237 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 6238 | OwningExprResult DefArg |
| 6239 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6240 | if (DefArg.isInvalid()) { |
| 6241 | IsError = true; |
| 6242 | break; |
| 6243 | } |
| 6244 | |
| 6245 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6246 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6247 | |
| 6248 | TheCall->setArg(i + 1, Arg); |
| 6249 | } |
| 6250 | |
| 6251 | // If this is a variadic call, handle args passed through "...". |
| 6252 | if (Proto->isVariadic()) { |
| 6253 | // Promote the arguments (C99 6.5.2.2p7). |
| 6254 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6255 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6256 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6257 | TheCall->setArg(i + 1, Arg); |
| 6258 | } |
| 6259 | } |
| 6260 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6261 | if (IsError) return true; |
| 6262 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6263 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6264 | return true; |
| 6265 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 6266 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6269 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6270 | /// (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] | 6271 | /// @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] | 6272 | Sema::OwningExprResult |
| 6273 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6274 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6275 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6277 | // C++ [over.ref]p1: |
| 6278 | // |
| 6279 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6280 | // for a class object x of type T if T::operator->() exists and if |
| 6281 | // the operator is selected as the best match function by the |
| 6282 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6283 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 6284 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6285 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6286 | |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 6287 | if (RequireCompleteType(Base->getLocStart(), Base->getType(), |
| 6288 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6289 | << Base->getSourceRange())) |
| 6290 | return ExprError(); |
| 6291 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6292 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6293 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6294 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6295 | |
| 6296 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6297 | Oper != OperEnd; ++Oper) { |
| 6298 | NamedDecl *D = *Oper; |
| 6299 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6300 | if (isa<UsingShadowDecl>(D)) |
| 6301 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6302 | |
| 6303 | AddMethodCandidate(cast<CXXMethodDecl>(D), ActingContext, |
| 6304 | Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6305 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6306 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6307 | |
| 6308 | // Perform overload resolution. |
| 6309 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6310 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6311 | case OR_Success: |
| 6312 | // Overload resolution succeeded; we'll build the call below. |
| 6313 | break; |
| 6314 | |
| 6315 | case OR_No_Viable_Function: |
| 6316 | if (CandidateSet.empty()) |
| 6317 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6318 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6319 | else |
| 6320 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6321 | << "operator->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6322 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6323 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6324 | |
| 6325 | case OR_Ambiguous: |
| 6326 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6327 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6328 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6329 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6330 | |
| 6331 | case OR_Deleted: |
| 6332 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6333 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6334 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6335 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6336 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
| 6339 | // Convert the object parameter. |
| 6340 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6341 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6342 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6343 | |
| 6344 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6345 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6346 | |
| 6347 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6348 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6349 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6350 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6351 | |
| 6352 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6353 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6354 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6355 | &Base, 1, ResultTy, OpLoc)); |
| 6356 | |
| 6357 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6358 | Method)) |
| 6359 | return ExprError(); |
| 6360 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6363 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6364 | /// a C++ overloaded function (possibly with some parentheses and |
| 6365 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6366 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6367 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 6368 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6369 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6370 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 6371 | if (SubExpr == PE->getSubExpr()) |
| 6372 | return PE->Retain(); |
| 6373 | |
| 6374 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6375 | } |
| 6376 | |
| 6377 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 6378 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6379 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6380 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6381 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6382 | if (SubExpr == ICE->getSubExpr()) |
| 6383 | return ICE->Retain(); |
| 6384 | |
| 6385 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6386 | ICE->getCastKind(), |
| 6387 | SubExpr, |
| 6388 | ICE->isLvalueCast()); |
| 6389 | } |
| 6390 | |
| 6391 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6392 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6393 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6394 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6395 | if (Method->isStatic()) { |
| 6396 | // Do nothing: static member functions aren't any different |
| 6397 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6398 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6399 | // Fix the sub expression, which really has to be an |
| 6400 | // UnresolvedLookupExpr holding an overloaded member function |
| 6401 | // or template. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6402 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6403 | if (SubExpr == UnOp->getSubExpr()) |
| 6404 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6405 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6406 | assert(isa<DeclRefExpr>(SubExpr) |
| 6407 | && "fixed to something other than a decl ref"); |
| 6408 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6409 | && "fixed to a member ref with no nested name qualifier"); |
| 6410 | |
| 6411 | // We have taken the address of a pointer to member |
| 6412 | // function. Perform the computation here so that we get the |
| 6413 | // appropriate pointer to member type. |
| 6414 | QualType ClassType |
| 6415 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6416 | QualType MemPtrType |
| 6417 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6418 | |
| 6419 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6420 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6421 | } |
| 6422 | } |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6423 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6424 | if (SubExpr == UnOp->getSubExpr()) |
| 6425 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6426 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6427 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6428 | Context.getPointerType(SubExpr->getType()), |
| 6429 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6430 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6431 | |
| 6432 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6433 | // FIXME: avoid copy. |
| 6434 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6435 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6436 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6437 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6440 | return DeclRefExpr::Create(Context, |
| 6441 | ULE->getQualifier(), |
| 6442 | ULE->getQualifierRange(), |
| 6443 | Fn, |
| 6444 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6445 | Fn->getType(), |
| 6446 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6447 | } |
| 6448 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6449 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6450 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6451 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6452 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6453 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6454 | TemplateArgs = &TemplateArgsBuffer; |
| 6455 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6456 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6457 | Expr *Base; |
| 6458 | |
| 6459 | // If we're filling in |
| 6460 | if (MemExpr->isImplicitAccess()) { |
| 6461 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6462 | return DeclRefExpr::Create(Context, |
| 6463 | MemExpr->getQualifier(), |
| 6464 | MemExpr->getQualifierRange(), |
| 6465 | Fn, |
| 6466 | MemExpr->getMemberLoc(), |
| 6467 | Fn->getType(), |
| 6468 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6469 | } else { |
| 6470 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6471 | if (MemExpr->getQualifier()) |
| 6472 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6473 | Base = new (Context) CXXThisExpr(Loc, |
| 6474 | MemExpr->getBaseType(), |
| 6475 | /*isImplicit=*/true); |
| 6476 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6477 | } else |
| 6478 | Base = MemExpr->getBase()->Retain(); |
| 6479 | |
| 6480 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6481 | MemExpr->isArrow(), |
| 6482 | MemExpr->getQualifier(), |
| 6483 | MemExpr->getQualifierRange(), |
| 6484 | Fn, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6485 | MemExpr->getMemberLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6486 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6487 | Fn->getType()); |
| 6488 | } |
| 6489 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6490 | assert(false && "Invalid reference to overloaded function"); |
| 6491 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6492 | } |
| 6493 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6494 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
| 6495 | FunctionDecl *Fn) { |
| 6496 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Fn)); |
| 6497 | } |
| 6498 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6499 | } // end namespace clang |