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, |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 83 | ICR_Complex_Real_Conversion |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 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; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 121 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 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. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 151 | if (getToType(1)->isBooleanType() && |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 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(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 167 | QualType ToType = getToType(1); |
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; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 450 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 451 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 452 | return ICS; |
| 453 | } |
| 454 | |
| 455 | if (!getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 456 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 457 | return ICS; |
| 458 | } |
| 459 | |
| 460 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 461 | OverloadingResult UserDefResult |
| 462 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
| 463 | !SuppressUserConversions, AllowExplicit, |
| 464 | ForceRValue, UserCast); |
| 465 | |
| 466 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 467 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 468 | // C++ [over.ics.user]p4: |
| 469 | // A conversion of an expression of class type to the same class |
| 470 | // type is given Exact Match rank, and a conversion of an |
| 471 | // expression of class type to a base class of that type is |
| 472 | // given Conversion rank, in spite of the fact that a copy |
| 473 | // constructor (i.e., a user-defined conversion function) is |
| 474 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 476 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 478 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 479 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 480 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 0d6d12b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 481 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 482 | // Turn this into a "standard" conversion sequence, so that it |
| 483 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 484 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 485 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 486 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 487 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 488 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 489 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 490 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 491 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 492 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 493 | |
| 494 | // C++ [over.best.ics]p4: |
| 495 | // However, when considering the argument of a user-defined |
| 496 | // conversion function that is a candidate by 13.3.1.3 when |
| 497 | // invoked for the copying of the temporary in the second step |
| 498 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 499 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 500 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 501 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 502 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 503 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 504 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 505 | ICS.setAmbiguous(); |
| 506 | ICS.Ambiguous.setFromType(From->getType()); |
| 507 | ICS.Ambiguous.setToType(ToType); |
| 508 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 509 | Cand != Conversions.end(); ++Cand) |
| 510 | if (Cand->Viable) |
| 511 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 512 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 513 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 514 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 515 | |
| 516 | return ICS; |
| 517 | } |
| 518 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 519 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 520 | /// conversion that strips "noreturn" off the nested function type. |
| 521 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 522 | QualType ToType, QualType &ResultTy) { |
| 523 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 524 | return false; |
| 525 | |
| 526 | // Strip the noreturn off the type we're converting from; noreturn can |
| 527 | // safely be removed. |
| 528 | FromType = Context.getNoReturnType(FromType, false); |
| 529 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 530 | return false; |
| 531 | |
| 532 | ResultTy = FromType; |
| 533 | return true; |
| 534 | } |
| 535 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 536 | /// IsStandardConversion - Determines whether there is a standard |
| 537 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 538 | /// expression From to the type ToType. Standard conversion sequences |
| 539 | /// only consider non-class types; for conversions that involve class |
| 540 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 541 | /// contain the standard conversion sequence required to perform this |
| 542 | /// conversion and this routine will return true. Otherwise, this |
| 543 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | bool |
| 545 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 546 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 548 | QualType FromType = From->getType(); |
| 549 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 550 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 551 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 552 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 553 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 554 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 555 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 557 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 559 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 560 | if (getLangOptions().CPlusPlus) |
| 561 | return false; |
| 562 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 566 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 567 | // array-to-pointer conversion, or function-to-pointer conversion |
| 568 | // (C++ 4p1). |
| 569 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 570 | DeclAccessPair AccessPair; |
| 571 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 573 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 574 | // converted to an rvalue. |
| 575 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 577 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 578 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 579 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 580 | |
| 581 | // If T is a non-class type, the type of the rvalue is the |
| 582 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 583 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 584 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 585 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 586 | } else if (FromType->isArrayType()) { |
| 587 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 588 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 589 | |
| 590 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 591 | // bound of T" can be converted to an rvalue of type "pointer to |
| 592 | // T" (C++ 4.2p1). |
| 593 | FromType = Context.getArrayDecayedType(FromType); |
| 594 | |
| 595 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 596 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 597 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | |
| 599 | // For the purpose of ranking in overload resolution |
| 600 | // (13.3.3.1.1), this conversion is considered an |
| 601 | // array-to-pointer conversion followed by a qualification |
| 602 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 603 | SCS.Second = ICK_Identity; |
| 604 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 605 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 606 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 607 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 608 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 609 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 610 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 611 | |
| 612 | // An lvalue of function type T can be converted to an rvalue of |
| 613 | // type "pointer to T." The result is a pointer to the |
| 614 | // function. (C++ 4.3p1). |
| 615 | FromType = Context.getPointerType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | } else if (FunctionDecl *Fn |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 617 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 618 | AccessPair)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 619 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 620 | SCS.First = ICK_Function_To_Pointer; |
| 621 | |
| 622 | // We were able to resolve the address of the overloaded function, |
| 623 | // so we can convert to the type of that function. |
| 624 | FromType = Fn->getType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 625 | if (ToType->isLValueReferenceType()) |
| 626 | FromType = Context.getLValueReferenceType(FromType); |
| 627 | else if (ToType->isRValueReferenceType()) |
| 628 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 629 | else if (ToType->isMemberPointerType()) { |
| 630 | // Resolve address only succeeds if both sides are member pointers, |
| 631 | // but it doesn't have to be the same class. See DR 247. |
| 632 | // Note that this means that the type of &Derived::fn can be |
| 633 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 634 | // base class, even if it was brought into the derived class via a |
| 635 | // using declaration. The standard isn't clear on this issue at all. |
| 636 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 637 | FromType = Context.getMemberPointerType(FromType, |
| 638 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 639 | } else |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 640 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 641 | } else { |
| 642 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 643 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 644 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 645 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 646 | |
| 647 | // The second conversion can be an integral promotion, floating |
| 648 | // point promotion, integral conversion, floating point conversion, |
| 649 | // floating-integral conversion, pointer conversion, |
| 650 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 651 | // For overloading in C, this can also be a "compatible-type" |
| 652 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 653 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 654 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 655 | // The unqualified versions of the types are the same: there's no |
| 656 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 657 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 658 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 660 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 661 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 662 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 663 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 664 | SCS.Second = ICK_Floating_Promotion; |
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 (IsComplexPromotion(FromType, ToType)) { |
| 667 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 668 | SCS.Second = ICK_Complex_Promotion; |
| 669 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 670 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 671 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 672 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 673 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 674 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 675 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 676 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 677 | SCS.Second = ICK_Complex_Conversion; |
| 678 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 679 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 680 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 681 | // Complex-real conversions (C99 6.3.1.7) |
| 682 | SCS.Second = ICK_Complex_Real; |
| 683 | FromType = ToType.getUnqualifiedType(); |
| 684 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 685 | // Floating point conversions (C++ 4.8). |
| 686 | SCS.Second = ICK_Floating_Conversion; |
| 687 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 688 | } else if ((FromType->isFloatingType() && |
| 689 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 690 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 692 | ToType->isFloatingType())) { |
| 693 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 694 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 695 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 696 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 697 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 698 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 699 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 700 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 701 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 702 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 703 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 704 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 705 | } else if (ToType->isBooleanType() && |
| 706 | (FromType->isArithmeticType() || |
| 707 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 708 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 709 | FromType->isBlockPointerType() || |
| 710 | FromType->isMemberPointerType() || |
| 711 | FromType->isNullPtrType())) { |
| 712 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 713 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 714 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 716 | Context.typesAreCompatible(ToType, FromType)) { |
| 717 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 718 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 719 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 720 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 721 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 722 | } else { |
| 723 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 724 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 725 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 726 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 727 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 728 | QualType CanonFrom; |
| 729 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 730 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 731 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 732 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 733 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 734 | CanonFrom = Context.getCanonicalType(FromType); |
| 735 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 736 | } else { |
| 737 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 738 | SCS.Third = ICK_Identity; |
| 739 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 741 | // [...] Any difference in top-level cv-qualification is |
| 742 | // subsumed by the initialization itself and does not constitute |
| 743 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 744 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 746 | if (CanonFrom.getLocalUnqualifiedType() |
| 747 | == CanonTo.getLocalUnqualifiedType() && |
| 748 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 749 | FromType = ToType; |
| 750 | CanonFrom = CanonTo; |
| 751 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 752 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 753 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 754 | |
| 755 | // If we have not converted the argument type to the parameter type, |
| 756 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 757 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 758 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 760 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 764 | /// expression From (whose potentially-adjusted type is FromType) to |
| 765 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 766 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 768 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 769 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 770 | if (!To) { |
| 771 | return false; |
| 772 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 773 | |
| 774 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 775 | // unsigned short int can be converted to an rvalue of type int if |
| 776 | // int can represent all the values of the source type; otherwise, |
| 777 | // the source rvalue can be converted to an rvalue of type unsigned |
| 778 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 779 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 780 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 781 | if (// We can promote any signed, promotable integer type to an int |
| 782 | (FromType->isSignedIntegerType() || |
| 783 | // We can promote any unsigned integer type whose size is |
| 784 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 786 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 787 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 790 | return To->getKind() == BuiltinType::UInt; |
| 791 | } |
| 792 | |
| 793 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 794 | // can be converted to an rvalue of the first of the following types |
| 795 | // that can represent all the values of its underlying type: int, |
| 796 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 797 | |
| 798 | // We pre-calculate the promotion type for enum types. |
| 799 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 800 | if (ToType->isIntegerType()) |
| 801 | return Context.hasSameUnqualifiedType(ToType, |
| 802 | FromEnumType->getDecl()->getPromotionType()); |
| 803 | |
| 804 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 805 | // Determine whether the type we're converting from is signed or |
| 806 | // unsigned. |
| 807 | bool FromIsSigned; |
| 808 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 809 | |
| 810 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 811 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 812 | |
| 813 | // The types we'll try to promote to, in the appropriate |
| 814 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | QualType PromoteTypes[6] = { |
| 816 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 817 | Context.LongTy, Context.UnsignedLongTy , |
| 818 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 819 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 820 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 821 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 822 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 824 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 825 | // We found the type that we can promote to. If this is the |
| 826 | // type we wanted, we have a promotion. Otherwise, no |
| 827 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 828 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 834 | // rvalue of type int if int can represent all the values of the |
| 835 | // bit-field; otherwise, it can be converted to unsigned int if |
| 836 | // unsigned int can represent all the values of the bit-field. If |
| 837 | // the bit-field is larger yet, no integral promotion applies to |
| 838 | // it. If the bit-field has an enumerated type, it is treated as any |
| 839 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 840 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 841 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 842 | using llvm::APSInt; |
| 843 | if (From) |
| 844 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 845 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 846 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 847 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 848 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 849 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 851 | // Are we promoting to an int from a bitfield that fits in an int? |
| 852 | if (BitWidth < ToSize || |
| 853 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 854 | return To->getKind() == BuiltinType::Int; |
| 855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 857 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 858 | // that fits into an unsigned int? |
| 859 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 860 | return To->getKind() == BuiltinType::UInt; |
| 861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 863 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 864 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 865 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 867 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 868 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 869 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 870 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 871 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 872 | |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 877 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 878 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 880 | /// An rvalue of type float can be converted to an rvalue of type |
| 881 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 882 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 883 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 884 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 885 | ToBuiltin->getKind() == BuiltinType::Double) |
| 886 | return true; |
| 887 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 888 | // C99 6.3.1.5p1: |
| 889 | // When a float is promoted to double or long double, or a |
| 890 | // double is promoted to long double [...]. |
| 891 | if (!getLangOptions().CPlusPlus && |
| 892 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 893 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 894 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 895 | return true; |
| 896 | } |
| 897 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 901 | /// \brief Determine if a conversion is a complex promotion. |
| 902 | /// |
| 903 | /// A complex promotion is defined as a complex -> complex conversion |
| 904 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 905 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 906 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 907 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 908 | if (!FromComplex) |
| 909 | return false; |
| 910 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 911 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 912 | if (!ToComplex) |
| 913 | return false; |
| 914 | |
| 915 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 916 | ToComplex->getElementType()) || |
| 917 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 918 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 921 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 922 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 923 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 924 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 925 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | static QualType |
| 927 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 928 | QualType ToPointee, QualType ToType, |
| 929 | ASTContext &Context) { |
| 930 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 931 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 932 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
| 934 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 935 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 936 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 937 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 938 | return ToType; |
| 939 | |
| 940 | // Build a pointer to ToPointee. It has the right qualifiers |
| 941 | // already. |
| 942 | return Context.getPointerType(ToPointee); |
| 943 | } |
| 944 | |
| 945 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 946 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 947 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 948 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 951 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 952 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 953 | /// not have the right set of qualifiers. |
| 954 | static QualType |
| 955 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 956 | QualType ToType, |
| 957 | ASTContext &Context) { |
| 958 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 959 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 960 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 961 | |
| 962 | // Exact qualifier match -> return the pointer type we're converting to. |
| 963 | if (CanonToType.getLocalQualifiers() == Quals) |
| 964 | return ToType; |
| 965 | |
| 966 | // Just build a canonical type that has the right qualifiers. |
| 967 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 968 | } |
| 969 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 971 | bool InOverloadResolution, |
| 972 | ASTContext &Context) { |
| 973 | // Handle value-dependent integral null pointer constants correctly. |
| 974 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 975 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 976 | Expr->getType()->isIntegralType()) |
| 977 | return !InOverloadResolution; |
| 978 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 979 | return Expr->isNullPointerConstant(Context, |
| 980 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 981 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 982 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 984 | /// IsPointerConversion - Determines whether the conversion of the |
| 985 | /// expression From, which has the (possibly adjusted) type FromType, |
| 986 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 987 | /// 4.10). If so, returns true and places the converted type (that |
| 988 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 989 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 990 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 991 | /// This routine also supports conversions to and from block pointers |
| 992 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 993 | /// pointers to interfaces. FIXME: Once we've determined the |
| 994 | /// appropriate overloading rules for Objective-C, we may want to |
| 995 | /// split the Objective-C checks into a different routine; however, |
| 996 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 997 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 998 | /// set if the conversion is an allowed Objective-C conversion that |
| 999 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1000 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1001 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1002 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1004 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1005 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1006 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1007 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1009 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1010 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1011 | ConvertedType = ToType; |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1015 | // Blocks: Block pointers can be converted to void*. |
| 1016 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1017 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1018 | ConvertedType = ToType; |
| 1019 | return true; |
| 1020 | } |
| 1021 | // Blocks: A null pointer constant can be converted to a block |
| 1022 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1024 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1025 | ConvertedType = ToType; |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1029 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1030 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1032 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1033 | ConvertedType = ToType; |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1037 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1038 | if (!ToTypePtr) |
| 1039 | return false; |
| 1040 | |
| 1041 | // 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] | 1042 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1043 | ConvertedType = ToType; |
| 1044 | return true; |
| 1045 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1046 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1047 | // Beyond this point, both types need to be pointers |
| 1048 | // , including objective-c pointers. |
| 1049 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1050 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1051 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1052 | ToType, Context); |
| 1053 | return true; |
| 1054 | |
| 1055 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1056 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1057 | if (!FromTypePtr) |
| 1058 | return false; |
| 1059 | |
| 1060 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1061 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1062 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1063 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1064 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1065 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1067 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1068 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1069 | return true; |
| 1070 | } |
| 1071 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1072 | // When we're overloading in C, we allow a special kind of pointer |
| 1073 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1075 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1077 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1079 | return true; |
| 1080 | } |
| 1081 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1082 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1084 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1085 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1086 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1087 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1088 | // necessitates this conversion is ill-formed. The result of the |
| 1089 | // conversion is a pointer to the base class sub-object of the |
| 1090 | // derived class object. The null pointer value is converted to |
| 1091 | // the null pointer value of the destination type. |
| 1092 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1093 | // Note that we do not check for ambiguity or inaccessibility |
| 1094 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1095 | if (getLangOptions().CPlusPlus && |
| 1096 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1097 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1098 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1099 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1101 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1102 | ToType, Context); |
| 1103 | return true; |
| 1104 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1105 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | /// isObjCPointerConversion - Determines whether this is an |
| 1110 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1111 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1113 | QualType& ConvertedType, |
| 1114 | bool &IncompatibleObjC) { |
| 1115 | if (!getLangOptions().ObjC1) |
| 1116 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1117 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1118 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1119 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1121 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1122 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1123 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1124 | // 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] | 1125 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1126 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1127 | ConvertedType = ToType; |
| 1128 | return true; |
| 1129 | } |
| 1130 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1132 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1134 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1135 | ConvertedType = ToType; |
| 1136 | return true; |
| 1137 | } |
| 1138 | // Objective C++: We're able to convert from a pointer to an |
| 1139 | // interface to a pointer to a different interface. |
| 1140 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1141 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1142 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1143 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1144 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1145 | FromObjCPtr->getPointeeType())) |
| 1146 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1147 | ConvertedType = ToType; |
| 1148 | return true; |
| 1149 | } |
| 1150 | |
| 1151 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1152 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1153 | // interfaces, which is permitted. However, we're going to |
| 1154 | // complain about it. |
| 1155 | IncompatibleObjC = true; |
| 1156 | ConvertedType = FromType; |
| 1157 | return true; |
| 1158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1160 | // 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] | 1161 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1162 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1163 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1164 | else if (const BlockPointerType *ToBlockPtr = |
| 1165 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1166 | // 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] | 1167 | // to a block pointer type. |
| 1168 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1169 | ConvertedType = ToType; |
| 1170 | return true; |
| 1171 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1172 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1173 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1174 | else if (FromType->getAs<BlockPointerType>() && |
| 1175 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1176 | // 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] | 1177 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1178 | ConvertedType = ToType; |
| 1179 | return true; |
| 1180 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1181 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1182 | return false; |
| 1183 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1184 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1185 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1186 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1187 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1188 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1189 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1190 | return false; |
| 1191 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1192 | // If we have pointers to pointers, recursively check whether this |
| 1193 | // is an Objective-C conversion. |
| 1194 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1195 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1196 | IncompatibleObjC)) { |
| 1197 | // We always complain about this conversion. |
| 1198 | IncompatibleObjC = true; |
| 1199 | ConvertedType = ToType; |
| 1200 | return true; |
| 1201 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1202 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1203 | // as in I* to id. |
| 1204 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1205 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1206 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1207 | IncompatibleObjC)) { |
| 1208 | ConvertedType = ToType; |
| 1209 | return true; |
| 1210 | } |
| 1211 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1212 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1213 | // differences in the argument and result types are in Objective-C |
| 1214 | // pointer conversions. If so, we permit the conversion (but |
| 1215 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1217 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1218 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1219 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1220 | if (FromFunctionType && ToFunctionType) { |
| 1221 | // If the function types are exactly the same, this isn't an |
| 1222 | // Objective-C pointer conversion. |
| 1223 | if (Context.getCanonicalType(FromPointeeType) |
| 1224 | == Context.getCanonicalType(ToPointeeType)) |
| 1225 | return false; |
| 1226 | |
| 1227 | // Perform the quick checks that will tell us whether these |
| 1228 | // function types are obviously different. |
| 1229 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1230 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1231 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1232 | return false; |
| 1233 | |
| 1234 | bool HasObjCConversion = false; |
| 1235 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1236 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1237 | // Okay, the types match exactly. Nothing to do. |
| 1238 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1239 | ToFunctionType->getResultType(), |
| 1240 | ConvertedType, IncompatibleObjC)) { |
| 1241 | // Okay, we have an Objective-C pointer conversion. |
| 1242 | HasObjCConversion = true; |
| 1243 | } else { |
| 1244 | // Function types are too different. Abort. |
| 1245 | return false; |
| 1246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1248 | // Check argument types. |
| 1249 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1250 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1251 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1252 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1253 | if (Context.getCanonicalType(FromArgType) |
| 1254 | == Context.getCanonicalType(ToArgType)) { |
| 1255 | // Okay, the types match exactly. Nothing to do. |
| 1256 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1257 | ConvertedType, IncompatibleObjC)) { |
| 1258 | // Okay, we have an Objective-C pointer conversion. |
| 1259 | HasObjCConversion = true; |
| 1260 | } else { |
| 1261 | // Argument types are too different. Abort. |
| 1262 | return false; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | if (HasObjCConversion) { |
| 1267 | // We had an Objective-C conversion. Allow this pointer |
| 1268 | // conversion, but complain about it. |
| 1269 | ConvertedType = ToType; |
| 1270 | IncompatibleObjC = true; |
| 1271 | return true; |
| 1272 | } |
| 1273 | } |
| 1274 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1275 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1278 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1279 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1280 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1281 | /// conversions for which IsPointerConversion has already returned |
| 1282 | /// true. It returns true and produces a diagnostic if there was an |
| 1283 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1284 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1285 | CastExpr::CastKind &Kind, |
| 1286 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1287 | QualType FromType = From->getType(); |
| 1288 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1289 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1290 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1291 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1292 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1293 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1294 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1295 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1296 | // We must have a derived-to-base conversion. Check an |
| 1297 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1298 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1299 | From->getExprLoc(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1300 | From->getSourceRange(), |
| 1301 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1302 | return true; |
| 1303 | |
| 1304 | // The conversion was successful. |
| 1305 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1306 | } |
| 1307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1309 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1311 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1312 | // Objective-C++ conversions are always okay. |
| 1313 | // FIXME: We should have a different class of conversions for the |
| 1314 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1315 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1316 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1317 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1318 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
| 1321 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1322 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1323 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1324 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1325 | /// If so, returns true and places the converted type (that might differ from |
| 1326 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1327 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1328 | QualType ToType, |
| 1329 | bool InOverloadResolution, |
| 1330 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1331 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1332 | if (!ToTypePtr) |
| 1333 | return false; |
| 1334 | |
| 1335 | // 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] | 1336 | if (From->isNullPointerConstant(Context, |
| 1337 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1338 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1339 | ConvertedType = ToType; |
| 1340 | return true; |
| 1341 | } |
| 1342 | |
| 1343 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1344 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1345 | if (!FromTypePtr) |
| 1346 | return false; |
| 1347 | |
| 1348 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1349 | // where D is derived from B (C++ 4.11p2). |
| 1350 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1351 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1352 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1353 | |
| 1354 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1355 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1356 | ToClass.getTypePtr()); |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
| 1360 | return false; |
| 1361 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1362 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1363 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1364 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1365 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1366 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1367 | /// true and produces a diagnostic if there was an error, or returns false |
| 1368 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1370 | CastExpr::CastKind &Kind, |
| 1371 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1372 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1373 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1374 | if (!FromPtrType) { |
| 1375 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1376 | assert(From->isNullPointerConstant(Context, |
| 1377 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1378 | "Expr must be null pointer constant!"); |
| 1379 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1380 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1381 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1382 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1383 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1384 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1385 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1386 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1387 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1388 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1389 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1390 | // FIXME: What about dependent types? |
| 1391 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1392 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1393 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1394 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/ true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1395 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1396 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1397 | assert(DerivationOkay && |
| 1398 | "Should not have been called if derivation isn't OK."); |
| 1399 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1400 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1401 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1402 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1403 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1404 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1405 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1406 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1407 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1409 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1410 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1411 | << FromClass << ToClass << QualType(VBase, 0) |
| 1412 | << From->getSourceRange(); |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1416 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1417 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1418 | Paths.front(), |
| 1419 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1420 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1421 | // Must be a base to derived member conversion. |
| 1422 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1423 | return false; |
| 1424 | } |
| 1425 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1426 | /// IsQualificationConversion - Determines whether the conversion from |
| 1427 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1428 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | bool |
| 1430 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1431 | FromType = Context.getCanonicalType(FromType); |
| 1432 | ToType = Context.getCanonicalType(ToType); |
| 1433 | |
| 1434 | // If FromType and ToType are the same type, this is not a |
| 1435 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1436 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1437 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1438 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1439 | // (C++ 4.4p4): |
| 1440 | // A conversion can add cv-qualifiers at levels other than the first |
| 1441 | // in multi-level pointers, subject to the following rules: [...] |
| 1442 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1443 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1444 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1445 | // Within each iteration of the loop, we check the qualifiers to |
| 1446 | // determine if this still looks like a qualification |
| 1447 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1448 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1449 | // until there are no more pointers or pointers-to-members left to |
| 1450 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1451 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1452 | |
| 1453 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1454 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1455 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1456 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1458 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1459 | // every cv for 0 < k < j. |
| 1460 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1461 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1462 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1464 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1465 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1467 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1468 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1469 | |
| 1470 | // We are left with FromType and ToType being the pointee types |
| 1471 | // after unwrapping the original FromType and ToType the same number |
| 1472 | // of types. If we unwrapped any pointers, and if FromType and |
| 1473 | // ToType have the same unqualified type (since we checked |
| 1474 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1475 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1478 | /// Determines whether there is a user-defined conversion sequence |
| 1479 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1480 | /// ToType. If such a conversion exists, User will contain the |
| 1481 | /// user-defined conversion sequence that performs such a conversion |
| 1482 | /// and this routine will return true. Otherwise, this routine returns |
| 1483 | /// false and User is unspecified. |
| 1484 | /// |
| 1485 | /// \param AllowConversionFunctions true if the conversion should |
| 1486 | /// consider conversion functions at all. If false, only constructors |
| 1487 | /// will be considered. |
| 1488 | /// |
| 1489 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1490 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1491 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1492 | /// |
| 1493 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1494 | /// for overload resolution. |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1495 | /// \param UserCast true if looking for user defined conversion for a static |
| 1496 | /// cast. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1497 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1498 | UserDefinedConversionSequence& User, |
| 1499 | OverloadCandidateSet& CandidateSet, |
| 1500 | bool AllowConversionFunctions, |
| 1501 | bool AllowExplicit, |
| 1502 | bool ForceRValue, |
| 1503 | bool UserCast) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1504 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1505 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1506 | // We're not going to find any constructors. |
| 1507 | } else if (CXXRecordDecl *ToRecordDecl |
| 1508 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1509 | // C++ [over.match.ctor]p1: |
| 1510 | // When objects of class type are direct-initialized (8.5), or |
| 1511 | // copy-initialized from an expression of the same or a |
| 1512 | // derived class type (8.5), overload resolution selects the |
| 1513 | // constructor. [...] For copy-initialization, the candidate |
| 1514 | // functions are all the converting constructors (12.3.1) of |
| 1515 | // that class. The argument list is the expression-list within |
| 1516 | // the parentheses of the initializer. |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1517 | bool SuppressUserConversions = !UserCast; |
| 1518 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1519 | IsDerivedFrom(From->getType(), ToType)) { |
| 1520 | SuppressUserConversions = false; |
| 1521 | AllowConversionFunctions = false; |
| 1522 | } |
| 1523 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1525 | = Context.DeclarationNames.getCXXConstructorName( |
| 1526 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1527 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1529 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1530 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1531 | NamedDecl *D = *Con; |
| 1532 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1533 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1534 | // Find the constructor (which may be a template). |
| 1535 | CXXConstructorDecl *Constructor = 0; |
| 1536 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1537 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1538 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1540 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1541 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1542 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1543 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1544 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1545 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1546 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1547 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1548 | /*ExplicitArgs*/ 0, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1549 | &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1550 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1551 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1552 | // Allow one user-defined conversion when user specifies a |
| 1553 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1554 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1555 | &From, 1, CandidateSet, |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1556 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1557 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1558 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1562 | if (!AllowConversionFunctions) { |
| 1563 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1565 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1566 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1567 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1569 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1571 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1572 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1573 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1574 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1575 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1576 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1577 | DeclAccessPair FoundDecl = I.getPair(); |
| 1578 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1579 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1580 | if (isa<UsingShadowDecl>(D)) |
| 1581 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1582 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1583 | CXXConversionDecl *Conv; |
| 1584 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1585 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1586 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1587 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1588 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1589 | |
| 1590 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1591 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1592 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1593 | ActingContext, From, ToType, |
| 1594 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1595 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1596 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1597 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1601 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1602 | |
| 1603 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1604 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1605 | case OR_Success: |
| 1606 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1608 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1609 | // C++ [over.ics.user]p1: |
| 1610 | // If the user-defined conversion is specified by a |
| 1611 | // constructor (12.3.1), the initial standard conversion |
| 1612 | // sequence converts the source type to the type required by |
| 1613 | // the argument of the constructor. |
| 1614 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1615 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1616 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1617 | User.EllipsisConversion = true; |
| 1618 | else { |
| 1619 | User.Before = Best->Conversions[0].Standard; |
| 1620 | User.EllipsisConversion = false; |
| 1621 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1622 | User.ConversionFunction = Constructor; |
| 1623 | User.After.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1624 | User.After.setFromType( |
| 1625 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1626 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1627 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1628 | } else if (CXXConversionDecl *Conversion |
| 1629 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1630 | // C++ [over.ics.user]p1: |
| 1631 | // |
| 1632 | // [...] If the user-defined conversion is specified by a |
| 1633 | // conversion function (12.3.2), the initial standard |
| 1634 | // conversion sequence converts the source type to the |
| 1635 | // implicit object parameter of the conversion function. |
| 1636 | User.Before = Best->Conversions[0].Standard; |
| 1637 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1638 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
| 1640 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1641 | // The second standard conversion sequence converts the |
| 1642 | // result of the user-defined conversion to the target type |
| 1643 | // for the sequence. Since an implicit conversion sequence |
| 1644 | // is an initialization, the special rules for |
| 1645 | // initialization by user-defined conversion apply when |
| 1646 | // selecting the best user-defined conversion for a |
| 1647 | // user-defined conversion sequence (see 13.3.3 and |
| 1648 | // 13.3.3.1). |
| 1649 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1650 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1651 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1652 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1653 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1656 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1657 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1658 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1659 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1660 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1661 | |
| 1662 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1663 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1666 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1667 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1668 | |
| 1669 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1670 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1671 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1672 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1673 | OverloadingResult OvResult = |
| 1674 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1675 | CandidateSet, true, false, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1676 | if (OvResult == OR_Ambiguous) |
| 1677 | Diag(From->getSourceRange().getBegin(), |
| 1678 | diag::err_typecheck_ambiguous_condition) |
| 1679 | << From->getType() << ToType << From->getSourceRange(); |
| 1680 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1681 | Diag(From->getSourceRange().getBegin(), |
| 1682 | diag::err_typecheck_nonviable_condition) |
| 1683 | << From->getType() << ToType << From->getSourceRange(); |
| 1684 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1685 | return false; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1686 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1687 | return true; |
| 1688 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1689 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1690 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1691 | /// conversion sequences to determine whether one is better than the |
| 1692 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1694 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1695 | const ImplicitConversionSequence& ICS2) |
| 1696 | { |
| 1697 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1698 | // conversion sequences (as defined in 13.3.3.1) |
| 1699 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1700 | // conversion sequence than a user-defined conversion sequence or |
| 1701 | // an ellipsis conversion sequence, and |
| 1702 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1703 | // conversion sequence than an ellipsis conversion sequence |
| 1704 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1706 | // C++0x [over.best.ics]p10: |
| 1707 | // For the purpose of ranking implicit conversion sequences as |
| 1708 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1709 | // treated as a user-defined sequence that is indistinguishable |
| 1710 | // from any other user-defined conversion sequence. |
| 1711 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1712 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1713 | return ImplicitConversionSequence::Better; |
| 1714 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1715 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1716 | return ImplicitConversionSequence::Worse; |
| 1717 | } |
| 1718 | |
| 1719 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1720 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Two implicit conversion sequences of the same form are |
| 1723 | // indistinguishable conversion sequences unless one of the |
| 1724 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1725 | if (ICS1.isStandard()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1726 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1727 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1728 | // User-defined conversion sequence U1 is a better conversion |
| 1729 | // sequence than another user-defined conversion sequence U2 if |
| 1730 | // they contain the same user-defined conversion function or |
| 1731 | // constructor and if the second standard conversion sequence of |
| 1732 | // U1 is better than the second standard conversion sequence of |
| 1733 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1735 | ICS2.UserDefined.ConversionFunction) |
| 1736 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1737 | ICS2.UserDefined.After); |
| 1738 | } |
| 1739 | |
| 1740 | return ImplicitConversionSequence::Indistinguishable; |
| 1741 | } |
| 1742 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1743 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 1744 | // determine if one is a proper subset of the other. |
| 1745 | static ImplicitConversionSequence::CompareKind |
| 1746 | compareStandardConversionSubsets(ASTContext &Context, |
| 1747 | const StandardConversionSequence& SCS1, |
| 1748 | const StandardConversionSequence& SCS2) { |
| 1749 | ImplicitConversionSequence::CompareKind Result |
| 1750 | = ImplicitConversionSequence::Indistinguishable; |
| 1751 | |
| 1752 | if (SCS1.Second != SCS2.Second) { |
| 1753 | if (SCS1.Second == ICK_Identity) |
| 1754 | Result = ImplicitConversionSequence::Better; |
| 1755 | else if (SCS2.Second == ICK_Identity) |
| 1756 | Result = ImplicitConversionSequence::Worse; |
| 1757 | else |
| 1758 | return ImplicitConversionSequence::Indistinguishable; |
| 1759 | } else if (!Context.hasSameType(SCS1.getToType(1), SCS2.getToType(1))) |
| 1760 | return ImplicitConversionSequence::Indistinguishable; |
| 1761 | |
| 1762 | if (SCS1.Third == SCS2.Third) { |
| 1763 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 1764 | : ImplicitConversionSequence::Indistinguishable; |
| 1765 | } |
| 1766 | |
| 1767 | if (SCS1.Third == ICK_Identity) |
| 1768 | return Result == ImplicitConversionSequence::Worse |
| 1769 | ? ImplicitConversionSequence::Indistinguishable |
| 1770 | : ImplicitConversionSequence::Better; |
| 1771 | |
| 1772 | if (SCS2.Third == ICK_Identity) |
| 1773 | return Result == ImplicitConversionSequence::Better |
| 1774 | ? ImplicitConversionSequence::Indistinguishable |
| 1775 | : ImplicitConversionSequence::Worse; |
| 1776 | |
| 1777 | return ImplicitConversionSequence::Indistinguishable; |
| 1778 | } |
| 1779 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1780 | /// CompareStandardConversionSequences - Compare two standard |
| 1781 | /// conversion sequences to determine whether one is better than the |
| 1782 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1784 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1785 | const StandardConversionSequence& SCS2) |
| 1786 | { |
| 1787 | // Standard conversion sequence S1 is a better conversion sequence |
| 1788 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1789 | |
| 1790 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1791 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1792 | // excluding any Lvalue Transformation; the identity conversion |
| 1793 | // sequence is considered to be a subsequence of any |
| 1794 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1795 | if (ImplicitConversionSequence::CompareKind CK |
| 1796 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 1797 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1798 | |
| 1799 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1800 | // defined below), or, if not that, |
| 1801 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1802 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1803 | if (Rank1 < Rank2) |
| 1804 | return ImplicitConversionSequence::Better; |
| 1805 | else if (Rank2 < Rank1) |
| 1806 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1808 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1809 | // are indistinguishable unless one of the following rules |
| 1810 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1812 | // A conversion that is not a conversion of a pointer, or |
| 1813 | // pointer to member, to bool is better than another conversion |
| 1814 | // that is such a conversion. |
| 1815 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1816 | return SCS2.isPointerConversionToBool() |
| 1817 | ? ImplicitConversionSequence::Better |
| 1818 | : ImplicitConversionSequence::Worse; |
| 1819 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1820 | // C++ [over.ics.rank]p4b2: |
| 1821 | // |
| 1822 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1823 | // conversion of B* to A* is better than conversion of B* to |
| 1824 | // void*, and conversion of A* to void* is better than conversion |
| 1825 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1827 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1829 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1830 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1831 | // Exactly one of the conversion sequences is a conversion to |
| 1832 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1833 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1834 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1835 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1836 | // Neither conversion sequence converts to a void pointer; compare |
| 1837 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1838 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1839 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1840 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1841 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1842 | // Both conversion sequences are conversions to void |
| 1843 | // pointers. Compare the source types to determine if there's an |
| 1844 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1845 | QualType FromType1 = SCS1.getFromType(); |
| 1846 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1847 | |
| 1848 | // Adjust the types we're converting from via the array-to-pointer |
| 1849 | // conversion, if we need to. |
| 1850 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1851 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1852 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1853 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1854 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1855 | QualType FromPointee1 |
| 1856 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1857 | QualType FromPointee2 |
| 1858 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1860 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1861 | return ImplicitConversionSequence::Better; |
| 1862 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1863 | return ImplicitConversionSequence::Worse; |
| 1864 | |
| 1865 | // Objective-C++: If one interface is more specific than the |
| 1866 | // other, it is the better one. |
| 1867 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1868 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1869 | if (FromIface1 && FromIface1) { |
| 1870 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1871 | return ImplicitConversionSequence::Better; |
| 1872 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1873 | return ImplicitConversionSequence::Worse; |
| 1874 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1875 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1878 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1880 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1881 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1882 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1883 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1884 | // C++0x [over.ics.rank]p3b4: |
| 1885 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1886 | // implicit object parameter of a non-static member function declared |
| 1887 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1888 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1889 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1890 | // or if the member function in this case has a ref qualifier. |
| 1891 | // (Of course, we don't have ref qualifiers yet.) |
| 1892 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1893 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1894 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1895 | |
| 1896 | // C++ [over.ics.rank]p3b4: |
| 1897 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1898 | // which the references refer are the same type except for |
| 1899 | // top-level cv-qualifiers, and the type to which the reference |
| 1900 | // initialized by S2 refers is more cv-qualified than the type |
| 1901 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1902 | QualType T1 = SCS1.getToType(2); |
| 1903 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1904 | T1 = Context.getCanonicalType(T1); |
| 1905 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1906 | Qualifiers T1Quals, T2Quals; |
| 1907 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1908 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1909 | if (UnqualT1 == UnqualT2) { |
| 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); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1916 | if (T2.isMoreQualifiedThan(T1)) |
| 1917 | return ImplicitConversionSequence::Better; |
| 1918 | else if (T1.isMoreQualifiedThan(T2)) |
| 1919 | return ImplicitConversionSequence::Worse; |
| 1920 | } |
| 1921 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1922 | |
| 1923 | return ImplicitConversionSequence::Indistinguishable; |
| 1924 | } |
| 1925 | |
| 1926 | /// CompareQualificationConversions - Compares two standard conversion |
| 1927 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1929 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1930 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1932 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1933 | // -- S1 and S2 differ only in their qualification conversion and |
| 1934 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1935 | // cv-qualification signature of type T1 is a proper subset of |
| 1936 | // the cv-qualification signature of type T2, and S1 is not the |
| 1937 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1938 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1939 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1940 | return ImplicitConversionSequence::Indistinguishable; |
| 1941 | |
| 1942 | // FIXME: the example in the standard doesn't use a qualification |
| 1943 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1944 | QualType T1 = SCS1.getToType(2); |
| 1945 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1946 | T1 = Context.getCanonicalType(T1); |
| 1947 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1948 | Qualifiers T1Quals, T2Quals; |
| 1949 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1950 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1951 | |
| 1952 | // If the types are the same, we won't learn anything by unwrapped |
| 1953 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1954 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1955 | return ImplicitConversionSequence::Indistinguishable; |
| 1956 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1957 | // If the type is an array type, promote the element qualifiers to the type |
| 1958 | // for comparison. |
| 1959 | if (isa<ArrayType>(T1) && T1Quals) |
| 1960 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1961 | if (isa<ArrayType>(T2) && T2Quals) |
| 1962 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1963 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1965 | = ImplicitConversionSequence::Indistinguishable; |
| 1966 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1967 | // Within each iteration of the loop, we check the qualifiers to |
| 1968 | // determine if this still looks like a qualification |
| 1969 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1970 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1971 | // until there are no more pointers or pointers-to-members left |
| 1972 | // to unwrap. This essentially mimics what |
| 1973 | // IsQualificationConversion does, but here we're checking for a |
| 1974 | // strict subset of qualifiers. |
| 1975 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1976 | // The qualifiers are the same, so this doesn't tell us anything |
| 1977 | // about how the sequences rank. |
| 1978 | ; |
| 1979 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1980 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1981 | if (Result == ImplicitConversionSequence::Worse) |
| 1982 | // Neither has qualifiers that are a subset of the other's |
| 1983 | // qualifiers. |
| 1984 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1986 | Result = ImplicitConversionSequence::Better; |
| 1987 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1988 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1989 | if (Result == ImplicitConversionSequence::Better) |
| 1990 | // Neither has qualifiers that are a subset of the other's |
| 1991 | // qualifiers. |
| 1992 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1994 | Result = ImplicitConversionSequence::Worse; |
| 1995 | } else { |
| 1996 | // Qualifiers are disjoint. |
| 1997 | return ImplicitConversionSequence::Indistinguishable; |
| 1998 | } |
| 1999 | |
| 2000 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2001 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2002 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2005 | // Check that the winning standard conversion sequence isn't using |
| 2006 | // the deprecated string literal array to pointer conversion. |
| 2007 | switch (Result) { |
| 2008 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2009 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2010 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2011 | break; |
| 2012 | |
| 2013 | case ImplicitConversionSequence::Indistinguishable: |
| 2014 | break; |
| 2015 | |
| 2016 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2017 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2018 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2019 | break; |
| 2020 | } |
| 2021 | |
| 2022 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2025 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2026 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2027 | /// various kinds of derived-to-base conversions (C++ |
| 2028 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2029 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2030 | ImplicitConversionSequence::CompareKind |
| 2031 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2032 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2033 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2034 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2035 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2036 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Adjust the types we're converting from via the array-to-pointer |
| 2039 | // conversion, if we need to. |
| 2040 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2041 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2042 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2043 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2044 | |
| 2045 | // Canonicalize all of the types. |
| 2046 | FromType1 = Context.getCanonicalType(FromType1); |
| 2047 | ToType1 = Context.getCanonicalType(ToType1); |
| 2048 | FromType2 = Context.getCanonicalType(FromType2); |
| 2049 | ToType2 = Context.getCanonicalType(ToType2); |
| 2050 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2051 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2052 | // |
| 2053 | // If class B is derived directly or indirectly from class A and |
| 2054 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2055 | // |
| 2056 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2057 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2061 | SCS2.Second == ICK_Pointer_Conversion && |
| 2062 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2063 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2064 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2066 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2068 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2069 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2070 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2071 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2072 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2073 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2074 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2075 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2076 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2077 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2079 | // -- 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] | 2080 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2081 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2082 | return ImplicitConversionSequence::Better; |
| 2083 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2084 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (ToIface1 && ToIface2) { |
| 2087 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2088 | return ImplicitConversionSequence::Better; |
| 2089 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2090 | return ImplicitConversionSequence::Worse; |
| 2091 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2092 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2093 | |
| 2094 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2095 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2096 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2097 | return ImplicitConversionSequence::Better; |
| 2098 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2099 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2101 | if (FromIface1 && FromIface2) { |
| 2102 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2103 | return ImplicitConversionSequence::Better; |
| 2104 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2105 | return ImplicitConversionSequence::Worse; |
| 2106 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2107 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2110 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2111 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2112 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2113 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2114 | const MemberPointerType * FromMemPointer1 = |
| 2115 | FromType1->getAs<MemberPointerType>(); |
| 2116 | const MemberPointerType * ToMemPointer1 = |
| 2117 | ToType1->getAs<MemberPointerType>(); |
| 2118 | const MemberPointerType * FromMemPointer2 = |
| 2119 | FromType2->getAs<MemberPointerType>(); |
| 2120 | const MemberPointerType * ToMemPointer2 = |
| 2121 | ToType2->getAs<MemberPointerType>(); |
| 2122 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2123 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2124 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2125 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2126 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2127 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2128 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2129 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2130 | // 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] | 2131 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2132 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2133 | return ImplicitConversionSequence::Worse; |
| 2134 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2135 | return ImplicitConversionSequence::Better; |
| 2136 | } |
| 2137 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2138 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2139 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2140 | return ImplicitConversionSequence::Better; |
| 2141 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2142 | return ImplicitConversionSequence::Worse; |
| 2143 | } |
| 2144 | } |
| 2145 | |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2146 | if ((SCS1.ReferenceBinding || SCS1.CopyConstructor) && |
| 2147 | (SCS2.ReferenceBinding || SCS2.CopyConstructor) && |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2148 | SCS1.Second == ICK_Derived_To_Base) { |
| 2149 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2150 | // -- binding of an expression of type C to a reference of type |
| 2151 | // B& is better than binding an expression of type C to a |
| 2152 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2153 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2154 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2155 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2156 | return ImplicitConversionSequence::Better; |
| 2157 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2158 | return ImplicitConversionSequence::Worse; |
| 2159 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2161 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 9e23932 | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2162 | // -- binding of an expression of type B to a reference of type |
| 2163 | // A& is better than binding an expression of type C to a |
| 2164 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2165 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2166 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2167 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2168 | return ImplicitConversionSequence::Better; |
| 2169 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2170 | return ImplicitConversionSequence::Worse; |
| 2171 | } |
| 2172 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2173 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2174 | return ImplicitConversionSequence::Indistinguishable; |
| 2175 | } |
| 2176 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2177 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2178 | /// ToType from the expression From. Return the implicit conversion |
| 2179 | /// sequence required to pass this argument, which may be a bad |
| 2180 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2181 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2182 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2183 | /// 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] | 2184 | ImplicitConversionSequence |
| 2185 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2186 | bool SuppressUserConversions, bool ForceRValue, |
| 2187 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2188 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2189 | ImplicitConversionSequence ICS; |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2190 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | CheckReferenceInit(From, ToType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2192 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2193 | SuppressUserConversions, |
| 2194 | /*AllowExplicit=*/false, |
| 2195 | ForceRValue, |
| 2196 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2197 | return ICS; |
| 2198 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2200 | SuppressUserConversions, |
| 2201 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2202 | ForceRValue, |
| 2203 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2207 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2208 | /// parameter of the given member function (@c Method) from the |
| 2209 | /// expression @p From. |
| 2210 | ImplicitConversionSequence |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2211 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2212 | CXXMethodDecl *Method, |
| 2213 | CXXRecordDecl *ActingContext) { |
| 2214 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2215 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2216 | // const volatile object. |
| 2217 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2218 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2219 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2220 | |
| 2221 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2222 | // to exit early. |
| 2223 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2224 | |
| 2225 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2226 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2227 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2228 | FromType = PT->getPointeeType(); |
| 2229 | |
| 2230 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2231 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2232 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2233 | // where X is the class of which the function is a member |
| 2234 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2235 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2237 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2238 | // reference binding here, that allows class rvalues to bind to |
| 2239 | // non-constant references. |
| 2240 | |
| 2241 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2242 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2243 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2244 | if (ImplicitParamType.getCVRQualifiers() |
| 2245 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2246 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2247 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2248 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2249 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2250 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2251 | |
| 2252 | // Check that we have either the same type or a derived type. It |
| 2253 | // affects the conversion rank. |
| 2254 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2255 | ImplicitConversionKind SecondKind; |
| 2256 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2257 | SecondKind = ICK_Identity; |
| 2258 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2259 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2260 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2261 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2262 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2263 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2264 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2265 | |
| 2266 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2267 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2268 | ICS.Standard.setAsIdentityConversion(); |
| 2269 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2270 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2271 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2272 | ICS.Standard.ReferenceBinding = true; |
| 2273 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2274 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2275 | return ICS; |
| 2276 | } |
| 2277 | |
| 2278 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2279 | /// the implicit object parameter for the given Method with the given |
| 2280 | /// expression. |
| 2281 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2282 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 2283 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2284 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2285 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2286 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2288 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2290 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2291 | FromRecordType = PT->getPointeeType(); |
| 2292 | DestType = Method->getThisType(Context); |
| 2293 | } else { |
| 2294 | FromRecordType = From->getType(); |
| 2295 | DestType = ImplicitParamRecordType; |
| 2296 | } |
| 2297 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2298 | // Note that we always use the true parent context when performing |
| 2299 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | ImplicitConversionSequence ICS |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2301 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2302 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2303 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2304 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2305 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2306 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2308 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2309 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2310 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2311 | if (!Context.hasSameType(From->getType(), DestType)) |
| 2312 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
| 2313 | /*isLvalue=*/!From->getType()->getAs<PointerType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2314 | return false; |
| 2315 | } |
| 2316 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2317 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2318 | /// expression From to bool (C++0x [conv]p3). |
| 2319 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2321 | // FIXME: Are these flags correct? |
| 2322 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2324 | /*ForceRValue=*/false, |
| 2325 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2329 | /// of the expression From to bool (C++0x [conv]p3). |
| 2330 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2331 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2332 | if (!ICS.isBad()) |
| 2333 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2334 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2335 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2336 | return Diag(From->getSourceRange().getBegin(), |
| 2337 | diag::err_typecheck_bool_condition) |
| 2338 | << From->getType() << From->getSourceRange(); |
| 2339 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2342 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2343 | /// candidate functions, using the given function call arguments. If |
| 2344 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2345 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2346 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2347 | /// hacky way to implement the overloading rules for elidable copy |
| 2348 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2349 | /// |
| 2350 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2351 | /// based on an incomplete set of function arguments. This feature is used by |
| 2352 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | void |
| 2354 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2355 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2356 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2357 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2358 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2359 | bool ForceRValue, |
| 2360 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2362 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2363 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2365 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2367 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2368 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2369 | // If we get here, it's because we're calling a member function |
| 2370 | // that is named without a member access expression (e.g., |
| 2371 | // "this->f") that was either written explicitly or created |
| 2372 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2373 | // function, e.g., X::f(). We use an empty type for the implied |
| 2374 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2375 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2376 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2377 | QualType(), Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2378 | SuppressUserConversions, ForceRValue); |
| 2379 | return; |
| 2380 | } |
| 2381 | // We treat a constructor like a non-member function, since its object |
| 2382 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2385 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2386 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2387 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2388 | // Overload resolution is always an unevaluated context. |
| 2389 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2390 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2391 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2392 | // C++ [class.copy]p3: |
| 2393 | // A member function template is never instantiated to perform the copy |
| 2394 | // of a class object to an object of its class type. |
| 2395 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2396 | if (NumArgs == 1 && |
| 2397 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 2398 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 2399 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2400 | return; |
| 2401 | } |
| 2402 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2403 | // Add this candidate |
| 2404 | CandidateSet.push_back(OverloadCandidate()); |
| 2405 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2406 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2407 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2408 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2409 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2410 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2411 | |
| 2412 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2413 | |
| 2414 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2415 | // parameters is viable only if it has an ellipsis in its parameter |
| 2416 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2417 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2418 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2419 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2420 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2421 | return; |
| 2422 | } |
| 2423 | |
| 2424 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2425 | // is viable only if the (m+1)st parameter has a default argument |
| 2426 | // (8.3.6). For the purposes of overload resolution, the |
| 2427 | // parameter list is truncated on the right, so that there are |
| 2428 | // exactly m parameters. |
| 2429 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2430 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2431 | // Not enough arguments. |
| 2432 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2433 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2434 | return; |
| 2435 | } |
| 2436 | |
| 2437 | // Determine the implicit conversion sequences for each of the |
| 2438 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2439 | Candidate.Conversions.resize(NumArgs); |
| 2440 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2441 | if (ArgIdx < NumArgsInProto) { |
| 2442 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2443 | // exist for each argument an implicit conversion sequence |
| 2444 | // (13.3.3.1) that converts that argument to the corresponding |
| 2445 | // parameter of F. |
| 2446 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | Candidate.Conversions[ArgIdx] |
| 2448 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2449 | SuppressUserConversions, ForceRValue, |
| 2450 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2451 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2452 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2453 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2454 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2455 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2456 | } else { |
| 2457 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2458 | // argument for which there is no corresponding parameter is |
| 2459 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2460 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | } |
| 2464 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2465 | /// \brief Add all of the function declarations in the given function set to |
| 2466 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2467 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2468 | Expr **Args, unsigned NumArgs, |
| 2469 | OverloadCandidateSet& CandidateSet, |
| 2470 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2471 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2472 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 2473 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2474 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2475 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2476 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2477 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2478 | CandidateSet, SuppressUserConversions); |
| 2479 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2480 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2481 | SuppressUserConversions); |
| 2482 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2483 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2484 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2485 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2486 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2487 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2488 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2489 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2490 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2491 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2492 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2493 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2494 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2495 | Args, NumArgs, CandidateSet, |
| 2496 | SuppressUserConversions); |
| 2497 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2498 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2501 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2502 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2503 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2504 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2505 | Expr **Args, unsigned NumArgs, |
| 2506 | OverloadCandidateSet& CandidateSet, |
| 2507 | bool SuppressUserConversions, bool ForceRValue) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2508 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2509 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2510 | |
| 2511 | if (isa<UsingShadowDecl>(Decl)) |
| 2512 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2513 | |
| 2514 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2515 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2516 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2517 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 2518 | /*ExplicitArgs*/ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2519 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2520 | CandidateSet, |
| 2521 | SuppressUserConversions, |
| 2522 | ForceRValue); |
| 2523 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2524 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2525 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2526 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2527 | } |
| 2528 | } |
| 2529 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2530 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2531 | /// of candidate functions, using the given function call arguments |
| 2532 | /// and the object argument (@c Object). For example, in a call |
| 2533 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2534 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2535 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2536 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2537 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2538 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2540 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2541 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 2542 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2543 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | bool SuppressUserConversions, bool ForceRValue) { |
| 2545 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2546 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2547 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2548 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2549 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2550 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2551 | if (!CandidateSet.isNewCandidate(Method)) |
| 2552 | return; |
| 2553 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2554 | // Overload resolution is always an unevaluated context. |
| 2555 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2556 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2557 | // Add this candidate |
| 2558 | CandidateSet.push_back(OverloadCandidate()); |
| 2559 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2560 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2561 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2562 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2563 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2564 | |
| 2565 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2566 | |
| 2567 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2568 | // parameters is viable only if it has an ellipsis in its parameter |
| 2569 | // list (8.3.5). |
| 2570 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2571 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2572 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2577 | // is viable only if the (m+1)st parameter has a default argument |
| 2578 | // (8.3.6). For the purposes of overload resolution, the |
| 2579 | // parameter list is truncated on the right, so that there are |
| 2580 | // exactly m parameters. |
| 2581 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2582 | if (NumArgs < MinRequiredArgs) { |
| 2583 | // Not enough arguments. |
| 2584 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2585 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | Candidate.Viable = true; |
| 2590 | Candidate.Conversions.resize(NumArgs + 1); |
| 2591 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2592 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2593 | // The implicit object argument is ignored. |
| 2594 | Candidate.IgnoreObjectArgument = true; |
| 2595 | else { |
| 2596 | // Determine the implicit conversion sequence for the object |
| 2597 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2598 | Candidate.Conversions[0] |
| 2599 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2600 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2601 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2602 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2603 | return; |
| 2604 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | // Determine the implicit conversion sequences for each of the |
| 2608 | // arguments. |
| 2609 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2610 | if (ArgIdx < NumArgsInProto) { |
| 2611 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2612 | // exist for each argument an implicit conversion sequence |
| 2613 | // (13.3.3.1) that converts that argument to the corresponding |
| 2614 | // parameter of F. |
| 2615 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | Candidate.Conversions[ArgIdx + 1] |
| 2617 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2618 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2619 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2620 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2621 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2622 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2623 | break; |
| 2624 | } |
| 2625 | } else { |
| 2626 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2627 | // argument for which there is no corresponding parameter is |
| 2628 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2629 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2634 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2635 | /// set, using template argument deduction to produce an appropriate member |
| 2636 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2638 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2639 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2640 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2641 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2642 | QualType ObjectType, |
| 2643 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2644 | OverloadCandidateSet& CandidateSet, |
| 2645 | bool SuppressUserConversions, |
| 2646 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2647 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2648 | return; |
| 2649 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2650 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2652 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | // 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] | 2654 | // candidate functions in the usual way.113) A given name can refer to one |
| 2655 | // or more function templates and also to a set of overloaded non-template |
| 2656 | // functions. In such a case, the candidate functions generated from each |
| 2657 | // function template are combined with the set of non-template candidate |
| 2658 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2659 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2660 | FunctionDecl *Specialization = 0; |
| 2661 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2662 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2663 | Args, NumArgs, Specialization, Info)) { |
| 2664 | // FIXME: Record what happened with template argument deduction, so |
| 2665 | // that we can give the user a beautiful diagnostic. |
| 2666 | (void)Result; |
| 2667 | return; |
| 2668 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2670 | // Add the function template specialization produced by template argument |
| 2671 | // deduction as a candidate. |
| 2672 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2674 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2675 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2676 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2677 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2678 | } |
| 2679 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2680 | /// \brief Add a C++ function template specialization as a candidate |
| 2681 | /// in the candidate set, using template argument deduction to produce |
| 2682 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2684 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2685 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2686 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2687 | Expr **Args, unsigned NumArgs, |
| 2688 | OverloadCandidateSet& CandidateSet, |
| 2689 | bool SuppressUserConversions, |
| 2690 | bool ForceRValue) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2691 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2692 | return; |
| 2693 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2694 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2696 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | // 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] | 2698 | // candidate functions in the usual way.113) A given name can refer to one |
| 2699 | // or more function templates and also to a set of overloaded non-template |
| 2700 | // functions. In such a case, the candidate functions generated from each |
| 2701 | // function template are combined with the set of non-template candidate |
| 2702 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2703 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2704 | FunctionDecl *Specialization = 0; |
| 2705 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2706 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2707 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2708 | CandidateSet.push_back(OverloadCandidate()); |
| 2709 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2710 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2711 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 2712 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2713 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2714 | Candidate.IsSurrogate = false; |
| 2715 | Candidate.IgnoreObjectArgument = false; |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 2716 | |
| 2717 | // TODO: record more information about failed template arguments |
| 2718 | Candidate.DeductionFailure.Result = Result; |
| 2719 | Candidate.DeductionFailure.TemplateParameter = Info.Param.getOpaqueValue(); |
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?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2726 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 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 | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2738 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2739 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2740 | Expr *From, QualType ToType, |
| 2741 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2742 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2743 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2744 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2745 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2746 | return; |
| 2747 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2748 | // Overload resolution is always an unevaluated context. |
| 2749 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2750 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2751 | // Add this candidate |
| 2752 | CandidateSet.push_back(OverloadCandidate()); |
| 2753 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2754 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2755 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2756 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2757 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2758 | Candidate.FinalConversion.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2759 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2760 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2762 | // Determine the implicit conversion sequence for the implicit |
| 2763 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2764 | Candidate.Viable = true; |
| 2765 | Candidate.Conversions.resize(1); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2766 | Candidate.Conversions[0] |
| 2767 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2768 | ActingContext); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2769 | // Conversion functions to a different type in the base class is visible in |
| 2770 | // the derived class. So, a derived to base conversion should not participate |
| 2771 | // in overload resolution. |
| 2772 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2773 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2774 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2775 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2776 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2777 | return; |
| 2778 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2779 | |
| 2780 | // We won't go through a user-define type conversion function to convert a |
| 2781 | // derived to base as such conversions are given Conversion Rank. They only |
| 2782 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2783 | QualType FromCanon |
| 2784 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2785 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2786 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2787 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2788 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2789 | return; |
| 2790 | } |
| 2791 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2792 | |
| 2793 | // To determine what the conversion from the result of calling the |
| 2794 | // conversion function to the type we're eventually trying to |
| 2795 | // convert to (ToType), we need to synthesize a call to the |
| 2796 | // conversion function and attempt copy initialization from it. This |
| 2797 | // makes sure that we get the right semantics with respect to |
| 2798 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2799 | // call on the stack and we don't need its arguments to be |
| 2800 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2802 | From->getLocStart()); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2803 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2804 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2805 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
| 2807 | // 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] | 2808 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2809 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2811 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2812 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | ImplicitConversionSequence ICS = |
| 2814 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2815 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2816 | /*ForceRValue=*/false, |
| 2817 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2819 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2820 | case ImplicitConversionSequence::StandardConversion: |
| 2821 | Candidate.FinalConversion = ICS.Standard; |
| 2822 | break; |
| 2823 | |
| 2824 | case ImplicitConversionSequence::BadConversion: |
| 2825 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2826 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | |
| 2829 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2831 | "Can only end up with a standard conversion sequence or failure"); |
| 2832 | } |
| 2833 | } |
| 2834 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2835 | /// \brief Adds a conversion function template specialization |
| 2836 | /// candidate to the overload set, using template argument deduction |
| 2837 | /// to deduce the template arguments of the conversion function |
| 2838 | /// template from the type that we are converting to (C++ |
| 2839 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2841 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2842 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2843 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2844 | Expr *From, QualType ToType, |
| 2845 | OverloadCandidateSet &CandidateSet) { |
| 2846 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2847 | "Only conversion function templates permitted here"); |
| 2848 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2849 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2850 | return; |
| 2851 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2852 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2853 | CXXConversionDecl *Specialization = 0; |
| 2854 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2856 | Specialization, Info)) { |
| 2857 | // FIXME: Record what happened with template argument deduction, so |
| 2858 | // that we can give the user a beautiful diagnostic. |
| 2859 | (void)Result; |
| 2860 | return; |
| 2861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2863 | // Add the conversion function template specialization produced by |
| 2864 | // template argument deduction as a candidate. |
| 2865 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2866 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2867 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2870 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2871 | /// converts the given @c Object to a function pointer via the |
| 2872 | /// conversion function @c Conversion, and then attempts to call it |
| 2873 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2874 | /// the type of function that we'll eventually be calling. |
| 2875 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2876 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2877 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2878 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2879 | QualType ObjectType, |
| 2880 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2881 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2882 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2883 | return; |
| 2884 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2885 | // Overload resolution is always an unevaluated context. |
| 2886 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2887 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2888 | CandidateSet.push_back(OverloadCandidate()); |
| 2889 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2890 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2891 | Candidate.Function = 0; |
| 2892 | Candidate.Surrogate = Conversion; |
| 2893 | Candidate.Viable = true; |
| 2894 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2895 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2896 | Candidate.Conversions.resize(NumArgs + 1); |
| 2897 | |
| 2898 | // Determine the implicit conversion sequence for the implicit |
| 2899 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | ImplicitConversionSequence ObjectInit |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2901 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2902 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2903 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2904 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2905 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2906 | return; |
| 2907 | } |
| 2908 | |
| 2909 | // The first conversion is actually a user-defined conversion whose |
| 2910 | // first conversion is ObjectInit's standard conversion (which is |
| 2911 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2912 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2913 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2914 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2915 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2917 | = Candidate.Conversions[0].UserDefined.Before; |
| 2918 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2919 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2921 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2922 | |
| 2923 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2924 | // parameters is viable only if it has an ellipsis in its parameter |
| 2925 | // list (8.3.5). |
| 2926 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2927 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2928 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2929 | return; |
| 2930 | } |
| 2931 | |
| 2932 | // Function types don't have any default arguments, so just check if |
| 2933 | // we have enough arguments. |
| 2934 | if (NumArgs < NumArgsInProto) { |
| 2935 | // Not enough arguments. |
| 2936 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2937 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | // Determine the implicit conversion sequences for each of the |
| 2942 | // arguments. |
| 2943 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2944 | if (ArgIdx < NumArgsInProto) { |
| 2945 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2946 | // exist for each argument an implicit conversion sequence |
| 2947 | // (13.3.3.1) that converts that argument to the corresponding |
| 2948 | // parameter of F. |
| 2949 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | Candidate.Conversions[ArgIdx + 1] |
| 2951 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2952 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2953 | /*ForceRValue=*/false, |
| 2954 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2955 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2956 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2957 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2958 | break; |
| 2959 | } |
| 2960 | } else { |
| 2961 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2962 | // argument for which there is no corresponding parameter is |
| 2963 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2964 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | } |
| 2968 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2969 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2970 | // operator overloading logic over to the scheme used by binary operators, which |
| 2971 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2972 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2973 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2974 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2975 | OverloadCandidateSet& CandidateSet, |
| 2976 | SourceRange OpRange) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2977 | UnresolvedSet<16> Fns; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2978 | |
| 2979 | QualType T1 = Args[0]->getType(); |
| 2980 | QualType T2; |
| 2981 | if (NumArgs > 1) |
| 2982 | T2 = Args[1]->getType(); |
| 2983 | |
| 2984 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2985 | if (S) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2986 | LookupOverloadedOperatorName(Op, S, T1, T2, Fns); |
| 2987 | AddFunctionCandidates(Fns, Args, NumArgs, CandidateSet, false); |
| 2988 | AddArgumentDependentLookupCandidates(OpName, false, Args, NumArgs, 0, |
| 2989 | CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2990 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2991 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | /// \brief Add overload candidates for overloaded operators that are |
| 2995 | /// member functions. |
| 2996 | /// |
| 2997 | /// Add the overloaded operator candidates that are member functions |
| 2998 | /// for the operator Op that was used in an operator expression such |
| 2999 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3000 | /// CandidateSet will store the added overload candidates. (C++ |
| 3001 | /// [over.match.oper]). |
| 3002 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3003 | SourceLocation OpLoc, |
| 3004 | Expr **Args, unsigned NumArgs, |
| 3005 | OverloadCandidateSet& CandidateSet, |
| 3006 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3007 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3008 | |
| 3009 | // C++ [over.match.oper]p3: |
| 3010 | // For a unary operator @ with an operand of a type whose |
| 3011 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3012 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3013 | // a right operand of a type whose cv-unqualified version is T2, |
| 3014 | // three sets of candidate functions, designated member |
| 3015 | // candidates, non-member candidates and built-in candidates, are |
| 3016 | // constructed as follows: |
| 3017 | QualType T1 = Args[0]->getType(); |
| 3018 | QualType T2; |
| 3019 | if (NumArgs > 1) |
| 3020 | T2 = Args[1]->getType(); |
| 3021 | |
| 3022 | // -- If T1 is a class type, the set of member candidates is the |
| 3023 | // result of the qualified lookup of T1::operator@ |
| 3024 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3025 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3026 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3027 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3028 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3029 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3030 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3031 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3032 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3033 | Operators.suppressDiagnostics(); |
| 3034 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3036 | OperEnd = Operators.end(); |
| 3037 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3038 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3039 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3040 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3041 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3042 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3045 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3046 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3047 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3048 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3049 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3050 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3051 | /// (at the beginning of the argument list) that will be contextually |
| 3052 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3054 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3055 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3056 | bool IsAssignmentOperator, |
| 3057 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3058 | // Overload resolution is always an unevaluated context. |
| 3059 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3060 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3061 | // Add this candidate |
| 3062 | CandidateSet.push_back(OverloadCandidate()); |
| 3063 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3064 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3065 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3066 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3067 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3068 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3069 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3070 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3071 | |
| 3072 | // Determine the implicit conversion sequences for each of the |
| 3073 | // arguments. |
| 3074 | Candidate.Viable = true; |
| 3075 | Candidate.Conversions.resize(NumArgs); |
| 3076 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3077 | // C++ [over.match.oper]p4: |
| 3078 | // For the built-in assignment operators, conversions of the |
| 3079 | // left operand are restricted as follows: |
| 3080 | // -- no temporaries are introduced to hold the left operand, and |
| 3081 | // -- no user-defined conversions are applied to the left |
| 3082 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3083 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3084 | // |
| 3085 | // We block these conversions by turning off user-defined |
| 3086 | // conversions, since that is the only way that initialization of |
| 3087 | // a reference to a non-class type can occur from something that |
| 3088 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3089 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3091 | "Contextual conversion to bool requires bool type"); |
| 3092 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3093 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | Candidate.Conversions[ArgIdx] |
| 3095 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3096 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3097 | /*ForceRValue=*/false, |
| 3098 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3099 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3100 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3101 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3102 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3103 | break; |
| 3104 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3109 | /// candidate operator functions for built-in operators (C++ |
| 3110 | /// [over.built]). The types are separated into pointer types and |
| 3111 | /// enumeration types. |
| 3112 | class BuiltinCandidateTypeSet { |
| 3113 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3114 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3115 | |
| 3116 | /// PointerTypes - The set of pointer types that will be used in the |
| 3117 | /// built-in candidates. |
| 3118 | TypeSet PointerTypes; |
| 3119 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3120 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3121 | /// used in the built-in candidates. |
| 3122 | TypeSet MemberPointerTypes; |
| 3123 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3124 | /// EnumerationTypes - The set of enumeration types that will be |
| 3125 | /// used in the built-in candidates. |
| 3126 | TypeSet EnumerationTypes; |
| 3127 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3128 | /// Sema - The semantic analysis instance where we are building the |
| 3129 | /// candidate type set. |
| 3130 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3132 | /// Context - The AST context in which we will build the type sets. |
| 3133 | ASTContext &Context; |
| 3134 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3135 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3136 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3137 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3138 | |
| 3139 | public: |
| 3140 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3141 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3142 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3143 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3144 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3146 | void AddTypesConvertedFrom(QualType Ty, |
| 3147 | SourceLocation Loc, |
| 3148 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3149 | bool AllowExplicitConversions, |
| 3150 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3151 | |
| 3152 | /// pointer_begin - First pointer type found; |
| 3153 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3154 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3155 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3156 | iterator pointer_end() { return PointerTypes.end(); } |
| 3157 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3158 | /// member_pointer_begin - First member pointer type found; |
| 3159 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3160 | |
| 3161 | /// member_pointer_end - Past the last member pointer type found; |
| 3162 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3163 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3164 | /// enumeration_begin - First enumeration type found; |
| 3165 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3166 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3167 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3168 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3169 | }; |
| 3170 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3171 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3172 | /// the set of pointer types along with any more-qualified variants of |
| 3173 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3174 | /// will add "int const *", "int const volatile *", "int const |
| 3175 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3176 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3177 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3178 | /// |
| 3179 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3180 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3181 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3182 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3184 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3185 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3186 | return false; |
| 3187 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3188 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3189 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3190 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3191 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3192 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3193 | // (the qualifier would sink to the element type), and for another, the |
| 3194 | // only overload situation where it matters is subscript or pointer +- int, |
| 3195 | // and those shouldn't have qualifier variants anyway. |
| 3196 | if (PointeeTy->isArrayType()) |
| 3197 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3198 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3199 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3200 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3201 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3202 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3203 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3204 | // Iterate through all strict supersets of BaseCVR. |
| 3205 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3206 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3207 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3208 | // in the types. |
| 3209 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3210 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3211 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3212 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | return true; |
| 3216 | } |
| 3217 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3218 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3219 | /// to the set of pointer types along with any more-qualified variants of |
| 3220 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3221 | /// will add "int const *", "int const volatile *", "int const |
| 3222 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3223 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3224 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3225 | /// |
| 3226 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3227 | bool |
| 3228 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3229 | QualType Ty) { |
| 3230 | // Insert this type. |
| 3231 | if (!MemberPointerTypes.insert(Ty)) |
| 3232 | return false; |
| 3233 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3234 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3235 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3236 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3237 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3238 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3239 | // (the qualifier would sink to the element type), and for another, the |
| 3240 | // only overload situation where it matters is subscript or pointer +- int, |
| 3241 | // and those shouldn't have qualifier variants anyway. |
| 3242 | if (PointeeTy->isArrayType()) |
| 3243 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3244 | const Type *ClassTy = PointerTy->getClass(); |
| 3245 | |
| 3246 | // Iterate through all strict supersets of the pointee type's CVR |
| 3247 | // qualifiers. |
| 3248 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3249 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3250 | if ((CVR | BaseCVR) != CVR) continue; |
| 3251 | |
| 3252 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3253 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | return true; |
| 3257 | } |
| 3258 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3259 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3260 | /// 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] | 3261 | /// primarily interested in pointer types and enumeration types. We also |
| 3262 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3263 | /// AllowUserConversions is true if we should look at the conversion |
| 3264 | /// functions of a class type, and AllowExplicitConversions if we |
| 3265 | /// should also include the explicit conversion functions of a class |
| 3266 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3267 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3268 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3269 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3270 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3271 | bool AllowExplicitConversions, |
| 3272 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3273 | // Only deal with canonical types. |
| 3274 | Ty = Context.getCanonicalType(Ty); |
| 3275 | |
| 3276 | // Look through reference types; they aren't part of the type of an |
| 3277 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3278 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3279 | Ty = RefTy->getPointeeType(); |
| 3280 | |
| 3281 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3282 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3283 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3284 | // If we're dealing with an array type, decay to the pointer. |
| 3285 | if (Ty->isArrayType()) |
| 3286 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3287 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3288 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3289 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3290 | |
| 3291 | // Insert our type, and its more-qualified variants, into the set |
| 3292 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3293 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3294 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3295 | } else if (Ty->isMemberPointerType()) { |
| 3296 | // Member pointers are far easier, since the pointee can't be converted. |
| 3297 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3298 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3299 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3300 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3301 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3302 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3303 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3304 | // No conversion functions in incomplete types. |
| 3305 | return; |
| 3306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3308 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3309 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3310 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3311 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3312 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3313 | NamedDecl *D = I.getDecl(); |
| 3314 | if (isa<UsingShadowDecl>(D)) |
| 3315 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3316 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3318 | // about which builtin types we can convert to. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3319 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3320 | continue; |
| 3321 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3322 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3323 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3324 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3325 | VisibleQuals); |
| 3326 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3332 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3333 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3334 | /// given type to the candidate set. |
| 3335 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3336 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3338 | unsigned NumArgs, |
| 3339 | OverloadCandidateSet &CandidateSet) { |
| 3340 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3342 | // T& operator=(T&, T) |
| 3343 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3344 | ParamTypes[1] = T; |
| 3345 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3346 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3348 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3349 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3350 | ParamTypes[0] |
| 3351 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3352 | ParamTypes[1] = T; |
| 3353 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3355 | } |
| 3356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3358 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3359 | /// 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] | 3360 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3361 | Qualifiers VRQuals; |
| 3362 | const RecordType *TyRec; |
| 3363 | if (const MemberPointerType *RHSMPType = |
| 3364 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3365 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3366 | else |
| 3367 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3368 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3369 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3370 | VRQuals.addVolatile(); |
| 3371 | VRQuals.addRestrict(); |
| 3372 | return VRQuals; |
| 3373 | } |
| 3374 | |
| 3375 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3376 | if (!ClassDecl->hasDefinition()) |
| 3377 | return VRQuals; |
| 3378 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3379 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3380 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3381 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3382 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3383 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3384 | NamedDecl *D = I.getDecl(); |
| 3385 | if (isa<UsingShadowDecl>(D)) |
| 3386 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3387 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3388 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3389 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3390 | CanTy = ResTypeRef->getPointeeType(); |
| 3391 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3392 | // as see them. |
| 3393 | bool done = false; |
| 3394 | while (!done) { |
| 3395 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3396 | CanTy = ResTypePtr->getPointeeType(); |
| 3397 | else if (const MemberPointerType *ResTypeMPtr = |
| 3398 | CanTy->getAs<MemberPointerType>()) |
| 3399 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3400 | else |
| 3401 | done = true; |
| 3402 | if (CanTy.isVolatileQualified()) |
| 3403 | VRQuals.addVolatile(); |
| 3404 | if (CanTy.isRestrictQualified()) |
| 3405 | VRQuals.addRestrict(); |
| 3406 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3407 | return VRQuals; |
| 3408 | } |
| 3409 | } |
| 3410 | } |
| 3411 | return VRQuals; |
| 3412 | } |
| 3413 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3414 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3415 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3416 | /// on the operator @p Op and the arguments given. For example, if the |
| 3417 | /// operator is a binary '+', this routine might add "int |
| 3418 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3419 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3421 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3422 | Expr **Args, unsigned NumArgs, |
| 3423 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3424 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3425 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3426 | // that the first few of these types are the promoted integral |
| 3427 | // types; these types need to be first. |
| 3428 | // FIXME: What about complex? |
| 3429 | const unsigned FirstIntegralType = 0; |
| 3430 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3432 | LastPromotedIntegralType = 13; |
| 3433 | const unsigned FirstPromotedArithmeticType = 7, |
| 3434 | LastPromotedArithmeticType = 16; |
| 3435 | const unsigned NumArithmeticTypes = 16; |
| 3436 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3438 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3439 | Context.SignedCharTy, Context.ShortTy, |
| 3440 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3441 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3442 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3443 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3444 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3445 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3446 | "Invalid first promoted integral type"); |
| 3447 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3448 | == Context.UnsignedLongLongTy && |
| 3449 | "Invalid last promoted integral type"); |
| 3450 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3451 | "Invalid first promoted arithmetic type"); |
| 3452 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3453 | == Context.LongDoubleTy && |
| 3454 | "Invalid last promoted arithmetic type"); |
| 3455 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3456 | // Find all of the types that the arguments can convert to, but only |
| 3457 | // if the operator we're looking at has built-in operator candidates |
| 3458 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3459 | Qualifiers VisibleTypeConversionsQuals; |
| 3460 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3461 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3462 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3463 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3464 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3465 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3466 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3467 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3468 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3469 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3470 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3471 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3472 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3473 | OpLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3474 | true, |
| 3475 | (Op == OO_Exclaim || |
| 3476 | Op == OO_AmpAmp || |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3477 | Op == OO_PipePipe), |
| 3478 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | bool isComparison = false; |
| 3482 | switch (Op) { |
| 3483 | case OO_None: |
| 3484 | case NUM_OVERLOADED_OPERATORS: |
| 3485 | assert(false && "Expected an overloaded operator"); |
| 3486 | break; |
| 3487 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3488 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3490 | goto UnaryStar; |
| 3491 | else |
| 3492 | goto BinaryStar; |
| 3493 | break; |
| 3494 | |
| 3495 | case OO_Plus: // '+' is either unary or binary |
| 3496 | if (NumArgs == 1) |
| 3497 | goto UnaryPlus; |
| 3498 | else |
| 3499 | goto BinaryPlus; |
| 3500 | break; |
| 3501 | |
| 3502 | case OO_Minus: // '-' is either unary or binary |
| 3503 | if (NumArgs == 1) |
| 3504 | goto UnaryMinus; |
| 3505 | else |
| 3506 | goto BinaryMinus; |
| 3507 | break; |
| 3508 | |
| 3509 | case OO_Amp: // '&' is either unary or binary |
| 3510 | if (NumArgs == 1) |
| 3511 | goto UnaryAmp; |
| 3512 | else |
| 3513 | goto BinaryAmp; |
| 3514 | |
| 3515 | case OO_PlusPlus: |
| 3516 | case OO_MinusMinus: |
| 3517 | // C++ [over.built]p3: |
| 3518 | // |
| 3519 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3520 | // is either volatile or empty, there exist candidate operator |
| 3521 | // functions of the form |
| 3522 | // |
| 3523 | // VQ T& operator++(VQ T&); |
| 3524 | // T operator++(VQ T&, int); |
| 3525 | // |
| 3526 | // C++ [over.built]p4: |
| 3527 | // |
| 3528 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3529 | // than bool, and VQ is either volatile or empty, there exist |
| 3530 | // candidate operator functions of the form |
| 3531 | // |
| 3532 | // VQ T& operator--(VQ T&); |
| 3533 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3535 | Arith < NumArithmeticTypes; ++Arith) { |
| 3536 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3538 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3539 | |
| 3540 | // Non-volatile version. |
| 3541 | if (NumArgs == 1) |
| 3542 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3543 | else |
| 3544 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3545 | // heuristic to reduce number of builtin candidates in the set. |
| 3546 | // Add volatile version only if there are conversions to a volatile type. |
| 3547 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3548 | // Volatile version |
| 3549 | ParamTypes[0] |
| 3550 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3551 | if (NumArgs == 1) |
| 3552 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3553 | else |
| 3554 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3555 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | // C++ [over.built]p5: |
| 3559 | // |
| 3560 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3561 | // cv-unqualified object type, and VQ is either volatile or |
| 3562 | // empty, there exist candidate operator functions of the form |
| 3563 | // |
| 3564 | // T*VQ& operator++(T*VQ&); |
| 3565 | // T*VQ& operator--(T*VQ&); |
| 3566 | // T* operator++(T*VQ&, int); |
| 3567 | // T* operator--(T*VQ&, int); |
| 3568 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3569 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3570 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3571 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3572 | continue; |
| 3573 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | QualType ParamTypes[2] = { |
| 3575 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3576 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3578 | // Without volatile |
| 3579 | if (NumArgs == 1) |
| 3580 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3581 | else |
| 3582 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3583 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3584 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3585 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3586 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3587 | ParamTypes[0] |
| 3588 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3589 | if (NumArgs == 1) |
| 3590 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3591 | else |
| 3592 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3593 | } |
| 3594 | } |
| 3595 | break; |
| 3596 | |
| 3597 | UnaryStar: |
| 3598 | // C++ [over.built]p6: |
| 3599 | // For every cv-qualified or cv-unqualified object type T, there |
| 3600 | // exist candidate operator functions of the form |
| 3601 | // |
| 3602 | // T& operator*(T*); |
| 3603 | // |
| 3604 | // C++ [over.built]p7: |
| 3605 | // For every function type T, there exist candidate operator |
| 3606 | // functions of the form |
| 3607 | // T& operator*(T*); |
| 3608 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3609 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3610 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3611 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3612 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3613 | &ParamTy, Args, 1, CandidateSet); |
| 3614 | } |
| 3615 | break; |
| 3616 | |
| 3617 | UnaryPlus: |
| 3618 | // C++ [over.built]p8: |
| 3619 | // For every type T, there exist candidate operator functions of |
| 3620 | // the form |
| 3621 | // |
| 3622 | // T* operator+(T*); |
| 3623 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3624 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3625 | QualType ParamTy = *Ptr; |
| 3626 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3629 | // Fall through |
| 3630 | |
| 3631 | UnaryMinus: |
| 3632 | // C++ [over.built]p9: |
| 3633 | // For every promoted arithmetic type T, there exist candidate |
| 3634 | // operator functions of the form |
| 3635 | // |
| 3636 | // T operator+(T); |
| 3637 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3639 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3640 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3641 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3642 | } |
| 3643 | break; |
| 3644 | |
| 3645 | case OO_Tilde: |
| 3646 | // C++ [over.built]p10: |
| 3647 | // For every promoted integral type T, there exist candidate |
| 3648 | // operator functions of the form |
| 3649 | // |
| 3650 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3652 | Int < LastPromotedIntegralType; ++Int) { |
| 3653 | QualType IntTy = ArithmeticTypes[Int]; |
| 3654 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3655 | } |
| 3656 | break; |
| 3657 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3658 | case OO_New: |
| 3659 | case OO_Delete: |
| 3660 | case OO_Array_New: |
| 3661 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3662 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3663 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3664 | break; |
| 3665 | |
| 3666 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3667 | UnaryAmp: |
| 3668 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3669 | // C++ [over.match.oper]p3: |
| 3670 | // -- For the operator ',', the unary operator '&', or the |
| 3671 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3672 | break; |
| 3673 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3674 | case OO_EqualEqual: |
| 3675 | case OO_ExclaimEqual: |
| 3676 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | // For every pointer to member type T, there exist candidate operator |
| 3678 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3679 | // |
| 3680 | // bool operator==(T,T); |
| 3681 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3683 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3684 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3685 | MemPtr != MemPtrEnd; |
| 3686 | ++MemPtr) { |
| 3687 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3688 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3691 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3693 | case OO_Less: |
| 3694 | case OO_Greater: |
| 3695 | case OO_LessEqual: |
| 3696 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3697 | // C++ [over.built]p15: |
| 3698 | // |
| 3699 | // For every pointer or enumeration type T, there exist |
| 3700 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3702 | // bool operator<(T, T); |
| 3703 | // bool operator>(T, T); |
| 3704 | // bool operator<=(T, T); |
| 3705 | // bool operator>=(T, T); |
| 3706 | // bool operator==(T, T); |
| 3707 | // bool operator!=(T, T); |
| 3708 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3709 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3710 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3711 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3713 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3714 | = CandidateTypes.enumeration_begin(); |
| 3715 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3716 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3717 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3718 | } |
| 3719 | |
| 3720 | // Fall through. |
| 3721 | isComparison = true; |
| 3722 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3723 | BinaryPlus: |
| 3724 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3725 | if (!isComparison) { |
| 3726 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3727 | |
| 3728 | // C++ [over.built]p13: |
| 3729 | // |
| 3730 | // For every cv-qualified or cv-unqualified object type T |
| 3731 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3733 | // T* operator+(T*, ptrdiff_t); |
| 3734 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3735 | // T* operator-(T*, ptrdiff_t); |
| 3736 | // T* operator+(ptrdiff_t, T*); |
| 3737 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3738 | // |
| 3739 | // C++ [over.built]p14: |
| 3740 | // |
| 3741 | // For every T, where T is a pointer to object type, there |
| 3742 | // exist candidate operator functions of the form |
| 3743 | // |
| 3744 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3746 | = CandidateTypes.pointer_begin(); |
| 3747 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3748 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3749 | |
| 3750 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3751 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3752 | |
| 3753 | if (Op == OO_Plus) { |
| 3754 | // T* operator+(ptrdiff_t, T*); |
| 3755 | ParamTypes[0] = ParamTypes[1]; |
| 3756 | ParamTypes[1] = *Ptr; |
| 3757 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3758 | } else { |
| 3759 | // ptrdiff_t operator-(T, T); |
| 3760 | ParamTypes[1] = *Ptr; |
| 3761 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3762 | Args, 2, CandidateSet); |
| 3763 | } |
| 3764 | } |
| 3765 | } |
| 3766 | // Fall through |
| 3767 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3768 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3769 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3770 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3771 | // C++ [over.built]p12: |
| 3772 | // |
| 3773 | // For every pair of promoted arithmetic types L and R, there |
| 3774 | // exist candidate operator functions of the form |
| 3775 | // |
| 3776 | // LR operator*(L, R); |
| 3777 | // LR operator/(L, R); |
| 3778 | // LR operator+(L, R); |
| 3779 | // LR operator-(L, R); |
| 3780 | // bool operator<(L, R); |
| 3781 | // bool operator>(L, R); |
| 3782 | // bool operator<=(L, R); |
| 3783 | // bool operator>=(L, R); |
| 3784 | // bool operator==(L, R); |
| 3785 | // bool operator!=(L, R); |
| 3786 | // |
| 3787 | // where LR is the result of the usual arithmetic conversions |
| 3788 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3789 | // |
| 3790 | // C++ [over.built]p24: |
| 3791 | // |
| 3792 | // For every pair of promoted arithmetic types L and R, there exist |
| 3793 | // candidate operator functions of the form |
| 3794 | // |
| 3795 | // LR operator?(bool, L, R); |
| 3796 | // |
| 3797 | // where LR is the result of the usual arithmetic conversions |
| 3798 | // between types L and R. |
| 3799 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3800 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3801 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3803 | Right < LastPromotedArithmeticType; ++Right) { |
| 3804 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3805 | QualType Result |
| 3806 | = isComparison |
| 3807 | ? Context.BoolTy |
| 3808 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3809 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3810 | } |
| 3811 | } |
| 3812 | break; |
| 3813 | |
| 3814 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3815 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3816 | case OO_Caret: |
| 3817 | case OO_Pipe: |
| 3818 | case OO_LessLess: |
| 3819 | case OO_GreaterGreater: |
| 3820 | // C++ [over.built]p17: |
| 3821 | // |
| 3822 | // For every pair of promoted integral types L and R, there |
| 3823 | // exist candidate operator functions of the form |
| 3824 | // |
| 3825 | // LR operator%(L, R); |
| 3826 | // LR operator&(L, R); |
| 3827 | // LR operator^(L, R); |
| 3828 | // LR operator|(L, R); |
| 3829 | // L operator<<(L, R); |
| 3830 | // L operator>>(L, R); |
| 3831 | // |
| 3832 | // where LR is the result of the usual arithmetic conversions |
| 3833 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3834 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3835 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3837 | Right < LastPromotedIntegralType; ++Right) { |
| 3838 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3839 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3840 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3841 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3842 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3843 | } |
| 3844 | } |
| 3845 | break; |
| 3846 | |
| 3847 | case OO_Equal: |
| 3848 | // C++ [over.built]p20: |
| 3849 | // |
| 3850 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3851 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3852 | // empty, there exist candidate operator functions of the form |
| 3853 | // |
| 3854 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3855 | for (BuiltinCandidateTypeSet::iterator |
| 3856 | Enum = CandidateTypes.enumeration_begin(), |
| 3857 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3858 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3860 | CandidateSet); |
| 3861 | for (BuiltinCandidateTypeSet::iterator |
| 3862 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3863 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3864 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3865 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3866 | CandidateSet); |
| 3867 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3868 | |
| 3869 | case OO_PlusEqual: |
| 3870 | case OO_MinusEqual: |
| 3871 | // C++ [over.built]p19: |
| 3872 | // |
| 3873 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3874 | // volatile or empty, there exist candidate operator functions |
| 3875 | // of the form |
| 3876 | // |
| 3877 | // T*VQ& operator=(T*VQ&, T*); |
| 3878 | // |
| 3879 | // C++ [over.built]p21: |
| 3880 | // |
| 3881 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3882 | // cv-unqualified object type and VQ is either volatile or |
| 3883 | // empty, there exist candidate operator functions of the form |
| 3884 | // |
| 3885 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3886 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3887 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3888 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3889 | QualType ParamTypes[2]; |
| 3890 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3891 | |
| 3892 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3893 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3894 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3895 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3896 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3897 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3898 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3899 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3900 | ParamTypes[0] |
| 3901 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3902 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3903 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3904 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3905 | } |
| 3906 | // Fall through. |
| 3907 | |
| 3908 | case OO_StarEqual: |
| 3909 | case OO_SlashEqual: |
| 3910 | // C++ [over.built]p18: |
| 3911 | // |
| 3912 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3913 | // VQ is either volatile or empty, and R is a promoted |
| 3914 | // arithmetic type, there exist candidate operator functions of |
| 3915 | // the form |
| 3916 | // |
| 3917 | // VQ L& operator=(VQ L&, R); |
| 3918 | // VQ L& operator*=(VQ L&, R); |
| 3919 | // VQ L& operator/=(VQ L&, R); |
| 3920 | // VQ L& operator+=(VQ L&, R); |
| 3921 | // VQ L& operator-=(VQ L&, R); |
| 3922 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3924 | Right < LastPromotedArithmeticType; ++Right) { |
| 3925 | QualType ParamTypes[2]; |
| 3926 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3927 | |
| 3928 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3929 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3930 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3931 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3932 | |
| 3933 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3934 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3935 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3936 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3937 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3938 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3939 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3940 | } |
| 3941 | } |
| 3942 | break; |
| 3943 | |
| 3944 | case OO_PercentEqual: |
| 3945 | case OO_LessLessEqual: |
| 3946 | case OO_GreaterGreaterEqual: |
| 3947 | case OO_AmpEqual: |
| 3948 | case OO_CaretEqual: |
| 3949 | case OO_PipeEqual: |
| 3950 | // C++ [over.built]p22: |
| 3951 | // |
| 3952 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3953 | // is either volatile or empty, and R is a promoted integral |
| 3954 | // type, there exist candidate operator functions of the form |
| 3955 | // |
| 3956 | // VQ L& operator%=(VQ L&, R); |
| 3957 | // VQ L& operator<<=(VQ L&, R); |
| 3958 | // VQ L& operator>>=(VQ L&, R); |
| 3959 | // VQ L& operator&=(VQ L&, R); |
| 3960 | // VQ L& operator^=(VQ L&, R); |
| 3961 | // VQ L& operator|=(VQ L&, R); |
| 3962 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3963 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3964 | Right < LastPromotedIntegralType; ++Right) { |
| 3965 | QualType ParamTypes[2]; |
| 3966 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3967 | |
| 3968 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3969 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3970 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 3971 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3972 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3973 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3974 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 3975 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3976 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3977 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | break; |
| 3981 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3982 | case OO_Exclaim: { |
| 3983 | // C++ [over.operator]p23: |
| 3984 | // |
| 3985 | // There also exist candidate operator functions of the form |
| 3986 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3988 | // bool operator&&(bool, bool); [BELOW] |
| 3989 | // bool operator||(bool, bool); [BELOW] |
| 3990 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3991 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3992 | /*IsAssignmentOperator=*/false, |
| 3993 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3994 | break; |
| 3995 | } |
| 3996 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3997 | case OO_AmpAmp: |
| 3998 | case OO_PipePipe: { |
| 3999 | // C++ [over.operator]p23: |
| 4000 | // |
| 4001 | // There also exist candidate operator functions of the form |
| 4002 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4003 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4004 | // bool operator&&(bool, bool); |
| 4005 | // bool operator||(bool, bool); |
| 4006 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4007 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4008 | /*IsAssignmentOperator=*/false, |
| 4009 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4010 | break; |
| 4011 | } |
| 4012 | |
| 4013 | case OO_Subscript: |
| 4014 | // C++ [over.built]p13: |
| 4015 | // |
| 4016 | // For every cv-qualified or cv-unqualified object type T there |
| 4017 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4019 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4020 | // T& operator[](T*, ptrdiff_t); |
| 4021 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4022 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4023 | // T& operator[](ptrdiff_t, T*); |
| 4024 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4025 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4026 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4027 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4028 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4029 | |
| 4030 | // T& operator[](T*, ptrdiff_t) |
| 4031 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4032 | |
| 4033 | // T& operator[](ptrdiff_t, T*); |
| 4034 | ParamTypes[0] = ParamTypes[1]; |
| 4035 | ParamTypes[1] = *Ptr; |
| 4036 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4037 | } |
| 4038 | break; |
| 4039 | |
| 4040 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4041 | // C++ [over.built]p11: |
| 4042 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4043 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4044 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4045 | // there exist candidate operator functions of the form |
| 4046 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4047 | // where CV12 is the union of CV1 and CV2. |
| 4048 | { |
| 4049 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4050 | CandidateTypes.pointer_begin(); |
| 4051 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4052 | QualType C1Ty = (*Ptr); |
| 4053 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4054 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4055 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4056 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4057 | if (!isa<RecordType>(C1)) |
| 4058 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4059 | // heuristic to reduce number of builtin candidates in the set. |
| 4060 | // Add volatile/restrict version only if there are conversions to a |
| 4061 | // volatile/restrict type. |
| 4062 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4063 | continue; |
| 4064 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4065 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4066 | } |
| 4067 | for (BuiltinCandidateTypeSet::iterator |
| 4068 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4069 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4070 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4071 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4072 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4073 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4074 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4075 | break; |
| 4076 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4077 | // build CV12 T& |
| 4078 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4079 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4080 | T.isVolatileQualified()) |
| 4081 | continue; |
| 4082 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4083 | T.isRestrictQualified()) |
| 4084 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4085 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4086 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4087 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4088 | } |
| 4089 | } |
| 4090 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4091 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4092 | |
| 4093 | case OO_Conditional: |
| 4094 | // Note that we don't consider the first argument, since it has been |
| 4095 | // contextually converted to bool long ago. The candidates below are |
| 4096 | // therefore added as binary. |
| 4097 | // |
| 4098 | // C++ [over.built]p24: |
| 4099 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4100 | // there exist candidate operator functions of the form |
| 4101 | // |
| 4102 | // T operator?(bool, T, T); |
| 4103 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4104 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4105 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4106 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4107 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4108 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4109 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4110 | CandidateTypes.member_pointer_begin(), |
| 4111 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4112 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4113 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4114 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4115 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4116 | } |
| 4117 | } |
| 4118 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4119 | /// \brief Add function candidates found via argument-dependent lookup |
| 4120 | /// to the set of overloading candidates. |
| 4121 | /// |
| 4122 | /// This routine performs argument-dependent name lookup based on the |
| 4123 | /// given function name (which may also be an operator name) and adds |
| 4124 | /// all of the overload candidates found by ADL to the overload |
| 4125 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4127 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4128 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4129 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4130 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4131 | OverloadCandidateSet& CandidateSet, |
| 4132 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4133 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4134 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 4135 | // FIXME: This approach for uniquing ADL results (and removing |
| 4136 | // redundant candidates from the set) relies on pointer-equality, |
| 4137 | // which means we need to key off the canonical decl. However, |
| 4138 | // always going back to the canonical decl might not get us the |
| 4139 | // right set of default arguments. What default arguments are |
| 4140 | // we supposed to consider on ADL candidates, anyway? |
| 4141 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4142 | // FIXME: Pass in the explicit template arguments? |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4143 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4144 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4145 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4146 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4147 | CandEnd = CandidateSet.end(); |
| 4148 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4149 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4150 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4151 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4152 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4153 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4154 | |
| 4155 | // For each of the ADL candidates we found, add it to the overload |
| 4156 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4157 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4158 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4159 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4160 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4161 | continue; |
| 4162 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4163 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4164 | false, false, PartialOverloading); |
| 4165 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4166 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4167 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4168 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4169 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4172 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4173 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4175 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4176 | const OverloadCandidate& Cand2, |
| 4177 | SourceLocation Loc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4178 | // Define viable functions to be better candidates than non-viable |
| 4179 | // functions. |
| 4180 | if (!Cand2.Viable) |
| 4181 | return Cand1.Viable; |
| 4182 | else if (!Cand1.Viable) |
| 4183 | return false; |
| 4184 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4185 | // C++ [over.match.best]p1: |
| 4186 | // |
| 4187 | // -- if F is a static member function, ICS1(F) is defined such |
| 4188 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4189 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4190 | // better nor worse than ICS1(F). |
| 4191 | unsigned StartArg = 0; |
| 4192 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4193 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4194 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4195 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | // A viable function F1 is defined to be a better function than another |
| 4197 | // 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] | 4198 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4199 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4200 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4201 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4202 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4203 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4204 | Cand2.Conversions[ArgIdx])) { |
| 4205 | case ImplicitConversionSequence::Better: |
| 4206 | // Cand1 has a better conversion sequence. |
| 4207 | HasBetterConversion = true; |
| 4208 | break; |
| 4209 | |
| 4210 | case ImplicitConversionSequence::Worse: |
| 4211 | // Cand1 can't be better than Cand2. |
| 4212 | return false; |
| 4213 | |
| 4214 | case ImplicitConversionSequence::Indistinguishable: |
| 4215 | // Do nothing. |
| 4216 | break; |
| 4217 | } |
| 4218 | } |
| 4219 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4221 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4222 | if (HasBetterConversion) |
| 4223 | return true; |
| 4224 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4225 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4226 | // specialization, or, if not that, |
| 4227 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4228 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4229 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4230 | |
| 4231 | // -- F1 and F2 are function template specializations, and the function |
| 4232 | // template for F1 is more specialized than the template for F2 |
| 4233 | // 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] | 4234 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4235 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4236 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4237 | if (FunctionTemplateDecl *BetterTemplate |
| 4238 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4239 | Cand2.Function->getPrimaryTemplate(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4240 | Loc, |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4241 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4242 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4243 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4245 | // -- the context is an initialization by user-defined conversion |
| 4246 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4247 | // from the return type of F1 to the destination type (i.e., |
| 4248 | // the type of the entity being initialized) is a better |
| 4249 | // conversion sequence than the standard conversion sequence |
| 4250 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | if (Cand1.Function && Cand2.Function && |
| 4252 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4253 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4254 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4255 | Cand2.FinalConversion)) { |
| 4256 | case ImplicitConversionSequence::Better: |
| 4257 | // Cand1 has a better conversion sequence. |
| 4258 | return true; |
| 4259 | |
| 4260 | case ImplicitConversionSequence::Worse: |
| 4261 | // Cand1 can't be better than Cand2. |
| 4262 | return false; |
| 4263 | |
| 4264 | case ImplicitConversionSequence::Indistinguishable: |
| 4265 | // Do nothing |
| 4266 | break; |
| 4267 | } |
| 4268 | } |
| 4269 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4270 | return false; |
| 4271 | } |
| 4272 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4274 | /// within an overload candidate set. |
| 4275 | /// |
| 4276 | /// \param CandidateSet the set of candidate functions. |
| 4277 | /// |
| 4278 | /// \param Loc the location of the function name (or operator symbol) for |
| 4279 | /// which overload resolution occurs. |
| 4280 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4281 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4282 | /// function, Best points to the candidate function found. |
| 4283 | /// |
| 4284 | /// \returns The result of overload resolution. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4285 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4286 | SourceLocation Loc, |
| 4287 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4288 | // Find the best viable function. |
| 4289 | Best = CandidateSet.end(); |
| 4290 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4291 | Cand != CandidateSet.end(); ++Cand) { |
| 4292 | if (Cand->Viable) { |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4293 | if (Best == CandidateSet.end() || |
| 4294 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4295 | Best = Cand; |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | // If we didn't find any viable functions, abort. |
| 4300 | if (Best == CandidateSet.end()) |
| 4301 | return OR_No_Viable_Function; |
| 4302 | |
| 4303 | // Make sure that this function is better than every other viable |
| 4304 | // function. If not, we have an ambiguity. |
| 4305 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4306 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4307 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4308 | Cand != Best && |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4309 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4310 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4311 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4312 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4314 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4315 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4316 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4318 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4319 | return OR_Deleted; |
| 4320 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4321 | // C++ [basic.def.odr]p2: |
| 4322 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4323 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4324 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4325 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4326 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4327 | if (Best->Function) |
| 4328 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4329 | return OR_Success; |
| 4330 | } |
| 4331 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4332 | namespace { |
| 4333 | |
| 4334 | enum OverloadCandidateKind { |
| 4335 | oc_function, |
| 4336 | oc_method, |
| 4337 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4338 | oc_function_template, |
| 4339 | oc_method_template, |
| 4340 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4341 | oc_implicit_default_constructor, |
| 4342 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4343 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4344 | }; |
| 4345 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4346 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4347 | FunctionDecl *Fn, |
| 4348 | std::string &Description) { |
| 4349 | bool isTemplate = false; |
| 4350 | |
| 4351 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4352 | isTemplate = true; |
| 4353 | Description = S.getTemplateArgumentBindingsText( |
| 4354 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4355 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4356 | |
| 4357 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4358 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4359 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4360 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4361 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4362 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4366 | // This actually gets spelled 'candidate function' for now, but |
| 4367 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4368 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4369 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4370 | |
| 4371 | assert(Meth->isCopyAssignment() |
| 4372 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4373 | return oc_implicit_copy_assignment; |
| 4374 | } |
| 4375 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4376 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | } // end anonymous namespace |
| 4380 | |
| 4381 | // Notes the location of an overload candidate. |
| 4382 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4383 | std::string FnDesc; |
| 4384 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4385 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4386 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4389 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4390 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4391 | /// target types of the conversion. |
| 4392 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4393 | SourceLocation CaretLoc, |
| 4394 | const PartialDiagnostic &PDiag) { |
| 4395 | Diag(CaretLoc, PDiag) |
| 4396 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4397 | for (AmbiguousConversionSequence::const_iterator |
| 4398 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4399 | NoteOverloadCandidate(*I); |
| 4400 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4403 | namespace { |
| 4404 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4405 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4406 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4407 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4408 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4409 | FunctionDecl *Fn = Cand->Function; |
| 4410 | |
| 4411 | // There's a conversion slot for the object argument if this is a |
| 4412 | // non-constructor method. Note that 'I' corresponds the |
| 4413 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4414 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4415 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4416 | if (I == 0) |
| 4417 | isObjectArgument = true; |
| 4418 | else |
| 4419 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4422 | std::string FnDesc; |
| 4423 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4424 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4425 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4426 | QualType FromTy = Conv.Bad.getFromType(); |
| 4427 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4428 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4429 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4430 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4431 | Expr *E = FromExpr->IgnoreParens(); |
| 4432 | if (isa<UnaryOperator>(E)) |
| 4433 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4434 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4435 | |
| 4436 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 4437 | << (unsigned) FnKind << FnDesc |
| 4438 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4439 | << ToTy << Name << I+1; |
| 4440 | return; |
| 4441 | } |
| 4442 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4443 | // Do some hand-waving analysis to see if the non-viability is due |
| 4444 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4445 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4446 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4447 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4448 | CToTy = RT->getPointeeType(); |
| 4449 | else { |
| 4450 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4451 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4452 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4453 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4454 | } |
| 4455 | |
| 4456 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4457 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4458 | // It is dumb that we have to do this here. |
| 4459 | while (isa<ArrayType>(CFromTy)) |
| 4460 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4461 | while (isa<ArrayType>(CToTy)) |
| 4462 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4463 | |
| 4464 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4465 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4466 | |
| 4467 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4468 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4469 | << (unsigned) FnKind << FnDesc |
| 4470 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4471 | << FromTy |
| 4472 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4473 | << (unsigned) isObjectArgument << I+1; |
| 4474 | return; |
| 4475 | } |
| 4476 | |
| 4477 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4478 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4479 | |
| 4480 | if (isObjectArgument) { |
| 4481 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4482 | << (unsigned) FnKind << FnDesc |
| 4483 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4484 | << FromTy << (CVR - 1); |
| 4485 | } else { |
| 4486 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4487 | << (unsigned) FnKind << FnDesc |
| 4488 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4489 | << FromTy << (CVR - 1) << I+1; |
| 4490 | } |
| 4491 | return; |
| 4492 | } |
| 4493 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4494 | // Diagnose references or pointers to incomplete types differently, |
| 4495 | // since it's far from impossible that the incompleteness triggered |
| 4496 | // the failure. |
| 4497 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 4498 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 4499 | TempFromTy = PTy->getPointeeType(); |
| 4500 | if (TempFromTy->isIncompleteType()) { |
| 4501 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 4502 | << (unsigned) FnKind << FnDesc |
| 4503 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4504 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4505 | return; |
| 4506 | } |
| 4507 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4508 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4509 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4510 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4511 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 4512 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4516 | unsigned NumFormalArgs) { |
| 4517 | // TODO: treat calls to a missing default constructor as a special case |
| 4518 | |
| 4519 | FunctionDecl *Fn = Cand->Function; |
| 4520 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4521 | |
| 4522 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4523 | |
| 4524 | // at least / at most / exactly |
| 4525 | unsigned mode, modeCount; |
| 4526 | if (NumFormalArgs < MinParams) { |
| 4527 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4528 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4529 | mode = 0; // "at least" |
| 4530 | else |
| 4531 | mode = 2; // "exactly" |
| 4532 | modeCount = MinParams; |
| 4533 | } else { |
| 4534 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4535 | if (MinParams != FnTy->getNumArgs()) |
| 4536 | mode = 1; // "at most" |
| 4537 | else |
| 4538 | mode = 2; // "exactly" |
| 4539 | modeCount = FnTy->getNumArgs(); |
| 4540 | } |
| 4541 | |
| 4542 | std::string Description; |
| 4543 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4544 | |
| 4545 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4546 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4549 | /// Diagnose a failed template-argument deduction. |
| 4550 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 4551 | Expr **Args, unsigned NumArgs) { |
| 4552 | FunctionDecl *Fn = Cand->Function; // pattern |
| 4553 | |
| 4554 | TemplateParameter Param = TemplateParameter::getFromOpaqueValue( |
| 4555 | Cand->DeductionFailure.TemplateParameter); |
| 4556 | |
| 4557 | switch (Cand->DeductionFailure.Result) { |
| 4558 | case Sema::TDK_Success: |
| 4559 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 4560 | |
| 4561 | case Sema::TDK_Incomplete: { |
| 4562 | NamedDecl *ParamD; |
| 4563 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 4564 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 4565 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
| 4566 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 4567 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 4568 | << ParamD->getDeclName(); |
| 4569 | return; |
| 4570 | } |
| 4571 | |
| 4572 | // TODO: diagnose these individually, then kill off |
| 4573 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
| 4574 | case Sema::TDK_InstantiationDepth: |
| 4575 | case Sema::TDK_Inconsistent: |
| 4576 | case Sema::TDK_InconsistentQuals: |
| 4577 | case Sema::TDK_SubstitutionFailure: |
| 4578 | case Sema::TDK_NonDeducedMismatch: |
| 4579 | case Sema::TDK_TooManyArguments: |
| 4580 | case Sema::TDK_TooFewArguments: |
| 4581 | case Sema::TDK_InvalidExplicitArguments: |
| 4582 | case Sema::TDK_FailedOverloadResolution: |
| 4583 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 4584 | return; |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 4589 | /// already generated a primary error at the call site. |
| 4590 | /// |
| 4591 | /// It really does need to be a single diagnostic with its caret |
| 4592 | /// pointed at the candidate declaration. Yes, this creates some |
| 4593 | /// major challenges of technical writing. Yes, this makes pointing |
| 4594 | /// out problems with specific arguments quite awkward. It's still |
| 4595 | /// better than generating twenty screens of text for every failed |
| 4596 | /// overload. |
| 4597 | /// |
| 4598 | /// It would be great to be able to express per-candidate problems |
| 4599 | /// more richly for those diagnostic clients that cared, but we'd |
| 4600 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4601 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4602 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4603 | FunctionDecl *Fn = Cand->Function; |
| 4604 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4605 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4606 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4607 | std::string FnDesc; |
| 4608 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4609 | |
| 4610 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4611 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4612 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4615 | // We don't really have anything else to say about viable candidates. |
| 4616 | if (Cand->Viable) { |
| 4617 | S.NoteOverloadCandidate(Fn); |
| 4618 | return; |
| 4619 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4620 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4621 | switch (Cand->FailureKind) { |
| 4622 | case ovl_fail_too_many_arguments: |
| 4623 | case ovl_fail_too_few_arguments: |
| 4624 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4625 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4626 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4627 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 4628 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4629 | case ovl_fail_trivial_conversion: |
| 4630 | case ovl_fail_bad_final_conversion: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4631 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4632 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4633 | case ovl_fail_bad_conversion: { |
| 4634 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 4635 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4636 | if (Cand->Conversions[I].isBad()) |
| 4637 | return DiagnoseBadConversion(S, Cand, I); |
| 4638 | |
| 4639 | // FIXME: this currently happens when we're called from SemaInit |
| 4640 | // when user-conversion overload fails. Figure out how to handle |
| 4641 | // those conditions and diagnose them well. |
| 4642 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4643 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4644 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4648 | // Desugar the type of the surrogate down to a function type, |
| 4649 | // retaining as many typedefs as possible while still showing |
| 4650 | // the function type (and, therefore, its parameter types). |
| 4651 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4652 | bool isLValueReference = false; |
| 4653 | bool isRValueReference = false; |
| 4654 | bool isPointer = false; |
| 4655 | if (const LValueReferenceType *FnTypeRef = |
| 4656 | FnType->getAs<LValueReferenceType>()) { |
| 4657 | FnType = FnTypeRef->getPointeeType(); |
| 4658 | isLValueReference = true; |
| 4659 | } else if (const RValueReferenceType *FnTypeRef = |
| 4660 | FnType->getAs<RValueReferenceType>()) { |
| 4661 | FnType = FnTypeRef->getPointeeType(); |
| 4662 | isRValueReference = true; |
| 4663 | } |
| 4664 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4665 | FnType = FnTypePtr->getPointeeType(); |
| 4666 | isPointer = true; |
| 4667 | } |
| 4668 | // Desugar down to a function type. |
| 4669 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4670 | // Reconstruct the pointer/reference as appropriate. |
| 4671 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4672 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4673 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4674 | |
| 4675 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4676 | << FnType; |
| 4677 | } |
| 4678 | |
| 4679 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 4680 | const char *Opc, |
| 4681 | SourceLocation OpLoc, |
| 4682 | OverloadCandidate *Cand) { |
| 4683 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 4684 | std::string TypeStr("operator"); |
| 4685 | TypeStr += Opc; |
| 4686 | TypeStr += "("; |
| 4687 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4688 | if (Cand->Conversions.size() == 1) { |
| 4689 | TypeStr += ")"; |
| 4690 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 4691 | } else { |
| 4692 | TypeStr += ", "; |
| 4693 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4694 | TypeStr += ")"; |
| 4695 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 4700 | OverloadCandidate *Cand) { |
| 4701 | unsigned NoOperands = Cand->Conversions.size(); |
| 4702 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 4703 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4704 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 4705 | if (!ICS.isAmbiguous()) continue; |
| 4706 | |
| 4707 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4708 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4712 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 4713 | if (Cand->Function) |
| 4714 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 4715 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4716 | return Cand->Surrogate->getLocation(); |
| 4717 | return SourceLocation(); |
| 4718 | } |
| 4719 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4720 | struct CompareOverloadCandidatesForDisplay { |
| 4721 | Sema &S; |
| 4722 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4723 | |
| 4724 | bool operator()(const OverloadCandidate *L, |
| 4725 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 4726 | // Fast-path this check. |
| 4727 | if (L == R) return false; |
| 4728 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4729 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4730 | if (L->Viable) { |
| 4731 | if (!R->Viable) return true; |
| 4732 | |
| 4733 | // TODO: introduce a tri-valued comparison for overload |
| 4734 | // candidates. Would be more worthwhile if we had a sort |
| 4735 | // that could exploit it. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4736 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 4737 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4738 | } else if (R->Viable) |
| 4739 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4740 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4741 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4742 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4743 | // Criteria by which we can sort non-viable candidates: |
| 4744 | if (!L->Viable) { |
| 4745 | // 1. Arity mismatches come after other candidates. |
| 4746 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 4747 | L->FailureKind == ovl_fail_too_few_arguments) |
| 4748 | return false; |
| 4749 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 4750 | R->FailureKind == ovl_fail_too_few_arguments) |
| 4751 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4752 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4753 | // 2. Bad conversions come first and are ordered by the number |
| 4754 | // of bad conversions and quality of good conversions. |
| 4755 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 4756 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 4757 | return true; |
| 4758 | |
| 4759 | // If there's any ordering between the defined conversions... |
| 4760 | // FIXME: this might not be transitive. |
| 4761 | assert(L->Conversions.size() == R->Conversions.size()); |
| 4762 | |
| 4763 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 4764 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 4765 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4766 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 4767 | R->Conversions[I])) { |
| 4768 | case ImplicitConversionSequence::Better: |
| 4769 | leftBetter++; |
| 4770 | break; |
| 4771 | |
| 4772 | case ImplicitConversionSequence::Worse: |
| 4773 | leftBetter--; |
| 4774 | break; |
| 4775 | |
| 4776 | case ImplicitConversionSequence::Indistinguishable: |
| 4777 | break; |
| 4778 | } |
| 4779 | } |
| 4780 | if (leftBetter > 0) return true; |
| 4781 | if (leftBetter < 0) return false; |
| 4782 | |
| 4783 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 4784 | return false; |
| 4785 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4786 | // TODO: others? |
| 4787 | } |
| 4788 | |
| 4789 | // Sort everything else by location. |
| 4790 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 4791 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 4792 | |
| 4793 | // Put candidates without locations (e.g. builtins) at the end. |
| 4794 | if (LLoc.isInvalid()) return false; |
| 4795 | if (RLoc.isInvalid()) return true; |
| 4796 | |
| 4797 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4798 | } |
| 4799 | }; |
| 4800 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4801 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 4802 | /// computes up to the first |
| 4803 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 4804 | Expr **Args, unsigned NumArgs) { |
| 4805 | assert(!Cand->Viable); |
| 4806 | |
| 4807 | // Don't do anything on failures other than bad conversion. |
| 4808 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 4809 | |
| 4810 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4811 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4812 | unsigned ConvCount = Cand->Conversions.size(); |
| 4813 | while (true) { |
| 4814 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 4815 | ConvIdx++; |
| 4816 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 4817 | break; |
| 4818 | } |
| 4819 | |
| 4820 | if (ConvIdx == ConvCount) |
| 4821 | return; |
| 4822 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4823 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 4824 | "remaining conversion is initialized?"); |
| 4825 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4826 | // FIXME: these should probably be preserved from the overload |
| 4827 | // operation somehow. |
| 4828 | bool SuppressUserConversions = false; |
| 4829 | bool ForceRValue = false; |
| 4830 | |
| 4831 | const FunctionProtoType* Proto; |
| 4832 | unsigned ArgIdx = ConvIdx; |
| 4833 | |
| 4834 | if (Cand->IsSurrogate) { |
| 4835 | QualType ConvType |
| 4836 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 4837 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4838 | ConvType = ConvPtrType->getPointeeType(); |
| 4839 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 4840 | ArgIdx--; |
| 4841 | } else if (Cand->Function) { |
| 4842 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 4843 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 4844 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 4845 | ArgIdx--; |
| 4846 | } else { |
| 4847 | // Builtin binary operator with a bad first conversion. |
| 4848 | assert(ConvCount <= 3); |
| 4849 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 4850 | Cand->Conversions[ConvIdx] |
| 4851 | = S.TryCopyInitialization(Args[ConvIdx], |
| 4852 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 4853 | SuppressUserConversions, ForceRValue, |
| 4854 | /*InOverloadResolution*/ true); |
| 4855 | return; |
| 4856 | } |
| 4857 | |
| 4858 | // Fill in the rest of the conversions. |
| 4859 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4860 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 4861 | if (ArgIdx < NumArgsInProto) |
| 4862 | Cand->Conversions[ConvIdx] |
| 4863 | = S.TryCopyInitialization(Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 4864 | SuppressUserConversions, ForceRValue, |
| 4865 | /*InOverloadResolution=*/true); |
| 4866 | else |
| 4867 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 4868 | } |
| 4869 | } |
| 4870 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4871 | } // end anonymous namespace |
| 4872 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4873 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4874 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4875 | /// set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4876 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4877 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4878 | OverloadCandidateDisplayKind OCD, |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 4879 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4880 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4881 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4882 | // Sort the candidates by viability and position. Sorting directly would |
| 4883 | // be prohibitive, so we make a set of pointers and sort those. |
| 4884 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 4885 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 4886 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4887 | LastCand = CandidateSet.end(); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4888 | Cand != LastCand; ++Cand) { |
| 4889 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4890 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4891 | else if (OCD == OCD_AllCandidates) { |
| 4892 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 4893 | Cands.push_back(Cand); |
| 4894 | } |
| 4895 | } |
| 4896 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4897 | std::sort(Cands.begin(), Cands.end(), |
| 4898 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4899 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4900 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4901 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4902 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 4903 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 4904 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4905 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4906 | if (Cand->Function) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4907 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4908 | else if (Cand->IsSurrogate) |
| 4909 | NoteSurrogateCandidate(*this, Cand); |
| 4910 | |
| 4911 | // This a builtin candidate. We do not, in general, want to list |
| 4912 | // every possible builtin candidate. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4913 | else if (Cand->Viable) { |
| 4914 | // Generally we only see ambiguities including viable builtin |
| 4915 | // operators if overload resolution got screwed up by an |
| 4916 | // ambiguous user-defined conversion. |
| 4917 | // |
| 4918 | // FIXME: It's quite possible for different conversions to see |
| 4919 | // different ambiguities, though. |
| 4920 | if (!ReportedAmbiguousConversions) { |
| 4921 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 4922 | ReportedAmbiguousConversions = true; |
| 4923 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4924 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4925 | // If this is a viable builtin, print it. |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4926 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4927 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4928 | } |
| 4929 | } |
| 4930 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4931 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4932 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4933 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4934 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4935 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4936 | } |
| 4937 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4938 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4939 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4940 | /// expression with overloaded function type and @p ToType is the type |
| 4941 | /// we're trying to resolve to. For example: |
| 4942 | /// |
| 4943 | /// @code |
| 4944 | /// int f(double); |
| 4945 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4946 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4947 | /// int (*pfd)(double) = f; // selects f(double) |
| 4948 | /// @endcode |
| 4949 | /// |
| 4950 | /// This routine returns the resulting FunctionDecl if it could be |
| 4951 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4952 | /// routine will emit diagnostics if there is an error. |
| 4953 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4954 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4955 | bool Complain, |
| 4956 | DeclAccessPair &FoundResult) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4957 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4958 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4959 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4960 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4961 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4962 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4963 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4964 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4965 | FunctionType = MemTypePtr->getPointeeType(); |
| 4966 | IsMember = true; |
| 4967 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4968 | |
| 4969 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 4970 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4971 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4972 | return 0; |
| 4973 | |
| 4974 | // Find the actual overloaded function declaration. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4975 | if (From->getType() != Context.OverloadTy) |
| 4976 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4978 | // C++ [over.over]p1: |
| 4979 | // [...] [Note: any redundant set of parentheses surrounding the |
| 4980 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4981 | // C++ [over.over]p1: |
| 4982 | // [...] The overloaded function name can be preceded by the & |
| 4983 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4984 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 4985 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 4986 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 4987 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 4988 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4991 | // Look through all of the overloaded functions, searching for one |
| 4992 | // whose type matches exactly. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4993 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4994 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 4995 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4996 | bool FoundNonTemplateFunction = false; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4997 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 4998 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 4999 | // Look through any using declarations to find the underlying function. |
| 5000 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5001 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5002 | // C++ [over.over]p3: |
| 5003 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 5004 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5005 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5006 | // type "pointer-to-member-function." |
| 5007 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5008 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5009 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5010 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5012 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5013 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5014 | // static when converting to member pointer. |
| 5015 | if (Method->isStatic() == IsMember) |
| 5016 | continue; |
| 5017 | } else if (IsMember) |
| 5018 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5019 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5020 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | // If the name is a function template, template argument deduction is |
| 5022 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5023 | // resulting template argument list is used to generate a single |
| 5024 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5025 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5026 | // 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] | 5027 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5028 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5029 | if (TemplateDeductionResult Result |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5030 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5031 | FunctionType, Specialization, Info)) { |
| 5032 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5033 | (void)Result; |
| 5034 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5035 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5036 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5037 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5038 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5039 | Matches.push_back(std::make_pair(I.getPair(), |
| 5040 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5041 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5042 | |
| 5043 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5044 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5045 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5046 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5047 | // Skip non-static functions when converting to pointer, and static |
| 5048 | // when converting to member pointer. |
| 5049 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5050 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5051 | |
| 5052 | // If we have explicit template arguments, skip non-templates. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5053 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5054 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5055 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5056 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5057 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5058 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 5059 | QualType ResultTy; |
| 5060 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5061 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5062 | ResultTy)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5063 | Matches.push_back(std::make_pair(I.getPair(), |
| 5064 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5065 | FoundNonTemplateFunction = true; |
| 5066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5067 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5070 | // If there were 0 or 1 matches, we're done. |
| 5071 | if (Matches.empty()) |
| 5072 | return 0; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5073 | else if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5074 | FunctionDecl *Result = Matches[0].second; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5075 | FoundResult = Matches[0].first; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5076 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5077 | if (Complain) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5078 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5079 | return Result; |
| 5080 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5081 | |
| 5082 | // C++ [over.over]p4: |
| 5083 | // If more than one function is selected, [...] |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5084 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5085 | // [...] and any given function template specialization F1 is |
| 5086 | // eliminated if the set contains a second function template |
| 5087 | // specialization whose function template is more specialized |
| 5088 | // than the function template of F1 according to the partial |
| 5089 | // ordering rules of 14.5.5.2. |
| 5090 | |
| 5091 | // The algorithm specified above is quadratic. We instead use a |
| 5092 | // two-pass algorithm (similar to the one used to identify the |
| 5093 | // best viable function in an overload set) that identifies the |
| 5094 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5095 | |
| 5096 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 5097 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5098 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5099 | |
| 5100 | UnresolvedSetIterator Result = |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5101 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5102 | TPOC_Other, From->getLocStart(), |
| 5103 | PDiag(), |
| 5104 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5105 | << Matches[0].second->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5106 | PDiag(diag::note_ovl_candidate) |
| 5107 | << (unsigned) oc_function_template); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5108 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5109 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5110 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
| 5111 | if (Complain) |
| 5112 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5113 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5115 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5116 | // [...] any function template specializations in the set are |
| 5117 | // eliminated if the set also contains a non-template function, [...] |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5118 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5119 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5120 | ++I; |
| 5121 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5122 | Matches[I] = Matches[--N]; |
| 5123 | Matches.set_size(N); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5124 | } |
| 5125 | } |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5126 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5127 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5128 | // selected function. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5129 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5130 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5131 | FoundResult = Matches[0].first; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5132 | if (Complain) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5133 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
| 5134 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5136 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5137 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5138 | // returns (even if we issue the diagnostics here). |
| 5139 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5140 | << Matches[0].second->getDeclName(); |
| 5141 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5142 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
| 5145 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5146 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5147 | /// resolve that overloaded function expression down to a single function. |
| 5148 | /// |
| 5149 | /// This routine can only resolve template-ids that refer to a single function |
| 5150 | /// template, where that template-id refers to a single template whose template |
| 5151 | /// arguments are either provided by the template-id or have defaults, |
| 5152 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5153 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5154 | // C++ [over.over]p1: |
| 5155 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5156 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5157 | // C++ [over.over]p1: |
| 5158 | // [...] The overloaded function name can be preceded by the & |
| 5159 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5160 | |
| 5161 | if (From->getType() != Context.OverloadTy) |
| 5162 | return 0; |
| 5163 | |
| 5164 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5165 | |
| 5166 | // If we didn't actually find any template-ids, we're done. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5167 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5168 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5169 | |
| 5170 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5171 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5172 | |
| 5173 | // Look through all of the overloaded functions, searching for one |
| 5174 | // whose type matches exactly. |
| 5175 | FunctionDecl *Matched = 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5176 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5177 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5178 | // C++0x [temp.arg.explicit]p3: |
| 5179 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5180 | // where deduction is not done, if a template argument list is |
| 5181 | // specified and it, along with any default template arguments, |
| 5182 | // identifies a single function template specialization, then the |
| 5183 | // template-id is an lvalue for the function template specialization. |
| 5184 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5185 | |
| 5186 | // C++ [over.over]p2: |
| 5187 | // If the name is a function template, template argument deduction is |
| 5188 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5189 | // resulting template argument list is used to generate a single |
| 5190 | // function template specialization, which is added to the set of |
| 5191 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5192 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5193 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5194 | if (TemplateDeductionResult Result |
| 5195 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5196 | Specialization, Info)) { |
| 5197 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5198 | (void)Result; |
| 5199 | continue; |
| 5200 | } |
| 5201 | |
| 5202 | // Multiple matches; we can't resolve to a single declaration. |
| 5203 | if (Matched) |
| 5204 | return 0; |
| 5205 | |
| 5206 | Matched = Specialization; |
| 5207 | } |
| 5208 | |
| 5209 | return Matched; |
| 5210 | } |
| 5211 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5212 | /// \brief Add a single candidate to the overload set. |
| 5213 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5214 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5215 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5216 | Expr **Args, unsigned NumArgs, |
| 5217 | OverloadCandidateSet &CandidateSet, |
| 5218 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5219 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5220 | if (isa<UsingShadowDecl>(Callee)) |
| 5221 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5222 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5223 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5224 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5225 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5226 | false, false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5227 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | if (FunctionTemplateDecl *FuncTemplate |
| 5231 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5232 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 5233 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5234 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5235 | return; |
| 5236 | } |
| 5237 | |
| 5238 | assert(false && "unhandled case in overloaded call candidate"); |
| 5239 | |
| 5240 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5244 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5245 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5246 | Expr **Args, unsigned NumArgs, |
| 5247 | OverloadCandidateSet &CandidateSet, |
| 5248 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5249 | |
| 5250 | #ifndef NDEBUG |
| 5251 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5252 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5253 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5254 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5255 | // and let Y be the lookup set produced by argument dependent |
| 5256 | // lookup (defined as follows). If X contains |
| 5257 | // |
| 5258 | // -- a declaration of a class member, or |
| 5259 | // |
| 5260 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5261 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5262 | // |
| 5263 | // -- a declaration that is neither a function or a function |
| 5264 | // template |
| 5265 | // |
| 5266 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5267 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5268 | if (ULE->requiresADL()) { |
| 5269 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5270 | E = ULE->decls_end(); I != E; ++I) { |
| 5271 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5272 | assert(isa<UsingShadowDecl>(*I) || |
| 5273 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5274 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5275 | } |
| 5276 | } |
| 5277 | #endif |
| 5278 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5279 | // It would be nice to avoid this copy. |
| 5280 | TemplateArgumentListInfo TABuffer; |
| 5281 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5282 | if (ULE->hasExplicitTemplateArgs()) { |
| 5283 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5284 | ExplicitTemplateArgs = &TABuffer; |
| 5285 | } |
| 5286 | |
| 5287 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5288 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5289 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5290 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5291 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5292 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5293 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5294 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 5295 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5296 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5297 | CandidateSet, |
| 5298 | PartialOverloading); |
| 5299 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5300 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5301 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5302 | Expr **Args, unsigned NumArgs) { |
| 5303 | Fn->Destroy(SemaRef.Context); |
| 5304 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5305 | Args[Arg]->Destroy(SemaRef.Context); |
| 5306 | return SemaRef.ExprError(); |
| 5307 | } |
| 5308 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5309 | /// Attempts to recover from a call where no functions were found. |
| 5310 | /// |
| 5311 | /// Returns true if new candidates were found. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5312 | static Sema::OwningExprResult |
| 5313 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 5314 | UnresolvedLookupExpr *ULE, |
| 5315 | SourceLocation LParenLoc, |
| 5316 | Expr **Args, unsigned NumArgs, |
| 5317 | SourceLocation *CommaLocs, |
| 5318 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5319 | |
| 5320 | CXXScopeSpec SS; |
| 5321 | if (ULE->getQualifier()) { |
| 5322 | SS.setScopeRep(ULE->getQualifier()); |
| 5323 | SS.setRange(ULE->getQualifierRange()); |
| 5324 | } |
| 5325 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5326 | TemplateArgumentListInfo TABuffer; |
| 5327 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5328 | if (ULE->hasExplicitTemplateArgs()) { |
| 5329 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5330 | ExplicitTemplateArgs = &TABuffer; |
| 5331 | } |
| 5332 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5333 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5334 | Sema::LookupOrdinaryName); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 5335 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5336 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5337 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5338 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5339 | |
| 5340 | // Build an implicit member call if appropriate. Just drop the |
| 5341 | // casts and such from the call, we don't really care. |
| 5342 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5343 | if ((*R.begin())->isCXXClassMember()) |
| 5344 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5345 | else if (ExplicitTemplateArgs) |
| 5346 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5347 | else |
| 5348 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5349 | |
| 5350 | if (NewFn.isInvalid()) |
| 5351 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5352 | |
| 5353 | Fn->Destroy(SemaRef.Context); |
| 5354 | |
| 5355 | // This shouldn't cause an infinite loop because we're giving it |
| 5356 | // an expression with non-empty lookup results, which should never |
| 5357 | // end up here. |
| 5358 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5359 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5360 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5361 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5362 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5363 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5364 | /// (which eventually refers to the declaration Func) and the call |
| 5365 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5366 | /// to a specific function. If overload resolution succeeds, returns |
| 5367 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 5368 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5369 | /// arguments and Fn, and returns NULL. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5370 | Sema::OwningExprResult |
| 5371 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 5372 | SourceLocation LParenLoc, |
| 5373 | Expr **Args, unsigned NumArgs, |
| 5374 | SourceLocation *CommaLocs, |
| 5375 | SourceLocation RParenLoc) { |
| 5376 | #ifndef NDEBUG |
| 5377 | if (ULE->requiresADL()) { |
| 5378 | // To do ADL, we must have found an unqualified name. |
| 5379 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5380 | |
| 5381 | // We don't perform ADL for implicit declarations of builtins. |
| 5382 | // Verify that this was correctly set up. |
| 5383 | FunctionDecl *F; |
| 5384 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5385 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5386 | F->getBuiltinID() && F->isImplicit()) |
| 5387 | assert(0 && "performing ADL for builtin"); |
| 5388 | |
| 5389 | // We don't perform ADL in C. |
| 5390 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5391 | } |
| 5392 | #endif |
| 5393 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5394 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5395 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5396 | // Add the functions denoted by the callee to the set of candidate |
| 5397 | // functions, including those from argument-dependent lookup. |
| 5398 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5399 | |
| 5400 | // If we found nothing, try to recover. |
| 5401 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5402 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5403 | if (CandidateSet.empty()) |
| 5404 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5405 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5406 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5407 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5408 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5409 | case OR_Success: { |
| 5410 | FunctionDecl *FDecl = Best->Function; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5411 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5412 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5413 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5414 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5415 | |
| 5416 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5417 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5418 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5419 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5420 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5421 | break; |
| 5422 | |
| 5423 | case OR_Ambiguous: |
| 5424 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5425 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5426 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5427 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5428 | |
| 5429 | case OR_Deleted: |
| 5430 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5431 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5432 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5433 | << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5434 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5435 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5439 | // return NULL. |
| 5440 | Fn->Destroy(Context); |
| 5441 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5442 | Args[Arg]->Destroy(Context); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5443 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5444 | } |
| 5445 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5446 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5447 | return Functions.size() > 1 || |
| 5448 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5449 | } |
| 5450 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5451 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5452 | /// operator. |
| 5453 | /// |
| 5454 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5455 | /// |
| 5456 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5457 | /// operator. |
| 5458 | /// |
| 5459 | /// \param Functions The set of non-member functions that will be |
| 5460 | /// considered by overload resolution. The caller needs to build this |
| 5461 | /// set based on the context using, e.g., |
| 5462 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5463 | /// set should not contain any member functions; those will be added |
| 5464 | /// by CreateOverloadedUnaryOp(). |
| 5465 | /// |
| 5466 | /// \param input The input argument. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5467 | Sema::OwningExprResult |
| 5468 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 5469 | const UnresolvedSetImpl &Fns, |
| 5470 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5471 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5472 | Expr *Input = (Expr *)input.get(); |
| 5473 | |
| 5474 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5475 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5476 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5477 | |
| 5478 | Expr *Args[2] = { Input, 0 }; |
| 5479 | unsigned NumArgs = 1; |
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 | // For post-increment and post-decrement, add the implicit '0' as |
| 5482 | // the second argument, so that we know this is a post-increment or |
| 5483 | // post-decrement. |
| 5484 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5485 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5486 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5487 | SourceLocation()); |
| 5488 | NumArgs = 2; |
| 5489 | } |
| 5490 | |
| 5491 | if (Input->isTypeDependent()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5492 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5493 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5494 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5495 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5496 | /*ADL*/ true, IsOverloaded(Fns)); |
| 5497 | Fn->addDecls(Fns.begin(), Fns.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5498 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5499 | input.release(); |
| 5500 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5501 | &Args[0], NumArgs, |
| 5502 | Context.DependentTy, |
| 5503 | OpLoc)); |
| 5504 | } |
| 5505 | |
| 5506 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5507 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5508 | |
| 5509 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5510 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5511 | |
| 5512 | // Add operator candidates that are member functions. |
| 5513 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5514 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5515 | // Add candidates from ADL. |
| 5516 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 5517 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5518 | /*ExplicitTemplateArgs*/ 0, |
| 5519 | CandidateSet); |
| 5520 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5521 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5522 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5523 | |
| 5524 | // Perform overload resolution. |
| 5525 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5526 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5527 | case OR_Success: { |
| 5528 | // We found a built-in operator or an overloaded operator. |
| 5529 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5530 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5531 | if (FnDecl) { |
| 5532 | // We matched an overloaded operator. Build a call to that |
| 5533 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5535 | // Convert the arguments. |
| 5536 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5537 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5538 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5539 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 5540 | Best->FoundDecl, Method)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5541 | return ExprError(); |
| 5542 | } else { |
| 5543 | // Convert the arguments. |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5544 | OwningExprResult InputInit |
| 5545 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5546 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5547 | SourceLocation(), |
| 5548 | move(input)); |
| 5549 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5550 | return ExprError(); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5551 | |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5552 | input = move(InputInit); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5553 | Input = (Expr *)input.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | // Determine the result type |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5557 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5558 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5559 | // Build the actual expression node. |
| 5560 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5561 | SourceLocation()); |
| 5562 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5563 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5564 | input.release(); |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5565 | Args[0] = Input; |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5566 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5567 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5568 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5569 | |
| 5570 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5571 | FnDecl)) |
| 5572 | return ExprError(); |
| 5573 | |
| 5574 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5575 | } else { |
| 5576 | // We matched a built-in operator. Convert the arguments, then |
| 5577 | // break out so that we will build the appropriate built-in |
| 5578 | // operator node. |
| 5579 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5580 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5581 | return ExprError(); |
| 5582 | |
| 5583 | break; |
| 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | case OR_No_Viable_Function: |
| 5588 | // No viable function; fall through to handling this as a |
| 5589 | // built-in operator, which will produce an error message for us. |
| 5590 | break; |
| 5591 | |
| 5592 | case OR_Ambiguous: |
| 5593 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5594 | << UnaryOperator::getOpcodeStr(Opc) |
| 5595 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5596 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5597 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5598 | return ExprError(); |
| 5599 | |
| 5600 | case OR_Deleted: |
| 5601 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5602 | << Best->Function->isDeleted() |
| 5603 | << UnaryOperator::getOpcodeStr(Opc) |
| 5604 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5605 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5606 | return ExprError(); |
| 5607 | } |
| 5608 | |
| 5609 | // Either we found no viable overloaded operator or we matched a |
| 5610 | // built-in operator. In either case, fall through to trying to |
| 5611 | // build a built-in operation. |
| 5612 | input.release(); |
| 5613 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5614 | } |
| 5615 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5616 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5617 | /// operator. |
| 5618 | /// |
| 5619 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5620 | /// |
| 5621 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5622 | /// operator. |
| 5623 | /// |
| 5624 | /// \param Functions The set of non-member functions that will be |
| 5625 | /// considered by overload resolution. The caller needs to build this |
| 5626 | /// set based on the context using, e.g., |
| 5627 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5628 | /// set should not contain any member functions; those will be added |
| 5629 | /// by CreateOverloadedBinOp(). |
| 5630 | /// |
| 5631 | /// \param LHS Left-hand argument. |
| 5632 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5633 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5634 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5636 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5637 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5638 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5639 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5640 | |
| 5641 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5642 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5643 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5644 | |
| 5645 | // If either side is type-dependent, create an appropriate dependent |
| 5646 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5647 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5648 | if (Fns.empty()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5649 | // If there are no functions to store, just build a dependent |
| 5650 | // BinaryOperator or CompoundAssignment. |
| 5651 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5652 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5653 | Context.DependentTy, OpLoc)); |
| 5654 | |
| 5655 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5656 | Context.DependentTy, |
| 5657 | Context.DependentTy, |
| 5658 | Context.DependentTy, |
| 5659 | OpLoc)); |
| 5660 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5661 | |
| 5662 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5663 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5664 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5665 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5666 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5667 | /*ADL*/ true, IsOverloaded(Fns)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5668 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5669 | Fn->addDecls(Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5670 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5671 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5672 | Context.DependentTy, |
| 5673 | OpLoc)); |
| 5674 | } |
| 5675 | |
| 5676 | // If this is the .* operator, which is not overloadable, just |
| 5677 | // create a built-in binary operator. |
| 5678 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5679 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5680 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 5681 | // If this is the assignment operator, we only perform overload resolution |
| 5682 | // if the left-hand side is a class or enumeration type. This is actually |
| 5683 | // a hack. The standard requires that we do overload resolution between the |
| 5684 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5685 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5686 | // Note that we go the traditional code path for compound assignment forms. |
| 5687 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5688 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5689 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5690 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5691 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5692 | |
| 5693 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5694 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5695 | |
| 5696 | // Add operator candidates that are member functions. |
| 5697 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5698 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5699 | // Add candidates from ADL. |
| 5700 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 5701 | Args, 2, |
| 5702 | /*ExplicitTemplateArgs*/ 0, |
| 5703 | CandidateSet); |
| 5704 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5705 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5706 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5707 | |
| 5708 | // Perform overload resolution. |
| 5709 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5710 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5711 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5712 | // We found a built-in operator or an overloaded operator. |
| 5713 | FunctionDecl *FnDecl = Best->Function; |
| 5714 | |
| 5715 | if (FnDecl) { |
| 5716 | // We matched an overloaded operator. Build a call to that |
| 5717 | // operator. |
| 5718 | |
| 5719 | // Convert the arguments. |
| 5720 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5721 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5722 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5723 | |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5724 | OwningExprResult Arg1 |
| 5725 | = PerformCopyInitialization( |
| 5726 | InitializedEntity::InitializeParameter( |
| 5727 | FnDecl->getParamDecl(0)), |
| 5728 | SourceLocation(), |
| 5729 | Owned(Args[1])); |
| 5730 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5731 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 5733 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5734 | Best->FoundDecl, Method)) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5735 | return ExprError(); |
| 5736 | |
| 5737 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5738 | } else { |
| 5739 | // Convert the arguments. |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5740 | OwningExprResult Arg0 |
| 5741 | = PerformCopyInitialization( |
| 5742 | InitializedEntity::InitializeParameter( |
| 5743 | FnDecl->getParamDecl(0)), |
| 5744 | SourceLocation(), |
| 5745 | Owned(Args[0])); |
| 5746 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5747 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5748 | |
| 5749 | OwningExprResult Arg1 |
| 5750 | = PerformCopyInitialization( |
| 5751 | InitializedEntity::InitializeParameter( |
| 5752 | FnDecl->getParamDecl(1)), |
| 5753 | SourceLocation(), |
| 5754 | Owned(Args[1])); |
| 5755 | if (Arg1.isInvalid()) |
| 5756 | return ExprError(); |
| 5757 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5758 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | // Determine the result type |
| 5762 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5763 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5764 | ResultTy = ResultTy.getNonReferenceType(); |
| 5765 | |
| 5766 | // Build the actual expression node. |
| 5767 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 5768 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5769 | UsualUnaryConversions(FnExpr); |
| 5770 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5771 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5772 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5773 | Args, 2, ResultTy, |
| 5774 | OpLoc)); |
| 5775 | |
| 5776 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5777 | FnDecl)) |
| 5778 | return ExprError(); |
| 5779 | |
| 5780 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5781 | } else { |
| 5782 | // We matched a built-in operator. Convert the arguments, then |
| 5783 | // break out so that we will build the appropriate built-in |
| 5784 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5785 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5786 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5787 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5788 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5789 | return ExprError(); |
| 5790 | |
| 5791 | break; |
| 5792 | } |
| 5793 | } |
| 5794 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5795 | case OR_No_Viable_Function: { |
| 5796 | // C++ [over.match.oper]p9: |
| 5797 | // If the operator is the operator , [...] and there are no |
| 5798 | // viable functions, then the operator is assumed to be the |
| 5799 | // built-in operator and interpreted according to clause 5. |
| 5800 | if (Opc == BinaryOperator::Comma) |
| 5801 | break; |
| 5802 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5803 | // For class as left operand for assignment or compound assigment operator |
| 5804 | // do not fall through to handling in built-in, but report that no overloaded |
| 5805 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5806 | OwningExprResult Result = ExprError(); |
| 5807 | if (Args[0]->getType()->isRecordType() && |
| 5808 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5809 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5810 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5811 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5812 | } else { |
| 5813 | // No viable function; try to create a built-in operation, which will |
| 5814 | // produce an error. Then, show the non-viable candidates. |
| 5815 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5816 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5817 | assert(Result.isInvalid() && |
| 5818 | "C++ binary operator overloading is missing candidates!"); |
| 5819 | if (Result.isInvalid()) |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5820 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5821 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5822 | return move(Result); |
| 5823 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5824 | |
| 5825 | case OR_Ambiguous: |
| 5826 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5827 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5828 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5829 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5830 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5831 | return ExprError(); |
| 5832 | |
| 5833 | case OR_Deleted: |
| 5834 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5835 | << Best->Function->isDeleted() |
| 5836 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5837 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5838 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5839 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5840 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5841 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5842 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5843 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5846 | Action::OwningExprResult |
| 5847 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5848 | SourceLocation RLoc, |
| 5849 | ExprArg Base, ExprArg Idx) { |
| 5850 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5851 | static_cast<Expr*>(Idx.get()) }; |
| 5852 | DeclarationName OpName = |
| 5853 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5854 | |
| 5855 | // If either side is type-dependent, create an appropriate dependent |
| 5856 | // expression. |
| 5857 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5858 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5859 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5860 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5861 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5862 | 0, SourceRange(), OpName, LLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5863 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5864 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5865 | |
| 5866 | Base.release(); |
| 5867 | Idx.release(); |
| 5868 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5869 | Args, 2, |
| 5870 | Context.DependentTy, |
| 5871 | RLoc)); |
| 5872 | } |
| 5873 | |
| 5874 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5875 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5876 | |
| 5877 | // Subscript can only be overloaded as a member function. |
| 5878 | |
| 5879 | // Add operator candidates that are member functions. |
| 5880 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5881 | |
| 5882 | // Add builtin operator candidates. |
| 5883 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5884 | |
| 5885 | // Perform overload resolution. |
| 5886 | OverloadCandidateSet::iterator Best; |
| 5887 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5888 | case OR_Success: { |
| 5889 | // We found a built-in operator or an overloaded operator. |
| 5890 | FunctionDecl *FnDecl = Best->Function; |
| 5891 | |
| 5892 | if (FnDecl) { |
| 5893 | // We matched an overloaded operator. Build a call to that |
| 5894 | // operator. |
| 5895 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5896 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5897 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5898 | // Convert the arguments. |
| 5899 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 5900 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5901 | Best->FoundDecl, Method)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5902 | return ExprError(); |
| 5903 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 5904 | // Convert the arguments. |
| 5905 | OwningExprResult InputInit |
| 5906 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 5907 | FnDecl->getParamDecl(0)), |
| 5908 | SourceLocation(), |
| 5909 | Owned(Args[1])); |
| 5910 | if (InputInit.isInvalid()) |
| 5911 | return ExprError(); |
| 5912 | |
| 5913 | Args[1] = InputInit.takeAs<Expr>(); |
| 5914 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5915 | // Determine the result type |
| 5916 | QualType ResultTy |
| 5917 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5918 | ResultTy = ResultTy.getNonReferenceType(); |
| 5919 | |
| 5920 | // Build the actual expression node. |
| 5921 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5922 | LLoc); |
| 5923 | UsualUnaryConversions(FnExpr); |
| 5924 | |
| 5925 | Base.release(); |
| 5926 | Idx.release(); |
| 5927 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5928 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5929 | FnExpr, Args, 2, |
| 5930 | ResultTy, RLoc)); |
| 5931 | |
| 5932 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5933 | FnDecl)) |
| 5934 | return ExprError(); |
| 5935 | |
| 5936 | return MaybeBindToTemporary(TheCall.release()); |
| 5937 | } else { |
| 5938 | // We matched a built-in operator. Convert the arguments, then |
| 5939 | // break out so that we will build the appropriate built-in |
| 5940 | // operator node. |
| 5941 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5942 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5943 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5944 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5945 | return ExprError(); |
| 5946 | |
| 5947 | break; |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5952 | if (CandidateSet.empty()) |
| 5953 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5954 | << Args[0]->getType() << /*subscript*/ 0 |
| 5955 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5956 | else |
| 5957 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5958 | << Args[0]->getType() |
| 5959 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5960 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5961 | "[]", LLoc); |
| 5962 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | case OR_Ambiguous: |
| 5966 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5967 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5968 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5969 | "[]", LLoc); |
| 5970 | return ExprError(); |
| 5971 | |
| 5972 | case OR_Deleted: |
| 5973 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5974 | << Best->Function->isDeleted() << "[]" |
| 5975 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5976 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5977 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5978 | return ExprError(); |
| 5979 | } |
| 5980 | |
| 5981 | // We matched a built-in operator; build it. |
| 5982 | Base.release(); |
| 5983 | Idx.release(); |
| 5984 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 5985 | Owned(Args[1]), RLoc); |
| 5986 | } |
| 5987 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5988 | /// BuildCallToMemberFunction - Build a call to a member |
| 5989 | /// function. MemExpr is the expression that refers to the member |
| 5990 | /// function (and includes the object parameter), Args/NumArgs are the |
| 5991 | /// arguments to the function call (not including the object |
| 5992 | /// parameter). The caller needs to validate that the member |
| 5993 | /// expression refers to a member function or an overloaded member |
| 5994 | /// function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5995 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 5997 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5998 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 5999 | SourceLocation RParenLoc) { |
| 6000 | // Dig out the member expression. This holds both the object |
| 6001 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6002 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6003 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6004 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6005 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 6006 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6007 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6008 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6009 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6010 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6011 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6012 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6013 | } else { |
| 6014 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6015 | Qualifier = UnresExpr->getQualifier(); |
| 6016 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6017 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6018 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6019 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6020 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6021 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6022 | // FIXME: avoid copy. |
| 6023 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6024 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6025 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6026 | TemplateArgs = &TemplateArgsBuffer; |
| 6027 | } |
| 6028 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6029 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6030 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6031 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6032 | NamedDecl *Func = *I; |
| 6033 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6034 | if (isa<UsingShadowDecl>(Func)) |
| 6035 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6036 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6037 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6038 | // If explicit template arguments were provided, we can't call a |
| 6039 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6040 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6041 | continue; |
| 6042 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6043 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6044 | Args, NumArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6045 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6046 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6047 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6048 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6049 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6050 | CandidateSet, |
| 6051 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6052 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6054 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6055 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6056 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6057 | OverloadCandidateSet::iterator Best; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6058 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6059 | case OR_Success: |
| 6060 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6061 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6062 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6063 | break; |
| 6064 | |
| 6065 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6066 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6067 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6068 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6069 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6070 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6071 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6072 | |
| 6073 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6074 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6075 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6076 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6077 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6078 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6079 | |
| 6080 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6081 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6082 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6083 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6084 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6085 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6086 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6089 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6090 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6091 | // If overload resolution picked a static member, build a |
| 6092 | // non-member call based on that function. |
| 6093 | if (Method->isStatic()) { |
| 6094 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6095 | Args, NumArgs, RParenLoc); |
| 6096 | } |
| 6097 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6098 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6102 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6103 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6104 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6105 | Method->getResultType().getNonReferenceType(), |
| 6106 | RParenLoc)); |
| 6107 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6108 | // Check for a valid return type. |
| 6109 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6110 | TheCall.get(), Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6111 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6112 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6113 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6114 | // We only need to do this if there was actually an overload; otherwise |
| 6115 | // it was done at lookup. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6116 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6117 | if (!Method->isStatic() && |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6118 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 6119 | FoundDecl, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6120 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6121 | MemExpr->setBase(ObjectArg); |
| 6122 | |
| 6123 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6124 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6125 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6126 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6127 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6128 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6129 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6130 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 6131 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6132 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6135 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6136 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6137 | /// overloaded function call operator (@c operator()) or performing a |
| 6138 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6139 | Sema::ExprResult |
| 6140 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 6141 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6142 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6144 | SourceLocation RParenLoc) { |
| 6145 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6146 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6147 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6148 | // C++ [over.call.object]p1: |
| 6149 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6150 | // 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] | 6151 | // candidate functions includes at least the function call |
| 6152 | // operators of T. The function call operators of T are obtained by |
| 6153 | // ordinary lookup of the name operator() in the context of |
| 6154 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6155 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6156 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6157 | |
| 6158 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6159 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6160 | << Object->getSourceRange())) |
| 6161 | return true; |
| 6162 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6163 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6164 | LookupQualifiedName(R, Record->getDecl()); |
| 6165 | R.suppressDiagnostics(); |
| 6166 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6167 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6168 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6169 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6170 | Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6171 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6172 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6173 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6174 | // C++ [over.call.object]p2: |
| 6175 | // In addition, for each conversion function declared in T of the |
| 6176 | // form |
| 6177 | // |
| 6178 | // operator conversion-type-id () cv-qualifier; |
| 6179 | // |
| 6180 | // where cv-qualifier is the same cv-qualification as, or a |
| 6181 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 6182 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6183 | // R", or the type "reference to pointer to function of |
| 6184 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6185 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6186 | // is also considered as a candidate function. Similarly, |
| 6187 | // surrogate call functions are added to the set of candidate |
| 6188 | // functions for each conversion function declared in an |
| 6189 | // accessible base class provided the function is not hidden |
| 6190 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6191 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 6192 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6193 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6194 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6195 | NamedDecl *D = *I; |
| 6196 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6197 | if (isa<UsingShadowDecl>(D)) |
| 6198 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6199 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6200 | // Skip over templated conversion functions; they aren't |
| 6201 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6202 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6203 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6204 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6205 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6206 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6207 | // Strip the reference type (if any) and then the pointer type (if |
| 6208 | // any) to get down to what might be a function type. |
| 6209 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6210 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6211 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6213 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6214 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6215 | Object->getType(), Args, NumArgs, |
| 6216 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6218 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6219 | // Perform overload resolution. |
| 6220 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6221 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6222 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6223 | // Overload resolution succeeded; we'll build the appropriate call |
| 6224 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6225 | break; |
| 6226 | |
| 6227 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6228 | if (CandidateSet.empty()) |
| 6229 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6230 | << Object->getType() << /*call*/ 1 |
| 6231 | << Object->getSourceRange(); |
| 6232 | else |
| 6233 | Diag(Object->getSourceRange().getBegin(), |
| 6234 | diag::err_ovl_no_viable_object_call) |
| 6235 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6236 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6237 | break; |
| 6238 | |
| 6239 | case OR_Ambiguous: |
| 6240 | Diag(Object->getSourceRange().getBegin(), |
| 6241 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6242 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6243 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6244 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6245 | |
| 6246 | case OR_Deleted: |
| 6247 | Diag(Object->getSourceRange().getBegin(), |
| 6248 | diag::err_ovl_deleted_object_call) |
| 6249 | << Best->Function->isDeleted() |
| 6250 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6251 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6252 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6253 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6254 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6255 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6256 | // We had an error; delete all of the subexpressions and return |
| 6257 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6258 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6259 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6260 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6261 | return true; |
| 6262 | } |
| 6263 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6264 | if (Best->Function == 0) { |
| 6265 | // Since there is no function declaration, this is one of the |
| 6266 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6267 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6268 | = cast<CXXConversionDecl>( |
| 6269 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6270 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6271 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6272 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6273 | // We selected one of the surrogate functions that converts the |
| 6274 | // object parameter to a function pointer. Perform the conversion |
| 6275 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6276 | |
| 6277 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6278 | // and then call it. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6279 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 6280 | Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6281 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6282 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6283 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 6284 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6287 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6288 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6289 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6290 | // that calls this method, using Object for the implicit object |
| 6291 | // parameter and passing along the remaining arguments. |
| 6292 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6293 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6294 | |
| 6295 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6296 | unsigned NumArgsToCheck = NumArgs; |
| 6297 | |
| 6298 | // Build the full argument list for the method call (the |
| 6299 | // implicit object parameter is placed at the beginning of the |
| 6300 | // list). |
| 6301 | Expr **MethodArgs; |
| 6302 | if (NumArgs < NumArgsInProto) { |
| 6303 | NumArgsToCheck = NumArgsInProto; |
| 6304 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6305 | } else { |
| 6306 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6307 | } |
| 6308 | MethodArgs[0] = Object; |
| 6309 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6310 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6311 | |
| 6312 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6313 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6314 | UsualUnaryConversions(NewFn); |
| 6315 | |
| 6316 | // Once we've built TheCall, all of the expressions are properly |
| 6317 | // owned. |
| 6318 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6319 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6320 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6321 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6322 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6323 | delete [] MethodArgs; |
| 6324 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6325 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6326 | Method)) |
| 6327 | return true; |
| 6328 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6329 | // We may have default arguments. If so, we need to allocate more |
| 6330 | // slots in the call for them. |
| 6331 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6332 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6333 | else if (NumArgs > NumArgsInProto) |
| 6334 | NumArgsToCheck = NumArgsInProto; |
| 6335 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6336 | bool IsError = false; |
| 6337 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6338 | // Initialize the implicit object parameter. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6339 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6340 | Best->FoundDecl, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6341 | TheCall->setArg(0, Object); |
| 6342 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6343 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6344 | // Check the argument types. |
| 6345 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6346 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6347 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6348 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6349 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6350 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 6351 | |
| 6352 | OwningExprResult InputInit |
| 6353 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6354 | Method->getParamDecl(i)), |
| 6355 | SourceLocation(), Owned(Arg)); |
| 6356 | |
| 6357 | IsError |= InputInit.isInvalid(); |
| 6358 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6359 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 6360 | OwningExprResult DefArg |
| 6361 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6362 | if (DefArg.isInvalid()) { |
| 6363 | IsError = true; |
| 6364 | break; |
| 6365 | } |
| 6366 | |
| 6367 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6368 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6369 | |
| 6370 | TheCall->setArg(i + 1, Arg); |
| 6371 | } |
| 6372 | |
| 6373 | // If this is a variadic call, handle args passed through "...". |
| 6374 | if (Proto->isVariadic()) { |
| 6375 | // Promote the arguments (C99 6.5.2.2p7). |
| 6376 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6377 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6378 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6379 | TheCall->setArg(i + 1, Arg); |
| 6380 | } |
| 6381 | } |
| 6382 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6383 | if (IsError) return true; |
| 6384 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6385 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6386 | return true; |
| 6387 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 6388 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6389 | } |
| 6390 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6391 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6392 | /// (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] | 6393 | /// @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] | 6394 | Sema::OwningExprResult |
| 6395 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6396 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6397 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6398 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6399 | SourceLocation Loc = Base->getExprLoc(); |
| 6400 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6401 | // C++ [over.ref]p1: |
| 6402 | // |
| 6403 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6404 | // for a class object x of type T if T::operator->() exists and if |
| 6405 | // the operator is selected as the best match function by the |
| 6406 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6407 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6408 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6409 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6410 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6411 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 6412 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6413 | << Base->getSourceRange())) |
| 6414 | return ExprError(); |
| 6415 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6416 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6417 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6418 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6419 | |
| 6420 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6421 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6422 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6423 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6424 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6425 | |
| 6426 | // Perform overload resolution. |
| 6427 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6428 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6429 | case OR_Success: |
| 6430 | // Overload resolution succeeded; we'll build the call below. |
| 6431 | break; |
| 6432 | |
| 6433 | case OR_No_Viable_Function: |
| 6434 | if (CandidateSet.empty()) |
| 6435 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6436 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6437 | else |
| 6438 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6439 | << "operator->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6440 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6441 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6442 | |
| 6443 | case OR_Ambiguous: |
| 6444 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6445 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6446 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6447 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6448 | |
| 6449 | case OR_Deleted: |
| 6450 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6451 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6452 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6453 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6454 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6455 | } |
| 6456 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6457 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 6458 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6459 | // Convert the object parameter. |
| 6460 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6461 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 6462 | Best->FoundDecl, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6463 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6464 | |
| 6465 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6466 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6467 | |
| 6468 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6469 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6470 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6471 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6472 | |
| 6473 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6474 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6475 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6476 | &Base, 1, ResultTy, OpLoc)); |
| 6477 | |
| 6478 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6479 | Method)) |
| 6480 | return ExprError(); |
| 6481 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6484 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6485 | /// a C++ overloaded function (possibly with some parentheses and |
| 6486 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6487 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6488 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 6489 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6490 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6491 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6492 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 6493 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6494 | if (SubExpr == PE->getSubExpr()) |
| 6495 | return PE->Retain(); |
| 6496 | |
| 6497 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6498 | } |
| 6499 | |
| 6500 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6501 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 6502 | Found, Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6503 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6504 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6505 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6506 | if (SubExpr == ICE->getSubExpr()) |
| 6507 | return ICE->Retain(); |
| 6508 | |
| 6509 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6510 | ICE->getCastKind(), |
| 6511 | SubExpr, |
| 6512 | ICE->isLvalueCast()); |
| 6513 | } |
| 6514 | |
| 6515 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6516 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6517 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6518 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6519 | if (Method->isStatic()) { |
| 6520 | // Do nothing: static member functions aren't any different |
| 6521 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6522 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6523 | // Fix the sub expression, which really has to be an |
| 6524 | // UnresolvedLookupExpr holding an overloaded member function |
| 6525 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6526 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 6527 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6528 | if (SubExpr == UnOp->getSubExpr()) |
| 6529 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6530 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6531 | assert(isa<DeclRefExpr>(SubExpr) |
| 6532 | && "fixed to something other than a decl ref"); |
| 6533 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6534 | && "fixed to a member ref with no nested name qualifier"); |
| 6535 | |
| 6536 | // We have taken the address of a pointer to member |
| 6537 | // function. Perform the computation here so that we get the |
| 6538 | // appropriate pointer to member type. |
| 6539 | QualType ClassType |
| 6540 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6541 | QualType MemPtrType |
| 6542 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6543 | |
| 6544 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6545 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6546 | } |
| 6547 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6548 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 6549 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6550 | if (SubExpr == UnOp->getSubExpr()) |
| 6551 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6552 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6553 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6554 | Context.getPointerType(SubExpr->getType()), |
| 6555 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6556 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6557 | |
| 6558 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6559 | // FIXME: avoid copy. |
| 6560 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6561 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6562 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6563 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6566 | return DeclRefExpr::Create(Context, |
| 6567 | ULE->getQualifier(), |
| 6568 | ULE->getQualifierRange(), |
| 6569 | Fn, |
| 6570 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6571 | Fn->getType(), |
| 6572 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6575 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6576 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6577 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6578 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6579 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6580 | TemplateArgs = &TemplateArgsBuffer; |
| 6581 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6582 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6583 | Expr *Base; |
| 6584 | |
| 6585 | // If we're filling in |
| 6586 | if (MemExpr->isImplicitAccess()) { |
| 6587 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6588 | return DeclRefExpr::Create(Context, |
| 6589 | MemExpr->getQualifier(), |
| 6590 | MemExpr->getQualifierRange(), |
| 6591 | Fn, |
| 6592 | MemExpr->getMemberLoc(), |
| 6593 | Fn->getType(), |
| 6594 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6595 | } else { |
| 6596 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6597 | if (MemExpr->getQualifier()) |
| 6598 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6599 | Base = new (Context) CXXThisExpr(Loc, |
| 6600 | MemExpr->getBaseType(), |
| 6601 | /*isImplicit=*/true); |
| 6602 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6603 | } else |
| 6604 | Base = MemExpr->getBase()->Retain(); |
| 6605 | |
| 6606 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6607 | MemExpr->isArrow(), |
| 6608 | MemExpr->getQualifier(), |
| 6609 | MemExpr->getQualifierRange(), |
| 6610 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6611 | Found, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6612 | MemExpr->getMemberLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6613 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6614 | Fn->getType()); |
| 6615 | } |
| 6616 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6617 | assert(false && "Invalid reference to overloaded function"); |
| 6618 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6619 | } |
| 6620 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6621 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 6622 | DeclAccessPair Found, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6623 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6624 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6627 | } // end namespace clang |