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 | } |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 229 | OS << '\'' << ConversionFunction << '\''; |
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. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 439 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 440 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 441 | bool SuppressUserConversions, |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 442 | bool AllowExplicit, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 443 | bool InOverloadResolution) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 444 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 445 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 446 | ICS.setStandard(); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 447 | return ICS; |
| 448 | } |
| 449 | |
| 450 | if (!getLangOptions().CPlusPlus) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 451 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 452 | return ICS; |
| 453 | } |
| 454 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 455 | if (SuppressUserConversions) { |
| 456 | // C++ [over.ics.user]p4: |
| 457 | // A conversion of an expression of class type to the same class |
| 458 | // type is given Exact Match rank, and a conversion of an |
| 459 | // expression of class type to a base class of that type is |
| 460 | // given Conversion rank, in spite of the fact that a copy/move |
| 461 | // constructor (i.e., a user-defined conversion function) is |
| 462 | // called for those cases. |
| 463 | QualType FromType = From->getType(); |
| 464 | if (!ToType->getAs<RecordType>() || !FromType->getAs<RecordType>() || |
| 465 | !(Context.hasSameUnqualifiedType(FromType, ToType) || |
| 466 | IsDerivedFrom(FromType, ToType))) { |
| 467 | // We're not in the case above, so there is no conversion that |
| 468 | // we can perform. |
| 469 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 470 | return ICS; |
| 471 | } |
| 472 | |
| 473 | ICS.setStandard(); |
| 474 | ICS.Standard.setAsIdentityConversion(); |
| 475 | ICS.Standard.setFromType(FromType); |
| 476 | ICS.Standard.setAllToTypes(ToType); |
| 477 | |
| 478 | // We don't actually check at this point whether there is a valid |
| 479 | // copy/move constructor, since overloading just assumes that it |
| 480 | // exists. When we actually perform initialization, we'll find the |
| 481 | // appropriate constructor to copy the returned object, if needed. |
| 482 | ICS.Standard.CopyConstructor = 0; |
| 483 | |
| 484 | // Determine whether this is considered a derived-to-base conversion. |
| 485 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 486 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 487 | |
| 488 | return ICS; |
| 489 | } |
| 490 | |
| 491 | // Attempt user-defined conversion. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 492 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 493 | OverloadingResult UserDefResult |
| 494 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 495 | AllowExplicit); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 496 | |
| 497 | if (UserDefResult == OR_Success) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 498 | ICS.setUserDefined(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 499 | // C++ [over.ics.user]p4: |
| 500 | // A conversion of an expression of class type to the same class |
| 501 | // type is given Exact Match rank, and a conversion of an |
| 502 | // expression of class type to a base class of that type is |
| 503 | // given Conversion rank, in spite of the fact that a copy |
| 504 | // constructor (i.e., a user-defined conversion function) is |
| 505 | // called for those cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 507 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 509 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 510 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 511 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 0d6d12b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 512 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 513 | // Turn this into a "standard" conversion sequence, so that it |
| 514 | // gets ranked with standard conversion sequences. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 515 | ICS.setStandard(); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 516 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 517 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 518 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 519 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 520 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 521 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 522 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 523 | } |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 524 | |
| 525 | // C++ [over.best.ics]p4: |
| 526 | // However, when considering the argument of a user-defined |
| 527 | // conversion function that is a candidate by 13.3.1.3 when |
| 528 | // invoked for the copying of the temporary in the second step |
| 529 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 530 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 531 | // ellipsis conversion sequences are allowed. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 532 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 533 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 534 | } |
John McCall | cefd3ad | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 535 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 536 | ICS.setAmbiguous(); |
| 537 | ICS.Ambiguous.setFromType(From->getType()); |
| 538 | ICS.Ambiguous.setToType(ToType); |
| 539 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 540 | Cand != Conversions.end(); ++Cand) |
| 541 | if (Cand->Viable) |
| 542 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 543 | } else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 544 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | b1663d0 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 545 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 546 | |
| 547 | return ICS; |
| 548 | } |
| 549 | |
Douglas Gregor | 575c63a | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 550 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 551 | /// expression From to the type ToType. Returns true if there was an |
| 552 | /// error, false otherwise. The expression From is replaced with the |
| 553 | /// converted expression. Flavor is the kind of conversion we're |
| 554 | /// performing, used in the error message. If @p AllowExplicit, |
| 555 | /// explicit user-defined conversions are permitted. |
| 556 | bool |
| 557 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 558 | AssignmentAction Action, bool AllowExplicit) { |
| 559 | ImplicitConversionSequence ICS; |
| 560 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 561 | } |
| 562 | |
| 563 | bool |
| 564 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 565 | AssignmentAction Action, bool AllowExplicit, |
| 566 | ImplicitConversionSequence& ICS) { |
| 567 | ICS = TryImplicitConversion(From, ToType, |
| 568 | /*SuppressUserConversions=*/false, |
| 569 | AllowExplicit, |
| 570 | /*InOverloadResolution=*/false); |
| 571 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 572 | } |
| 573 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 574 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 575 | /// conversion that strips "noreturn" off the nested function type. |
| 576 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 577 | QualType ToType, QualType &ResultTy) { |
| 578 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 579 | return false; |
| 580 | |
| 581 | // Strip the noreturn off the type we're converting from; noreturn can |
| 582 | // safely be removed. |
| 583 | FromType = Context.getNoReturnType(FromType, false); |
| 584 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 585 | return false; |
| 586 | |
| 587 | ResultTy = FromType; |
| 588 | return true; |
| 589 | } |
| 590 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 591 | /// IsStandardConversion - Determines whether there is a standard |
| 592 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 593 | /// expression From to the type ToType. Standard conversion sequences |
| 594 | /// only consider non-class types; for conversions that involve class |
| 595 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 596 | /// contain the standard conversion sequence required to perform this |
| 597 | /// conversion and this routine will return true. Otherwise, this |
| 598 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | bool |
| 600 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 601 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 603 | QualType FromType = From->getType(); |
| 604 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 605 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 606 | SCS.setAsIdentityConversion(); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 607 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 608 | SCS.IncompatibleObjC = false; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 609 | SCS.setFromType(FromType); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 610 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 612 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 614 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 615 | if (getLangOptions().CPlusPlus) |
| 616 | return false; |
| 617 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 621 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 622 | // array-to-pointer conversion, or function-to-pointer conversion |
| 623 | // (C++ 4p1). |
| 624 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 625 | DeclAccessPair AccessPair; |
| 626 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 628 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 629 | // converted to an rvalue. |
| 630 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 632 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 633 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 634 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 635 | |
| 636 | // If T is a non-class type, the type of the rvalue is the |
| 637 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 638 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 639 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 640 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 641 | } else if (FromType->isArrayType()) { |
| 642 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 643 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 644 | |
| 645 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 646 | // bound of T" can be converted to an rvalue of type "pointer to |
| 647 | // T" (C++ 4.2p1). |
| 648 | FromType = Context.getArrayDecayedType(FromType); |
| 649 | |
| 650 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 651 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 652 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 653 | |
| 654 | // For the purpose of ranking in overload resolution |
| 655 | // (13.3.3.1.1), this conversion is considered an |
| 656 | // array-to-pointer conversion followed by a qualification |
| 657 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 658 | SCS.Second = ICK_Identity; |
| 659 | SCS.Third = ICK_Qualification; |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 660 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 661 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 662 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 663 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 664 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 665 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 666 | |
| 667 | // An lvalue of function type T can be converted to an rvalue of |
| 668 | // type "pointer to T." The result is a pointer to the |
| 669 | // function. (C++ 4.3p1). |
| 670 | FromType = Context.getPointerType(FromType); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 671 | } else if (From->getType() == Context.OverloadTy) { |
| 672 | if (FunctionDecl *Fn |
| 673 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 674 | AccessPair)) { |
| 675 | // Address of overloaded function (C++ [over.over]). |
| 676 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 678 | // We were able to resolve the address of the overloaded function, |
| 679 | // so we can convert to the type of that function. |
| 680 | FromType = Fn->getType(); |
| 681 | if (ToType->isLValueReferenceType()) |
| 682 | FromType = Context.getLValueReferenceType(FromType); |
| 683 | else if (ToType->isRValueReferenceType()) |
| 684 | FromType = Context.getRValueReferenceType(FromType); |
| 685 | else if (ToType->isMemberPointerType()) { |
| 686 | // Resolve address only succeeds if both sides are member pointers, |
| 687 | // but it doesn't have to be the same class. See DR 247. |
| 688 | // Note that this means that the type of &Derived::fn can be |
| 689 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 690 | // base class, even if it was brought into the derived class via a |
| 691 | // using declaration. The standard isn't clear on this issue at all. |
| 692 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 693 | FromType = Context.getMemberPointerType(FromType, |
| 694 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 695 | } else { |
| 696 | FromType = Context.getPointerType(FromType); |
| 697 | } |
| 698 | } else { |
| 699 | return false; |
| 700 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 701 | } else { |
| 702 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 703 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 704 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 705 | SCS.setToType(0, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 706 | |
| 707 | // The second conversion can be an integral promotion, floating |
| 708 | // point promotion, integral conversion, floating point conversion, |
| 709 | // floating-integral conversion, pointer conversion, |
| 710 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 711 | // For overloading in C, this can also be a "compatible-type" |
| 712 | // conversion. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 713 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 714 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 715 | // The unqualified versions of the types are the same: there's no |
| 716 | // conversion to do. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 717 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 718 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 720 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 721 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 722 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 723 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 724 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 725 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 726 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 727 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 728 | SCS.Second = ICK_Complex_Promotion; |
| 729 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 730 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 731 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 732 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 733 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 734 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 735 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 736 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 737 | SCS.Second = ICK_Complex_Conversion; |
| 738 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 23a370f | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 739 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 740 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 741 | // Complex-real conversions (C99 6.3.1.7) |
| 742 | SCS.Second = ICK_Complex_Real; |
| 743 | FromType = ToType.getUnqualifiedType(); |
| 744 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 745 | // Floating point conversions (C++ 4.8). |
| 746 | SCS.Second = ICK_Floating_Conversion; |
| 747 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 748 | } else if ((FromType->isFloatingType() && |
| 749 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 750 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 752 | ToType->isFloatingType())) { |
| 753 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 754 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 755 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 756 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 757 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 758 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 759 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 760 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 761 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 762 | InOverloadResolution, FromType)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 763 | // Pointer to member conversions (4.11). |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 764 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 765 | } else if (ToType->isBooleanType() && |
| 766 | (FromType->isArithmeticType() || |
| 767 | FromType->isEnumeralType() || |
Fariborz Jahanian | 1f7711d | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 768 | FromType->isAnyPointerType() || |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 769 | FromType->isBlockPointerType() || |
| 770 | FromType->isMemberPointerType() || |
| 771 | FromType->isNullPtrType())) { |
| 772 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 773 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 774 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 776 | Context.typesAreCompatible(ToType, FromType)) { |
| 777 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 778 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 779 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 780 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 781 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 782 | } else { |
| 783 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 784 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 785 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 786 | SCS.setToType(1, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 788 | QualType CanonFrom; |
| 789 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 790 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 791 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 792 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 793 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 794 | CanonFrom = Context.getCanonicalType(FromType); |
| 795 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 796 | } else { |
| 797 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 798 | SCS.Third = ICK_Identity; |
| 799 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 801 | // [...] Any difference in top-level cv-qualification is |
| 802 | // subsumed by the initialization itself and does not constitute |
| 803 | // a conversion. [...] |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 804 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 806 | if (CanonFrom.getLocalUnqualifiedType() |
| 807 | == CanonTo.getLocalUnqualifiedType() && |
| 808 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 809 | FromType = ToType; |
| 810 | CanonFrom = CanonTo; |
| 811 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 812 | } |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 813 | SCS.setToType(2, FromType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 814 | |
| 815 | // If we have not converted the argument type to the parameter type, |
| 816 | // this is a bad conversion sequence. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 817 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 818 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 820 | return true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 824 | /// expression From (whose potentially-adjusted type is FromType) to |
| 825 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 826 | /// sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 828 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 829 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 830 | if (!To) { |
| 831 | return false; |
| 832 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 833 | |
| 834 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 835 | // unsigned short int can be converted to an rvalue of type int if |
| 836 | // int can represent all the values of the source type; otherwise, |
| 837 | // the source rvalue can be converted to an rvalue of type unsigned |
| 838 | // int (C++ 4.5p1). |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 839 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 840 | !FromType->isEnumeralType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 841 | if (// We can promote any signed, promotable integer type to an int |
| 842 | (FromType->isSignedIntegerType() || |
| 843 | // We can promote any unsigned integer type whose size is |
| 844 | // less than int to an int. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 846 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 847 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 850 | return To->getKind() == BuiltinType::UInt; |
| 851 | } |
| 852 | |
| 853 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 854 | // can be converted to an rvalue of the first of the following types |
| 855 | // that can represent all the values of its underlying type: int, |
| 856 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 857 | |
| 858 | // We pre-calculate the promotion type for enum types. |
| 859 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 860 | if (ToType->isIntegerType()) |
| 861 | return Context.hasSameUnqualifiedType(ToType, |
| 862 | FromEnumType->getDecl()->getPromotionType()); |
| 863 | |
| 864 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 865 | // Determine whether the type we're converting from is signed or |
| 866 | // unsigned. |
| 867 | bool FromIsSigned; |
| 868 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 869 | |
| 870 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 871 | FromIsSigned = true; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 872 | |
| 873 | // The types we'll try to promote to, in the appropriate |
| 874 | // order. Try each of these types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | QualType PromoteTypes[6] = { |
| 876 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 877 | Context.LongTy, Context.UnsignedLongTy , |
| 878 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 879 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 880 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 881 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 882 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 884 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 885 | // We found the type that we can promote to. If this is the |
| 886 | // type we wanted, we have a promotion. Otherwise, no |
| 887 | // promotion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 888 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 894 | // rvalue of type int if int can represent all the values of the |
| 895 | // bit-field; otherwise, it can be converted to unsigned int if |
| 896 | // unsigned int can represent all the values of the bit-field. If |
| 897 | // the bit-field is larger yet, no integral promotion applies to |
| 898 | // it. If the bit-field has an enumerated type, it is treated as any |
| 899 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 900 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 901 | // conversion. |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 902 | using llvm::APSInt; |
| 903 | if (From) |
| 904 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 905 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 906 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 907 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 908 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 909 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 911 | // Are we promoting to an int from a bitfield that fits in an int? |
| 912 | if (BitWidth < ToSize || |
| 913 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 914 | return To->getKind() == BuiltinType::Int; |
| 915 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 917 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 918 | // that fits into an unsigned int? |
| 919 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 920 | return To->getKind() == BuiltinType::UInt; |
| 921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 923 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 924 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 927 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 928 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 929 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 930 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 931 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 932 | |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 937 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 938 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 940 | /// An rvalue of type float can be converted to an rvalue of type |
| 941 | /// double. (C++ 4.6p1). |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 942 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 943 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 944 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 945 | ToBuiltin->getKind() == BuiltinType::Double) |
| 946 | return true; |
| 947 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 948 | // C99 6.3.1.5p1: |
| 949 | // When a float is promoted to double or long double, or a |
| 950 | // double is promoted to long double [...]. |
| 951 | if (!getLangOptions().CPlusPlus && |
| 952 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 953 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 954 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 955 | return true; |
| 956 | } |
| 957 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 958 | return false; |
| 959 | } |
| 960 | |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 961 | /// \brief Determine if a conversion is a complex promotion. |
| 962 | /// |
| 963 | /// A complex promotion is defined as a complex -> complex conversion |
| 964 | /// where the conversion between the underlying real types is a |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 965 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 966 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 967 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 968 | if (!FromComplex) |
| 969 | return false; |
| 970 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 971 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 972 | if (!ToComplex) |
| 973 | return false; |
| 974 | |
| 975 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 976 | ToComplex->getElementType()) || |
| 977 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 978 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 981 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 982 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 983 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 984 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 985 | /// the right set of qualifiers on its pointee. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | static QualType |
| 987 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 988 | QualType ToPointee, QualType ToType, |
| 989 | ASTContext &Context) { |
| 990 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 991 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 992 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
| 994 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 995 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 996 | // ToType is exactly what we need. Return it. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 997 | if (!ToType.isNull()) |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 998 | return ToType; |
| 999 | |
| 1000 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1001 | // already. |
| 1002 | return Context.getPointerType(ToPointee); |
| 1003 | } |
| 1004 | |
| 1005 | // Just build a canonical type that has the right qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1006 | return Context.getPointerType( |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1007 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1008 | Quals)); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1011 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1012 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1013 | /// not have the right set of qualifiers. |
| 1014 | static QualType |
| 1015 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1016 | QualType ToType, |
| 1017 | ASTContext &Context) { |
| 1018 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1019 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1020 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1021 | |
| 1022 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1023 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1024 | return ToType; |
| 1025 | |
| 1026 | // Just build a canonical type that has the right qualifiers. |
| 1027 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1028 | } |
| 1029 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1031 | bool InOverloadResolution, |
| 1032 | ASTContext &Context) { |
| 1033 | // Handle value-dependent integral null pointer constants correctly. |
| 1034 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1035 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 1036 | Expr->getType()->isIntegralType()) |
| 1037 | return !InOverloadResolution; |
| 1038 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1039 | return Expr->isNullPointerConstant(Context, |
| 1040 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1041 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1042 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1044 | /// IsPointerConversion - Determines whether the conversion of the |
| 1045 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1046 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1047 | /// 4.10). If so, returns true and places the converted type (that |
| 1048 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1049 | /// ConvertedType. |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1050 | /// |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1051 | /// This routine also supports conversions to and from block pointers |
| 1052 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1053 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1054 | /// appropriate overloading rules for Objective-C, we may want to |
| 1055 | /// split the Objective-C checks into a different routine; however, |
| 1056 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1057 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1058 | /// set if the conversion is an allowed Objective-C conversion that |
| 1059 | /// should result in a warning. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1060 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1061 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1062 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1064 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1065 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1066 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1067 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1069 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1070 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1071 | ConvertedType = ToType; |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1075 | // Blocks: Block pointers can be converted to void*. |
| 1076 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1077 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1078 | ConvertedType = ToType; |
| 1079 | return true; |
| 1080 | } |
| 1081 | // Blocks: A null pointer constant can be converted to a block |
| 1082 | // pointer type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1084 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1085 | ConvertedType = ToType; |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1089 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1090 | // pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1092 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1093 | ConvertedType = ToType; |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1097 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1098 | if (!ToTypePtr) |
| 1099 | return false; |
| 1100 | |
| 1101 | // 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] | 1102 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1103 | ConvertedType = ToType; |
| 1104 | return true; |
| 1105 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1106 | |
Fariborz Jahanian | adcfab1 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1107 | // Beyond this point, both types need to be pointers |
| 1108 | // , including objective-c pointers. |
| 1109 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1110 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1111 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1112 | ToType, Context); |
| 1113 | return true; |
| 1114 | |
| 1115 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1116 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1117 | if (!FromTypePtr) |
| 1118 | return false; |
| 1119 | |
| 1120 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1122 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1123 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1124 | // 4.10p2). |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1125 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1127 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1128 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1129 | return true; |
| 1130 | } |
| 1131 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1132 | // When we're overloading in C, we allow a special kind of pointer |
| 1133 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1135 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1137 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1139 | return true; |
| 1140 | } |
| 1141 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1142 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | // |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1144 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1145 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1146 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1147 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1148 | // necessitates this conversion is ill-formed. The result of the |
| 1149 | // conversion is a pointer to the base class sub-object of the |
| 1150 | // derived class object. The null pointer value is converted to |
| 1151 | // the null pointer value of the destination type. |
| 1152 | // |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1153 | // Note that we do not check for ambiguity or inaccessibility |
| 1154 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1155 | if (getLangOptions().CPlusPlus && |
| 1156 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | bf1764c | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1157 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | 2685eab | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1158 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1159 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1161 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1162 | ToType, Context); |
| 1163 | return true; |
| 1164 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1166 | return false; |
| 1167 | } |
| 1168 | |
| 1169 | /// isObjCPointerConversion - Determines whether this is an |
| 1170 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1171 | /// with the same arguments and return values. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1173 | QualType& ConvertedType, |
| 1174 | bool &IncompatibleObjC) { |
| 1175 | if (!getLangOptions().ObjC1) |
| 1176 | return false; |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1177 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1178 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1179 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1181 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1182 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1183 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1184 | // 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] | 1185 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1186 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1187 | ConvertedType = ToType; |
| 1188 | return true; |
| 1189 | } |
| 1190 | // Conversions with Objective-C's id<...>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1192 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1194 | /*compare=*/false)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1195 | ConvertedType = ToType; |
| 1196 | return true; |
| 1197 | } |
| 1198 | // Objective C++: We're able to convert from a pointer to an |
| 1199 | // interface to a pointer to a different interface. |
| 1200 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | ee9ca69 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1201 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1202 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1203 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1204 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1205 | FromObjCPtr->getPointeeType())) |
| 1206 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1207 | ConvertedType = ToType; |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
| 1211 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1212 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1213 | // interfaces, which is permitted. However, we're going to |
| 1214 | // complain about it. |
| 1215 | IncompatibleObjC = true; |
| 1216 | ConvertedType = FromType; |
| 1217 | return true; |
| 1218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1220 | // 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] | 1221 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1222 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1223 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1224 | else if (const BlockPointerType *ToBlockPtr = |
| 1225 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 4816839 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1226 | // 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] | 1227 | // to a block pointer type. |
| 1228 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1229 | ConvertedType = ToType; |
| 1230 | return true; |
| 1231 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1232 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | b351a7d | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1233 | } |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1234 | else if (FromType->getAs<BlockPointerType>() && |
| 1235 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1236 | // 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] | 1237 | // pointer to any object. |
Fariborz Jahanian | f7c43fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1238 | ConvertedType = ToType; |
| 1239 | return true; |
| 1240 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1241 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1242 | return false; |
| 1243 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1244 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1245 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1246 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1247 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1248 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1249 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1250 | return false; |
| 1251 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1252 | // If we have pointers to pointers, recursively check whether this |
| 1253 | // is an Objective-C conversion. |
| 1254 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1255 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1256 | IncompatibleObjC)) { |
| 1257 | // We always complain about this conversion. |
| 1258 | IncompatibleObjC = true; |
| 1259 | ConvertedType = ToType; |
| 1260 | return true; |
| 1261 | } |
Fariborz Jahanian | 83b7b31 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1262 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1263 | // as in I* to id. |
| 1264 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1265 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1266 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1267 | IncompatibleObjC)) { |
| 1268 | ConvertedType = ToType; |
| 1269 | return true; |
| 1270 | } |
| 1271 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1272 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1273 | // differences in the argument and result types are in Objective-C |
| 1274 | // pointer conversions. If so, we permit the conversion (but |
| 1275 | // complain about it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | const FunctionProtoType *FromFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1277 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1278 | const FunctionProtoType *ToFunctionType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1279 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1280 | if (FromFunctionType && ToFunctionType) { |
| 1281 | // If the function types are exactly the same, this isn't an |
| 1282 | // Objective-C pointer conversion. |
| 1283 | if (Context.getCanonicalType(FromPointeeType) |
| 1284 | == Context.getCanonicalType(ToPointeeType)) |
| 1285 | return false; |
| 1286 | |
| 1287 | // Perform the quick checks that will tell us whether these |
| 1288 | // function types are obviously different. |
| 1289 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1290 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1291 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1292 | return false; |
| 1293 | |
| 1294 | bool HasObjCConversion = false; |
| 1295 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1296 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1297 | // Okay, the types match exactly. Nothing to do. |
| 1298 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1299 | ToFunctionType->getResultType(), |
| 1300 | ConvertedType, IncompatibleObjC)) { |
| 1301 | // Okay, we have an Objective-C pointer conversion. |
| 1302 | HasObjCConversion = true; |
| 1303 | } else { |
| 1304 | // Function types are too different. Abort. |
| 1305 | return false; |
| 1306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1308 | // Check argument types. |
| 1309 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1310 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1311 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1312 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1313 | if (Context.getCanonicalType(FromArgType) |
| 1314 | == Context.getCanonicalType(ToArgType)) { |
| 1315 | // Okay, the types match exactly. Nothing to do. |
| 1316 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1317 | ConvertedType, IncompatibleObjC)) { |
| 1318 | // Okay, we have an Objective-C pointer conversion. |
| 1319 | HasObjCConversion = true; |
| 1320 | } else { |
| 1321 | // Argument types are too different. Abort. |
| 1322 | return false; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | if (HasObjCConversion) { |
| 1327 | // We had an Objective-C conversion. Allow this pointer |
| 1328 | // conversion, but complain about it. |
| 1329 | ConvertedType = ToType; |
| 1330 | IncompatibleObjC = true; |
| 1331 | return true; |
| 1332 | } |
| 1333 | } |
| 1334 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1335 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1338 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1339 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1340 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1341 | /// conversions for which IsPointerConversion has already returned |
| 1342 | /// true. It returns true and produces a diagnostic if there was an |
| 1343 | /// error, or returns false otherwise. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1344 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1345 | CastExpr::CastKind &Kind, |
| 1346 | bool IgnoreBaseAccess) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1347 | QualType FromType = From->getType(); |
| 1348 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1349 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1350 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1351 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1352 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1354 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1355 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1356 | // We must have a derived-to-base conversion. Check an |
| 1357 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1358 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1359 | From->getExprLoc(), |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1360 | From->getSourceRange(), |
| 1361 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1362 | return true; |
| 1363 | |
| 1364 | // The conversion was successful. |
| 1365 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1369 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1371 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1372 | // Objective-C++ conversions are always okay. |
| 1373 | // FIXME: We should have a different class of conversions for the |
| 1374 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1375 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1376 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1377 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1378 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1379 | return false; |
| 1380 | } |
| 1381 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1382 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1383 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1384 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1385 | /// If so, returns true and places the converted type (that might differ from |
| 1386 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1387 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1388 | QualType ToType, |
| 1389 | bool InOverloadResolution, |
| 1390 | QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1391 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1392 | if (!ToTypePtr) |
| 1393 | return false; |
| 1394 | |
| 1395 | // 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] | 1396 | if (From->isNullPointerConstant(Context, |
| 1397 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1398 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1399 | ConvertedType = ToType; |
| 1400 | return true; |
| 1401 | } |
| 1402 | |
| 1403 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1404 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1405 | if (!FromTypePtr) |
| 1406 | return false; |
| 1407 | |
| 1408 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1409 | // where D is derived from B (C++ 4.11p2). |
| 1410 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1411 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1412 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1413 | |
| 1414 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1415 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1416 | ToClass.getTypePtr()); |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | return false; |
| 1421 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1422 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1423 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1424 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1425 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1426 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1427 | /// true and produces a diagnostic if there was an error, or returns false |
| 1428 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1430 | CastExpr::CastKind &Kind, |
| 1431 | bool IgnoreBaseAccess) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1432 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1433 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1434 | if (!FromPtrType) { |
| 1435 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1436 | assert(From->isNullPointerConstant(Context, |
| 1437 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1438 | "Expr must be null pointer constant!"); |
| 1439 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1440 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1441 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1442 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1443 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1444 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1445 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1446 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1447 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1448 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1449 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1450 | // FIXME: What about dependent types? |
| 1451 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1452 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1453 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1454 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/ true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1455 | /*DetectVirtual=*/true); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1456 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1457 | assert(DerivationOkay && |
| 1458 | "Should not have been called if derivation isn't OK."); |
| 1459 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1460 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1461 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1462 | getUnqualifiedType())) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1463 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1464 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1465 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1466 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1467 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1469 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1470 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1471 | << FromClass << ToClass << QualType(VBase, 0) |
| 1472 | << From->getSourceRange(); |
| 1473 | return true; |
| 1474 | } |
| 1475 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1476 | if (!IgnoreBaseAccess) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1477 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1478 | Paths.front(), |
| 1479 | diag::err_downcast_from_inaccessible_base); |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1480 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1481 | // Must be a base to derived member conversion. |
| 1482 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1483 | return false; |
| 1484 | } |
| 1485 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1486 | /// IsQualificationConversion - Determines whether the conversion from |
| 1487 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1488 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | bool |
| 1490 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1491 | FromType = Context.getCanonicalType(FromType); |
| 1492 | ToType = Context.getCanonicalType(ToType); |
| 1493 | |
| 1494 | // If FromType and ToType are the same type, this is not a |
| 1495 | // qualification conversion. |
Sebastian Redl | 22c9240 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1496 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1497 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1499 | // (C++ 4.4p4): |
| 1500 | // A conversion can add cv-qualifiers at levels other than the first |
| 1501 | // in multi-level pointers, subject to the following rules: [...] |
| 1502 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1503 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1504 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1505 | // Within each iteration of the loop, we check the qualifiers to |
| 1506 | // determine if this still looks like a qualification |
| 1507 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1508 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1509 | // until there are no more pointers or pointers-to-members left to |
| 1510 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1511 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1512 | |
| 1513 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1514 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1515 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1516 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1518 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1519 | // every cv for 0 < k < j. |
| 1520 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1521 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1522 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1524 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1525 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1527 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1528 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1529 | |
| 1530 | // We are left with FromType and ToType being the pointee types |
| 1531 | // after unwrapping the original FromType and ToType the same number |
| 1532 | // of types. If we unwrapped any pointers, and if FromType and |
| 1533 | // ToType have the same unqualified type (since we checked |
| 1534 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1535 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1538 | /// Determines whether there is a user-defined conversion sequence |
| 1539 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1540 | /// ToType. If such a conversion exists, User will contain the |
| 1541 | /// user-defined conversion sequence that performs such a conversion |
| 1542 | /// and this routine will return true. Otherwise, this routine returns |
| 1543 | /// false and User is unspecified. |
| 1544 | /// |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1545 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1546 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1547 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1548 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1549 | UserDefinedConversionSequence& User, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1550 | OverloadCandidateSet& CandidateSet, |
| 1551 | bool AllowExplicit) { |
| 1552 | // Whether we will only visit constructors. |
| 1553 | bool ConstructorsOnly = false; |
| 1554 | |
| 1555 | // If the type we are conversion to is a class type, enumerate its |
| 1556 | // constructors. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1557 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1558 | // C++ [over.match.ctor]p1: |
| 1559 | // When objects of class type are direct-initialized (8.5), or |
| 1560 | // copy-initialized from an expression of the same or a |
| 1561 | // derived class type (8.5), overload resolution selects the |
| 1562 | // constructor. [...] For copy-initialization, the candidate |
| 1563 | // functions are all the converting constructors (12.3.1) of |
| 1564 | // that class. The argument list is the expression-list within |
| 1565 | // the parentheses of the initializer. |
| 1566 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1567 | (From->getType()->getAs<RecordType>() && |
| 1568 | IsDerivedFrom(From->getType(), ToType))) |
| 1569 | ConstructorsOnly = true; |
| 1570 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1571 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1572 | // We're not going to find any constructors. |
| 1573 | } else if (CXXRecordDecl *ToRecordDecl |
| 1574 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1576 | = Context.DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1577 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1578 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1580 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1581 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1582 | NamedDecl *D = *Con; |
| 1583 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1584 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1585 | // Find the constructor (which may be a template). |
| 1586 | CXXConstructorDecl *Constructor = 0; |
| 1587 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1588 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1589 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1591 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1592 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1593 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1594 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1595 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1596 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1597 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1598 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1599 | /*ExplicitArgs*/ 0, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1600 | &From, 1, CandidateSet, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1601 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1602 | else |
Fariborz Jahanian | 249cead | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1603 | // Allow one user-defined conversion when user specifies a |
| 1604 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1605 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1606 | &From, 1, CandidateSet, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1607 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1608 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1609 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1613 | // Enumerate conversion functions, if we're allowed to. |
| 1614 | if (ConstructorsOnly) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1616 | PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1617 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1619 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1621 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1622 | // Add all of the conversion functions as candidates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1623 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1624 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1625 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1626 | E = Conversions->end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1627 | DeclAccessPair FoundDecl = I.getPair(); |
| 1628 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1629 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1630 | if (isa<UsingShadowDecl>(D)) |
| 1631 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1632 | |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1633 | CXXConversionDecl *Conv; |
| 1634 | FunctionTemplateDecl *ConvTemplate; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1635 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1636 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1637 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1638 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1639 | |
| 1640 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1641 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1642 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1643 | ActingContext, From, ToType, |
| 1644 | CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1645 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1646 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1647 | From, ToType, CandidateSet); |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1648 | } |
| 1649 | } |
| 1650 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1651 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1652 | |
| 1653 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1654 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1655 | case OR_Success: |
| 1656 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1658 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1659 | // C++ [over.ics.user]p1: |
| 1660 | // If the user-defined conversion is specified by a |
| 1661 | // constructor (12.3.1), the initial standard conversion |
| 1662 | // sequence converts the source type to the type required by |
| 1663 | // the argument of the constructor. |
| 1664 | // |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1665 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1666 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1667 | User.EllipsisConversion = true; |
| 1668 | else { |
| 1669 | User.Before = Best->Conversions[0].Standard; |
| 1670 | User.EllipsisConversion = false; |
| 1671 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1672 | User.ConversionFunction = Constructor; |
| 1673 | User.After.setAsIdentityConversion(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1674 | User.After.setFromType( |
| 1675 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1676 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1677 | return OR_Success; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1678 | } else if (CXXConversionDecl *Conversion |
| 1679 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1680 | // C++ [over.ics.user]p1: |
| 1681 | // |
| 1682 | // [...] If the user-defined conversion is specified by a |
| 1683 | // conversion function (12.3.2), the initial standard |
| 1684 | // conversion sequence converts the source type to the |
| 1685 | // implicit object parameter of the conversion function. |
| 1686 | User.Before = Best->Conversions[0].Standard; |
| 1687 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1688 | User.EllipsisConversion = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | |
| 1690 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1691 | // The second standard conversion sequence converts the |
| 1692 | // result of the user-defined conversion to the target type |
| 1693 | // for the sequence. Since an implicit conversion sequence |
| 1694 | // is an initialization, the special rules for |
| 1695 | // initialization by user-defined conversion apply when |
| 1696 | // selecting the best user-defined conversion for a |
| 1697 | // user-defined conversion sequence (see 13.3.3 and |
| 1698 | // 13.3.3.1). |
| 1699 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1700 | return OR_Success; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1701 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1702 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1703 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1706 | case OR_No_Viable_Function: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1707 | return OR_No_Viable_Function; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1708 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1709 | // No conversion here! We're done. |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1710 | return OR_Deleted; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1711 | |
| 1712 | case OR_Ambiguous: |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1713 | return OR_Ambiguous; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Fariborz Jahanian | 34acd3e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1716 | return OR_No_Viable_Function; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1717 | } |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1718 | |
| 1719 | bool |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1720 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1721 | ImplicitConversionSequence ICS; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1722 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1723 | OverloadingResult OvResult = |
| 1724 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1725 | CandidateSet, false); |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1726 | if (OvResult == OR_Ambiguous) |
| 1727 | Diag(From->getSourceRange().getBegin(), |
| 1728 | diag::err_typecheck_ambiguous_condition) |
| 1729 | << From->getType() << ToType << From->getSourceRange(); |
| 1730 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1731 | Diag(From->getSourceRange().getBegin(), |
| 1732 | diag::err_typecheck_nonviable_condition) |
| 1733 | << From->getType() << ToType << From->getSourceRange(); |
| 1734 | else |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1735 | return false; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1736 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1737 | return true; |
| 1738 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1740 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1741 | /// conversion sequences to determine whether one is better than the |
| 1742 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1744 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1745 | const ImplicitConversionSequence& ICS2) |
| 1746 | { |
| 1747 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1748 | // conversion sequences (as defined in 13.3.3.1) |
| 1749 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1750 | // conversion sequence than a user-defined conversion sequence or |
| 1751 | // an ellipsis conversion sequence, and |
| 1752 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1753 | // conversion sequence than an ellipsis conversion sequence |
| 1754 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | // |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1756 | // C++0x [over.best.ics]p10: |
| 1757 | // For the purpose of ranking implicit conversion sequences as |
| 1758 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1759 | // treated as a user-defined sequence that is indistinguishable |
| 1760 | // from any other user-defined conversion sequence. |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1761 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 1762 | return ImplicitConversionSequence::Better; |
| 1763 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 1764 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1765 | |
Benjamin Kramer | b6eee07 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 1766 | // The following checks require both conversion sequences to be of |
| 1767 | // the same kind. |
| 1768 | if (ICS1.getKind() != ICS2.getKind()) |
| 1769 | return ImplicitConversionSequence::Indistinguishable; |
| 1770 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1771 | // Two implicit conversion sequences of the same form are |
| 1772 | // indistinguishable conversion sequences unless one of the |
| 1773 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1774 | if (ICS1.isStandard()) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1775 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1776 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1777 | // User-defined conversion sequence U1 is a better conversion |
| 1778 | // sequence than another user-defined conversion sequence U2 if |
| 1779 | // they contain the same user-defined conversion function or |
| 1780 | // constructor and if the second standard conversion sequence of |
| 1781 | // U1 is better than the second standard conversion sequence of |
| 1782 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1784 | ICS2.UserDefined.ConversionFunction) |
| 1785 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1786 | ICS2.UserDefined.After); |
| 1787 | } |
| 1788 | |
| 1789 | return ImplicitConversionSequence::Indistinguishable; |
| 1790 | } |
| 1791 | |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1792 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 1793 | // determine if one is a proper subset of the other. |
| 1794 | static ImplicitConversionSequence::CompareKind |
| 1795 | compareStandardConversionSubsets(ASTContext &Context, |
| 1796 | const StandardConversionSequence& SCS1, |
| 1797 | const StandardConversionSequence& SCS2) { |
| 1798 | ImplicitConversionSequence::CompareKind Result |
| 1799 | = ImplicitConversionSequence::Indistinguishable; |
| 1800 | |
| 1801 | if (SCS1.Second != SCS2.Second) { |
| 1802 | if (SCS1.Second == ICK_Identity) |
| 1803 | Result = ImplicitConversionSequence::Better; |
| 1804 | else if (SCS2.Second == ICK_Identity) |
| 1805 | Result = ImplicitConversionSequence::Worse; |
| 1806 | else |
| 1807 | return ImplicitConversionSequence::Indistinguishable; |
| 1808 | } else if (!Context.hasSameType(SCS1.getToType(1), SCS2.getToType(1))) |
| 1809 | return ImplicitConversionSequence::Indistinguishable; |
| 1810 | |
| 1811 | if (SCS1.Third == SCS2.Third) { |
| 1812 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 1813 | : ImplicitConversionSequence::Indistinguishable; |
| 1814 | } |
| 1815 | |
| 1816 | if (SCS1.Third == ICK_Identity) |
| 1817 | return Result == ImplicitConversionSequence::Worse |
| 1818 | ? ImplicitConversionSequence::Indistinguishable |
| 1819 | : ImplicitConversionSequence::Better; |
| 1820 | |
| 1821 | if (SCS2.Third == ICK_Identity) |
| 1822 | return Result == ImplicitConversionSequence::Better |
| 1823 | ? ImplicitConversionSequence::Indistinguishable |
| 1824 | : ImplicitConversionSequence::Worse; |
| 1825 | |
| 1826 | return ImplicitConversionSequence::Indistinguishable; |
| 1827 | } |
| 1828 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1829 | /// CompareStandardConversionSequences - Compare two standard |
| 1830 | /// conversion sequences to determine whether one is better than the |
| 1831 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1833 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1834 | const StandardConversionSequence& SCS2) |
| 1835 | { |
| 1836 | // Standard conversion sequence S1 is a better conversion sequence |
| 1837 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1838 | |
| 1839 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1840 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1841 | // excluding any Lvalue Transformation; the identity conversion |
| 1842 | // sequence is considered to be a subsequence of any |
| 1843 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1844 | if (ImplicitConversionSequence::CompareKind CK |
| 1845 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 1846 | return CK; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1847 | |
| 1848 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1849 | // defined below), or, if not that, |
| 1850 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1851 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1852 | if (Rank1 < Rank2) |
| 1853 | return ImplicitConversionSequence::Better; |
| 1854 | else if (Rank2 < Rank1) |
| 1855 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1857 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1858 | // are indistinguishable unless one of the following rules |
| 1859 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1861 | // A conversion that is not a conversion of a pointer, or |
| 1862 | // pointer to member, to bool is better than another conversion |
| 1863 | // that is such a conversion. |
| 1864 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1865 | return SCS2.isPointerConversionToBool() |
| 1866 | ? ImplicitConversionSequence::Better |
| 1867 | : ImplicitConversionSequence::Worse; |
| 1868 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1869 | // C++ [over.ics.rank]p4b2: |
| 1870 | // |
| 1871 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1872 | // conversion of B* to A* is better than conversion of B* to |
| 1873 | // void*, and conversion of A* to void* is better than conversion |
| 1874 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1876 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1878 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1879 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1880 | // Exactly one of the conversion sequences is a conversion to |
| 1881 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1882 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1883 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1884 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1885 | // Neither conversion sequence converts to a void pointer; compare |
| 1886 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1887 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1888 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1889 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1890 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1891 | // Both conversion sequences are conversions to void |
| 1892 | // pointers. Compare the source types to determine if there's an |
| 1893 | // inheritance relationship in their sources. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1894 | QualType FromType1 = SCS1.getFromType(); |
| 1895 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1896 | |
| 1897 | // Adjust the types we're converting from via the array-to-pointer |
| 1898 | // conversion, if we need to. |
| 1899 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1900 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1901 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1902 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1903 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1904 | QualType FromPointee1 |
| 1905 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1906 | QualType FromPointee2 |
| 1907 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | 0191969 | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1909 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1910 | return ImplicitConversionSequence::Better; |
| 1911 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1912 | return ImplicitConversionSequence::Worse; |
| 1913 | |
| 1914 | // Objective-C++: If one interface is more specific than the |
| 1915 | // other, it is the better one. |
| 1916 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1917 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1918 | if (FromIface1 && FromIface1) { |
| 1919 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1920 | return ImplicitConversionSequence::Better; |
| 1921 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1922 | return ImplicitConversionSequence::Worse; |
| 1923 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1924 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1925 | |
| 1926 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1927 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1929 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1930 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1932 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1933 | // C++0x [over.ics.rank]p3b4: |
| 1934 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1935 | // implicit object parameter of a non-static member function declared |
| 1936 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1937 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1938 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1939 | // or if the member function in this case has a ref qualifier. |
| 1940 | // (Of course, we don't have ref qualifiers yet.) |
| 1941 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1942 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1943 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1944 | |
| 1945 | // C++ [over.ics.rank]p3b4: |
| 1946 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1947 | // which the references refer are the same type except for |
| 1948 | // top-level cv-qualifiers, and the type to which the reference |
| 1949 | // initialized by S2 refers is more cv-qualified than the type |
| 1950 | // to which the reference initialized by S1 refers. |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1951 | QualType T1 = SCS1.getToType(2); |
| 1952 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1953 | T1 = Context.getCanonicalType(T1); |
| 1954 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1955 | Qualifiers T1Quals, T2Quals; |
| 1956 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1957 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1958 | if (UnqualT1 == UnqualT2) { |
| 1959 | // If the type is an array type, promote the element qualifiers to the type |
| 1960 | // for comparison. |
| 1961 | if (isa<ArrayType>(T1) && T1Quals) |
| 1962 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1963 | if (isa<ArrayType>(T2) && T2Quals) |
| 1964 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1965 | if (T2.isMoreQualifiedThan(T1)) |
| 1966 | return ImplicitConversionSequence::Better; |
| 1967 | else if (T1.isMoreQualifiedThan(T2)) |
| 1968 | return ImplicitConversionSequence::Worse; |
| 1969 | } |
| 1970 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1971 | |
| 1972 | return ImplicitConversionSequence::Indistinguishable; |
| 1973 | } |
| 1974 | |
| 1975 | /// CompareQualificationConversions - Compares two standard conversion |
| 1976 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1978 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1979 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1981 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1982 | // -- S1 and S2 differ only in their qualification conversion and |
| 1983 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1984 | // cv-qualification signature of type T1 is a proper subset of |
| 1985 | // the cv-qualification signature of type T2, and S1 is not the |
| 1986 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1987 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1988 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1989 | return ImplicitConversionSequence::Indistinguishable; |
| 1990 | |
| 1991 | // FIXME: the example in the standard doesn't use a qualification |
| 1992 | // conversion (!) |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1993 | QualType T1 = SCS1.getToType(2); |
| 1994 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1995 | T1 = Context.getCanonicalType(T1); |
| 1996 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1997 | Qualifiers T1Quals, T2Quals; |
| 1998 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1999 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2000 | |
| 2001 | // If the types are the same, we won't learn anything by unwrapped |
| 2002 | // them. |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2003 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2004 | return ImplicitConversionSequence::Indistinguishable; |
| 2005 | |
Chandler Carruth | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2006 | // If the type is an array type, promote the element qualifiers to the type |
| 2007 | // for comparison. |
| 2008 | if (isa<ArrayType>(T1) && T1Quals) |
| 2009 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2010 | if (isa<ArrayType>(T2) && T2Quals) |
| 2011 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2012 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2014 | = ImplicitConversionSequence::Indistinguishable; |
| 2015 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 2016 | // Within each iteration of the loop, we check the qualifiers to |
| 2017 | // determine if this still looks like a qualification |
| 2018 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2019 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2020 | // until there are no more pointers or pointers-to-members left |
| 2021 | // to unwrap. This essentially mimics what |
| 2022 | // IsQualificationConversion does, but here we're checking for a |
| 2023 | // strict subset of qualifiers. |
| 2024 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2025 | // The qualifiers are the same, so this doesn't tell us anything |
| 2026 | // about how the sequences rank. |
| 2027 | ; |
| 2028 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2029 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2030 | if (Result == ImplicitConversionSequence::Worse) |
| 2031 | // Neither has qualifiers that are a subset of the other's |
| 2032 | // qualifiers. |
| 2033 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2035 | Result = ImplicitConversionSequence::Better; |
| 2036 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2037 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2038 | if (Result == ImplicitConversionSequence::Better) |
| 2039 | // Neither has qualifiers that are a subset of the other's |
| 2040 | // qualifiers. |
| 2041 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2043 | Result = ImplicitConversionSequence::Worse; |
| 2044 | } else { |
| 2045 | // Qualifiers are disjoint. |
| 2046 | return ImplicitConversionSequence::Indistinguishable; |
| 2047 | } |
| 2048 | |
| 2049 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2050 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2051 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2054 | // Check that the winning standard conversion sequence isn't using |
| 2055 | // the deprecated string literal array to pointer conversion. |
| 2056 | switch (Result) { |
| 2057 | case ImplicitConversionSequence::Better: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2058 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2059 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2060 | break; |
| 2061 | |
| 2062 | case ImplicitConversionSequence::Indistinguishable: |
| 2063 | break; |
| 2064 | |
| 2065 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2066 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2067 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2068 | break; |
| 2069 | } |
| 2070 | |
| 2071 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2074 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2075 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2076 | /// various kinds of derived-to-base conversions (C++ |
| 2077 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2078 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2079 | ImplicitConversionSequence::CompareKind |
| 2080 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2081 | const StandardConversionSequence& SCS2) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2082 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2083 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2084 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2085 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Adjust the types we're converting from via the array-to-pointer |
| 2088 | // conversion, if we need to. |
| 2089 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2090 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2091 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2092 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2093 | |
| 2094 | // Canonicalize all of the types. |
| 2095 | FromType1 = Context.getCanonicalType(FromType1); |
| 2096 | ToType1 = Context.getCanonicalType(ToType1); |
| 2097 | FromType2 = Context.getCanonicalType(FromType2); |
| 2098 | ToType2 = Context.getCanonicalType(ToType2); |
| 2099 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2100 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2101 | // |
| 2102 | // If class B is derived directly or indirectly from class A and |
| 2103 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2104 | // |
| 2105 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2106 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2107 | |
| 2108 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2110 | SCS2.Second == ICK_Pointer_Conversion && |
| 2111 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2112 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2113 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2115 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2117 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2118 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2119 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2120 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2121 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2122 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2123 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2124 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2125 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2126 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2127 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2128 | // -- 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] | 2129 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2130 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2131 | return ImplicitConversionSequence::Better; |
| 2132 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2133 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2134 | |
| 2135 | if (ToIface1 && ToIface2) { |
| 2136 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2137 | return ImplicitConversionSequence::Better; |
| 2138 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2139 | return ImplicitConversionSequence::Worse; |
| 2140 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2141 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2142 | |
| 2143 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2144 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2145 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2146 | return ImplicitConversionSequence::Better; |
| 2147 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2148 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2150 | if (FromIface1 && FromIface2) { |
| 2151 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2152 | return ImplicitConversionSequence::Better; |
| 2153 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2154 | return ImplicitConversionSequence::Worse; |
| 2155 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2156 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2159 | // Ranking of member-pointer types. |
Fariborz Jahanian | 8577c98 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2160 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2161 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2162 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2163 | const MemberPointerType * FromMemPointer1 = |
| 2164 | FromType1->getAs<MemberPointerType>(); |
| 2165 | const MemberPointerType * ToMemPointer1 = |
| 2166 | ToType1->getAs<MemberPointerType>(); |
| 2167 | const MemberPointerType * FromMemPointer2 = |
| 2168 | FromType2->getAs<MemberPointerType>(); |
| 2169 | const MemberPointerType * ToMemPointer2 = |
| 2170 | ToType2->getAs<MemberPointerType>(); |
| 2171 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2172 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2173 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2174 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2175 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2176 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2177 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2178 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | 2357da0 | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2179 | // 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] | 2180 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2181 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2182 | return ImplicitConversionSequence::Worse; |
| 2183 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2184 | return ImplicitConversionSequence::Better; |
| 2185 | } |
| 2186 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2187 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2188 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2189 | return ImplicitConversionSequence::Better; |
| 2190 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2191 | return ImplicitConversionSequence::Worse; |
| 2192 | } |
| 2193 | } |
| 2194 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2195 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2196 | // -- 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] | 2197 | // -- binding of an expression of type C to a reference of type |
| 2198 | // B& is better than binding an expression of type C to a |
| 2199 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2200 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2201 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2202 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2203 | return ImplicitConversionSequence::Better; |
| 2204 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2205 | return ImplicitConversionSequence::Worse; |
| 2206 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2208 | // -- 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] | 2209 | // -- binding of an expression of type B to a reference of type |
| 2210 | // A& is better than binding an expression of type C to a |
| 2211 | // reference of type A&, |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2212 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2213 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2214 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2215 | return ImplicitConversionSequence::Better; |
| 2216 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2217 | return ImplicitConversionSequence::Worse; |
| 2218 | } |
| 2219 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2221 | return ImplicitConversionSequence::Indistinguishable; |
| 2222 | } |
| 2223 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2224 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2225 | /// determine whether they are reference-related, |
| 2226 | /// reference-compatible, reference-compatible with added |
| 2227 | /// qualification, or incompatible, for use in C++ initialization by |
| 2228 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2229 | /// type, and the first type (T1) is the pointee type of the reference |
| 2230 | /// type being initialized. |
| 2231 | Sema::ReferenceCompareResult |
| 2232 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2233 | QualType OrigT1, QualType OrigT2, |
| 2234 | bool& DerivedToBase) { |
| 2235 | assert(!OrigT1->isReferenceType() && |
| 2236 | "T1 must be the pointee type of the reference type"); |
| 2237 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2238 | |
| 2239 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2240 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2241 | Qualifiers T1Quals, T2Quals; |
| 2242 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2243 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2244 | |
| 2245 | // C++ [dcl.init.ref]p4: |
| 2246 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2247 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2248 | // T1 is a base class of T2. |
| 2249 | if (UnqualT1 == UnqualT2) |
| 2250 | DerivedToBase = false; |
| 2251 | else if (!RequireCompleteType(Loc, OrigT1, PDiag()) && |
| 2252 | !RequireCompleteType(Loc, OrigT2, PDiag()) && |
| 2253 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2254 | DerivedToBase = true; |
| 2255 | else |
| 2256 | return Ref_Incompatible; |
| 2257 | |
| 2258 | // At this point, we know that T1 and T2 are reference-related (at |
| 2259 | // least). |
| 2260 | |
| 2261 | // If the type is an array type, promote the element qualifiers to the type |
| 2262 | // for comparison. |
| 2263 | if (isa<ArrayType>(T1) && T1Quals) |
| 2264 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2265 | if (isa<ArrayType>(T2) && T2Quals) |
| 2266 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2267 | |
| 2268 | // C++ [dcl.init.ref]p4: |
| 2269 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2270 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2271 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2272 | // overload resolution, cases for which cv1 is greater |
| 2273 | // cv-qualification than cv2 are identified as |
| 2274 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2275 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2276 | return Ref_Compatible; |
| 2277 | else if (T1.isMoreQualifiedThan(T2)) |
| 2278 | return Ref_Compatible_With_Added_Qualification; |
| 2279 | else |
| 2280 | return Ref_Related; |
| 2281 | } |
| 2282 | |
| 2283 | /// \brief Compute an implicit conversion sequence for reference |
| 2284 | /// initialization. |
| 2285 | static ImplicitConversionSequence |
| 2286 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2287 | SourceLocation DeclLoc, |
| 2288 | bool SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2289 | bool AllowExplicit) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2290 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2291 | |
| 2292 | // Most paths end in a failed conversion. |
| 2293 | ImplicitConversionSequence ICS; |
| 2294 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2295 | |
| 2296 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2297 | QualType T2 = Init->getType(); |
| 2298 | |
| 2299 | // If the initializer is the address of an overloaded function, try |
| 2300 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2301 | // type of the resulting function. |
| 2302 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2303 | DeclAccessPair Found; |
| 2304 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2305 | false, Found)) |
| 2306 | T2 = Fn->getType(); |
| 2307 | } |
| 2308 | |
| 2309 | // Compute some basic properties of the types and the initializer. |
| 2310 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2311 | bool DerivedToBase = false; |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2312 | Expr::isLvalueResult InitLvalue = Init->isLvalue(S.Context); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2313 | Sema::ReferenceCompareResult RefRelationship |
| 2314 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
| 2315 | |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2316 | |
| 2317 | // C++ [over.ics.ref]p3: |
| 2318 | // Except for an implicit object parameter, for which see 13.3.1, |
| 2319 | // a standard conversion sequence cannot be formed if it requires |
| 2320 | // binding an lvalue reference to non-const to an rvalue or |
| 2321 | // binding an rvalue reference to an lvalue. |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2322 | // |
| 2323 | // FIXME: DPG doesn't trust this code. It seems far too early to |
| 2324 | // abort because of a binding of an rvalue reference to an lvalue. |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2325 | if (isRValRef && InitLvalue == Expr::LV_Valid) |
| 2326 | return ICS; |
| 2327 | |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2328 | // C++0x [dcl.init.ref]p16: |
| 2329 | // A reference to type "cv1 T1" is initialized by an expression |
| 2330 | // of type "cv2 T2" as follows: |
| 2331 | |
| 2332 | // -- If the initializer expression |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2333 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2334 | // reference-compatible with "cv2 T2," or |
| 2335 | // |
| 2336 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2337 | if (InitLvalue == Expr::LV_Valid && |
| 2338 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2339 | // C++ [over.ics.ref]p1: |
| 2340 | // When a parameter of reference type binds directly (8.5.3) |
| 2341 | // to an argument expression, the implicit conversion sequence |
| 2342 | // is the identity conversion, unless the argument expression |
| 2343 | // has a type that is a derived class of the parameter type, |
| 2344 | // in which case the implicit conversion sequence is a |
| 2345 | // derived-to-base Conversion (13.3.3.1). |
| 2346 | ICS.setStandard(); |
| 2347 | ICS.Standard.First = ICK_Identity; |
| 2348 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2349 | ICS.Standard.Third = ICK_Identity; |
| 2350 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2351 | ICS.Standard.setToType(0, T2); |
| 2352 | ICS.Standard.setToType(1, T1); |
| 2353 | ICS.Standard.setToType(2, T1); |
| 2354 | ICS.Standard.ReferenceBinding = true; |
| 2355 | ICS.Standard.DirectBinding = true; |
| 2356 | ICS.Standard.RRefBinding = false; |
| 2357 | ICS.Standard.CopyConstructor = 0; |
| 2358 | |
| 2359 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2360 | // derived-to-base conversions is suppressed when we're |
| 2361 | // computing the implicit conversion sequence (C++ |
| 2362 | // [over.best.ics]p2). |
| 2363 | return ICS; |
| 2364 | } |
| 2365 | |
| 2366 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2367 | // not reference-related to T2, and can be implicitly |
| 2368 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2369 | // is reference-compatible with "cv3 T3" 92) (this |
| 2370 | // conversion is selected by enumerating the applicable |
| 2371 | // conversion functions (13.3.1.6) and choosing the best |
| 2372 | // one through overload resolution (13.3)), |
| 2373 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
| 2374 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2375 | RefRelationship == Sema::Ref_Incompatible) { |
| 2376 | CXXRecordDecl *T2RecordDecl |
| 2377 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2378 | |
| 2379 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2380 | const UnresolvedSetImpl *Conversions |
| 2381 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2382 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2383 | E = Conversions->end(); I != E; ++I) { |
| 2384 | NamedDecl *D = *I; |
| 2385 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2386 | if (isa<UsingShadowDecl>(D)) |
| 2387 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2388 | |
| 2389 | FunctionTemplateDecl *ConvTemplate |
| 2390 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2391 | CXXConversionDecl *Conv; |
| 2392 | if (ConvTemplate) |
| 2393 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2394 | else |
| 2395 | Conv = cast<CXXConversionDecl>(D); |
| 2396 | |
| 2397 | // If the conversion function doesn't return a reference type, |
| 2398 | // it can't be considered for this conversion. |
| 2399 | if (Conv->getConversionType()->isLValueReferenceType() && |
| 2400 | (AllowExplicit || !Conv->isExplicit())) { |
| 2401 | if (ConvTemplate) |
| 2402 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2403 | Init, DeclType, CandidateSet); |
| 2404 | else |
| 2405 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2406 | DeclType, CandidateSet); |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | OverloadCandidateSet::iterator Best; |
| 2411 | switch (S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2412 | case OR_Success: |
| 2413 | // C++ [over.ics.ref]p1: |
| 2414 | // |
| 2415 | // [...] If the parameter binds directly to the result of |
| 2416 | // applying a conversion function to the argument |
| 2417 | // expression, the implicit conversion sequence is a |
| 2418 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2419 | // second standard conversion sequence either an identity |
| 2420 | // conversion or, if the conversion function returns an |
| 2421 | // entity of a type that is a derived class of the parameter |
| 2422 | // type, a derived-to-base Conversion. |
| 2423 | if (!Best->FinalConversion.DirectBinding) |
| 2424 | break; |
| 2425 | |
| 2426 | ICS.setUserDefined(); |
| 2427 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2428 | ICS.UserDefined.After = Best->FinalConversion; |
| 2429 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2430 | ICS.UserDefined.EllipsisConversion = false; |
| 2431 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2432 | ICS.UserDefined.After.DirectBinding && |
| 2433 | "Expected a direct reference binding!"); |
| 2434 | return ICS; |
| 2435 | |
| 2436 | case OR_Ambiguous: |
| 2437 | ICS.setAmbiguous(); |
| 2438 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2439 | Cand != CandidateSet.end(); ++Cand) |
| 2440 | if (Cand->Viable) |
| 2441 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2442 | return ICS; |
| 2443 | |
| 2444 | case OR_No_Viable_Function: |
| 2445 | case OR_Deleted: |
| 2446 | // There was no suitable conversion, or we found a deleted |
| 2447 | // conversion; continue with other checks. |
| 2448 | break; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | // -- Otherwise, the reference shall be to a non-volatile const |
| 2453 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 2454 | // rvalue reference and the initializer expression shall be an rvalue. |
Douglas Gregor | 66821b5 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2455 | // |
| 2456 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2457 | // point, which is that, due to p2 (which short-circuits reference |
| 2458 | // binding by only attempting a simple conversion for non-direct |
| 2459 | // bindings) and p3's strange wording, we allow a const volatile |
| 2460 | // reference to bind to an rvalue. Hence the check for the presence |
| 2461 | // of "const" rather than checking for "const" being the only |
| 2462 | // qualifier. |
| 2463 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2464 | return ICS; |
| 2465 | |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2466 | // -- if T2 is a class type and |
| 2467 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2468 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2469 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2470 | // -- T1 is not reference-related to T2 and the initializer |
| 2471 | // expression can be implicitly converted to an rvalue |
| 2472 | // of type "cv3 T3" (this conversion is selected by |
| 2473 | // enumerating the applicable conversion functions |
| 2474 | // (13.3.1.6) and choosing the best one through overload |
| 2475 | // resolution (13.3)), |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2476 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2477 | // then the reference is bound to the initializer |
| 2478 | // expression rvalue in the first case and to the object |
| 2479 | // that is the result of the conversion in the second case |
| 2480 | // (or, in either case, to the appropriate base class |
| 2481 | // subobject of the object). |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2482 | // |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2483 | // We're only checking the first case here, which is a direct |
| 2484 | // binding in C++0x but not in C++03. |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2485 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
| 2486 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2487 | ICS.setStandard(); |
| 2488 | ICS.Standard.First = ICK_Identity; |
| 2489 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2490 | ICS.Standard.Third = ICK_Identity; |
| 2491 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2492 | ICS.Standard.setToType(0, T2); |
| 2493 | ICS.Standard.setToType(1, T1); |
| 2494 | ICS.Standard.setToType(2, T1); |
| 2495 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | 9dc58bb | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2496 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2497 | ICS.Standard.RRefBinding = isRValRef; |
| 2498 | ICS.Standard.CopyConstructor = 0; |
| 2499 | return ICS; |
| 2500 | } |
| 2501 | |
| 2502 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2503 | // initialized from the initializer expression using the |
| 2504 | // rules for a non-reference copy initialization (8.5). The |
| 2505 | // reference is then bound to the temporary. If T1 is |
| 2506 | // reference-related to T2, cv1 must be the same |
| 2507 | // cv-qualification as, or greater cv-qualification than, |
| 2508 | // cv2; otherwise, the program is ill-formed. |
| 2509 | if (RefRelationship == Sema::Ref_Related) { |
| 2510 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2511 | // we would be reference-compatible or reference-compatible with |
| 2512 | // added qualification. But that wasn't the case, so the reference |
| 2513 | // initialization fails. |
| 2514 | return ICS; |
| 2515 | } |
| 2516 | |
| 2517 | // If at least one of the types is a class type, the types are not |
| 2518 | // related, and we aren't allowed any user conversions, the |
| 2519 | // reference binding fails. This case is important for breaking |
| 2520 | // recursion, since TryImplicitConversion below will attempt to |
| 2521 | // create a temporary through the use of a copy constructor. |
| 2522 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 2523 | (T1->isRecordType() || T2->isRecordType())) |
| 2524 | return ICS; |
| 2525 | |
| 2526 | // C++ [over.ics.ref]p2: |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2527 | // When a parameter of reference type is not bound directly to |
| 2528 | // an argument expression, the conversion sequence is the one |
| 2529 | // required to convert the argument expression to the |
| 2530 | // underlying type of the reference according to |
| 2531 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 2532 | // to copy-initializing a temporary of the underlying type with |
| 2533 | // the argument expression. Any difference in top-level |
| 2534 | // cv-qualification is subsumed by the initialization itself |
| 2535 | // and does not constitute a conversion. |
| 2536 | ICS = S.TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 2537 | /*AllowExplicit=*/false, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2538 | /*InOverloadResolution=*/false); |
| 2539 | |
| 2540 | // Of course, that's still a reference binding. |
| 2541 | if (ICS.isStandard()) { |
| 2542 | ICS.Standard.ReferenceBinding = true; |
| 2543 | ICS.Standard.RRefBinding = isRValRef; |
| 2544 | } else if (ICS.isUserDefined()) { |
| 2545 | ICS.UserDefined.After.ReferenceBinding = true; |
| 2546 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 2547 | } |
| 2548 | return ICS; |
| 2549 | } |
| 2550 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2551 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2552 | /// ToType from the expression From. Return the implicit conversion |
| 2553 | /// sequence required to pass this argument, which may be a bad |
| 2554 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2555 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | 74e386e | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 2556 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2557 | static ImplicitConversionSequence |
| 2558 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | b7f9e6a | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 2559 | bool SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2560 | bool InOverloadResolution) { |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2561 | if (ToType->isReferenceType()) |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2562 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2563 | /*FIXME:*/From->getLocStart(), |
| 2564 | SuppressUserConversions, |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2565 | /*AllowExplicit=*/false); |
Douglas Gregor | abe183d | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2567 | return S.TryImplicitConversion(From, ToType, |
| 2568 | SuppressUserConversions, |
| 2569 | /*AllowExplicit=*/false, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2570 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2573 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2574 | /// parameter of the given member function (@c Method) from the |
| 2575 | /// expression @p From. |
| 2576 | ImplicitConversionSequence |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2577 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2578 | CXXMethodDecl *Method, |
| 2579 | CXXRecordDecl *ActingContext) { |
| 2580 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2581 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2582 | // const volatile object. |
| 2583 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2584 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2585 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2586 | |
| 2587 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2588 | // to exit early. |
| 2589 | ImplicitConversionSequence ICS; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2590 | |
| 2591 | // We need to have an object of class type. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2592 | QualType FromType = OrigFromType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2593 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2594 | FromType = PT->getPointeeType(); |
| 2595 | |
| 2596 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2597 | |
Sebastian Redl | 65bdbfa | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2598 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2599 | // where X is the class of which the function is a member |
| 2600 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2601 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2603 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2604 | // reference binding here, that allows class rvalues to bind to |
| 2605 | // non-constant references. |
| 2606 | |
| 2607 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2608 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2609 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2610 | if (ImplicitParamType.getCVRQualifiers() |
| 2611 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2612 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2613 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2614 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2615 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2616 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2617 | |
| 2618 | // Check that we have either the same type or a derived type. It |
| 2619 | // affects the conversion rank. |
| 2620 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2621 | ImplicitConversionKind SecondKind; |
| 2622 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2623 | SecondKind = ICK_Identity; |
| 2624 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2625 | SecondKind = ICK_Derived_To_Base; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2626 | else { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2627 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2628 | FromType, ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2629 | return ICS; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2630 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Success. Mark this as a reference binding. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2633 | ICS.setStandard(); |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2634 | ICS.Standard.setAsIdentityConversion(); |
| 2635 | ICS.Standard.Second = SecondKind; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2636 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2637 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2638 | ICS.Standard.ReferenceBinding = true; |
| 2639 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2640 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2641 | return ICS; |
| 2642 | } |
| 2643 | |
| 2644 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2645 | /// the implicit object parameter for the given Method with the given |
| 2646 | /// expression. |
| 2647 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2648 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 2649 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2650 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2651 | CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2652 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2654 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2656 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2657 | FromRecordType = PT->getPointeeType(); |
| 2658 | DestType = Method->getThisType(Context); |
| 2659 | } else { |
| 2660 | FromRecordType = From->getType(); |
| 2661 | DestType = ImplicitParamRecordType; |
| 2662 | } |
| 2663 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2664 | // Note that we always use the true parent context when performing |
| 2665 | // the actual argument initialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2666 | ImplicitConversionSequence ICS |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2667 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2668 | Method->getParent()); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2669 | if (ICS.isBad()) |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2670 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2671 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2672 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2674 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2675 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2677 | if (!Context.hasSameType(From->getType(), DestType)) |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame^] | 2678 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, /*InheritancePath=*/0, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2679 | /*isLvalue=*/!From->getType()->getAs<PointerType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2680 | return false; |
| 2681 | } |
| 2682 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2683 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2684 | /// expression From to bool (C++0x [conv]p3). |
| 2685 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2687 | // FIXME: Are these flags correct? |
| 2688 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2690 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2694 | /// of the expression From to bool (C++0x [conv]p3). |
| 2695 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2696 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2697 | if (!ICS.isBad()) |
| 2698 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2699 | |
Fariborz Jahanian | cc5306a | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2700 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | 17c7a5d | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2701 | return Diag(From->getSourceRange().getBegin(), |
| 2702 | diag::err_typecheck_bool_condition) |
| 2703 | << From->getType() << From->getSourceRange(); |
| 2704 | return true; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2707 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2708 | /// candidate functions, using the given function call arguments. If |
| 2709 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2710 | /// conversions via constructors or conversion operators. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2711 | /// |
| 2712 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2713 | /// based on an incomplete set of function arguments. This feature is used by |
| 2714 | /// code completion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | void |
| 2716 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2717 | DeclAccessPair FoundDecl, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2718 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2719 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2720 | bool SuppressUserConversions, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2721 | bool PartialOverloading) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2723 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2724 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2726 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2728 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2729 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2730 | // If we get here, it's because we're calling a member function |
| 2731 | // that is named without a member access expression (e.g., |
| 2732 | // "this->f") that was either written explicitly or created |
| 2733 | // implicitly. This can happen with a qualified call to a member |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2734 | // function, e.g., X::f(). We use an empty type for the implied |
| 2735 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2736 | // is irrelevant. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2737 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2738 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2739 | SuppressUserConversions); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
| 2742 | // We treat a constructor like a non-member function, since its object |
| 2743 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2746 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2747 | return; |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2749 | // Overload resolution is always an unevaluated context. |
| 2750 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2751 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2752 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2753 | // C++ [class.copy]p3: |
| 2754 | // A member function template is never instantiated to perform the copy |
| 2755 | // of a class object to an object of its class type. |
| 2756 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2757 | if (NumArgs == 1 && |
| 2758 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 1211606 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 2759 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 2760 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2761 | return; |
| 2762 | } |
| 2763 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2764 | // Add this candidate |
| 2765 | CandidateSet.push_back(OverloadCandidate()); |
| 2766 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2767 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2768 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2769 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2770 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2771 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2772 | |
| 2773 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2774 | |
| 2775 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2776 | // parameters is viable only if it has an ellipsis in its parameter |
| 2777 | // list (8.3.5). |
Douglas Gregor | 5bd1a11 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2778 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2779 | !Proto->isVariadic()) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2780 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2781 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2782 | return; |
| 2783 | } |
| 2784 | |
| 2785 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2786 | // is viable only if the (m+1)st parameter has a default argument |
| 2787 | // (8.3.6). For the purposes of overload resolution, the |
| 2788 | // parameter list is truncated on the right, so that there are |
| 2789 | // exactly m parameters. |
| 2790 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2791 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2792 | // Not enough arguments. |
| 2793 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2794 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2795 | return; |
| 2796 | } |
| 2797 | |
| 2798 | // Determine the implicit conversion sequences for each of the |
| 2799 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2800 | Candidate.Conversions.resize(NumArgs); |
| 2801 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2802 | if (ArgIdx < NumArgsInProto) { |
| 2803 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2804 | // exist for each argument an implicit conversion sequence |
| 2805 | // (13.3.3.1) that converts that argument to the corresponding |
| 2806 | // parameter of F. |
| 2807 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2809 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 2810 | SuppressUserConversions, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2811 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2812 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2813 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2814 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2815 | break; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2816 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2817 | } else { |
| 2818 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2819 | // argument for which there is no corresponding parameter is |
| 2820 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2821 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2826 | /// \brief Add all of the function declarations in the given function set to |
| 2827 | /// the overload canddiate set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2828 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2829 | Expr **Args, unsigned NumArgs, |
| 2830 | OverloadCandidateSet& CandidateSet, |
| 2831 | bool SuppressUserConversions) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2832 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2833 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 2834 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2835 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2836 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2837 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2838 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2839 | CandidateSet, SuppressUserConversions); |
| 2840 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2841 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2842 | SuppressUserConversions); |
| 2843 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2844 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2845 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2846 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2847 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2848 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2849 | /*FIXME: explicit args */ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2850 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2851 | CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2852 | SuppressUserConversions); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2853 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2854 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2855 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2856 | Args, NumArgs, CandidateSet, |
| 2857 | SuppressUserConversions); |
| 2858 | } |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2859 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2862 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2863 | /// method) as a method candidate to the given overload set. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2864 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2865 | QualType ObjectType, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2866 | Expr **Args, unsigned NumArgs, |
| 2867 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2868 | bool SuppressUserConversions) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2869 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2870 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2871 | |
| 2872 | if (isa<UsingShadowDecl>(Decl)) |
| 2873 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2874 | |
| 2875 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2876 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2877 | "Expected a member function template"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2878 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 2879 | /*ExplicitArgs*/ 0, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2880 | ObjectType, Args, NumArgs, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2881 | CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2882 | SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2883 | } else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2884 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2885 | ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2886 | CandidateSet, SuppressUserConversions); |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2890 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2891 | /// of candidate functions, using the given function call arguments |
| 2892 | /// and the object argument (@c Object). For example, in a call |
| 2893 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2894 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2895 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2896 | /// operators. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2897 | void |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2898 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2899 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 2900 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2901 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2902 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2903 | const FunctionProtoType* Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2904 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2905 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2906 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2907 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2909 | if (!CandidateSet.isNewCandidate(Method)) |
| 2910 | return; |
| 2911 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2912 | // Overload resolution is always an unevaluated context. |
| 2913 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2914 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2915 | // Add this candidate |
| 2916 | CandidateSet.push_back(OverloadCandidate()); |
| 2917 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2918 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2919 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2920 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2921 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2922 | |
| 2923 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2924 | |
| 2925 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2926 | // parameters is viable only if it has an ellipsis in its parameter |
| 2927 | // list (8.3.5). |
| 2928 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2929 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2930 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2931 | return; |
| 2932 | } |
| 2933 | |
| 2934 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2935 | // is viable only if the (m+1)st parameter has a default argument |
| 2936 | // (8.3.6). For the purposes of overload resolution, the |
| 2937 | // parameter list is truncated on the right, so that there are |
| 2938 | // exactly m parameters. |
| 2939 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2940 | if (NumArgs < MinRequiredArgs) { |
| 2941 | // Not enough arguments. |
| 2942 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2943 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2944 | return; |
| 2945 | } |
| 2946 | |
| 2947 | Candidate.Viable = true; |
| 2948 | Candidate.Conversions.resize(NumArgs + 1); |
| 2949 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2950 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2951 | // The implicit object argument is ignored. |
| 2952 | Candidate.IgnoreObjectArgument = true; |
| 2953 | else { |
| 2954 | // Determine the implicit conversion sequence for the object |
| 2955 | // parameter. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2956 | Candidate.Conversions[0] |
| 2957 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2958 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2959 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2960 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2961 | return; |
| 2962 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | // Determine the implicit conversion sequences for each of the |
| 2966 | // arguments. |
| 2967 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2968 | if (ArgIdx < NumArgsInProto) { |
| 2969 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2970 | // exist for each argument an implicit conversion sequence |
| 2971 | // (13.3.3.1) that converts that argument to the corresponding |
| 2972 | // parameter of F. |
| 2973 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2975 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2976 | SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2977 | /*InOverloadResolution=*/true); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2978 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2979 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2980 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2981 | break; |
| 2982 | } |
| 2983 | } else { |
| 2984 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2985 | // argument for which there is no corresponding parameter is |
| 2986 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2987 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2992 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2993 | /// set, using template argument deduction to produce an appropriate member |
| 2994 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2996 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2997 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2998 | CXXRecordDecl *ActingContext, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2999 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3000 | QualType ObjectType, |
| 3001 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3002 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3003 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3004 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3005 | return; |
| 3006 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3007 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3009 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | // 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] | 3011 | // candidate functions in the usual way.113) A given name can refer to one |
| 3012 | // or more function templates and also to a set of overloaded non-template |
| 3013 | // functions. In such a case, the candidate functions generated from each |
| 3014 | // function template are combined with the set of non-template candidate |
| 3015 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3016 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3017 | FunctionDecl *Specialization = 0; |
| 3018 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3019 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3020 | Args, NumArgs, Specialization, Info)) { |
| 3021 | // FIXME: Record what happened with template argument deduction, so |
| 3022 | // that we can give the user a beautiful diagnostic. |
| 3023 | (void)Result; |
| 3024 | return; |
| 3025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3027 | // Add the function template specialization produced by template argument |
| 3028 | // deduction as a candidate. |
| 3029 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3030 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3031 | "Specialization is not a member function?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3032 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3033 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3034 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3037 | /// \brief Add a C++ function template specialization as a candidate |
| 3038 | /// in the candidate set, using template argument deduction to produce |
| 3039 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3041 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3042 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3043 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3044 | Expr **Args, unsigned NumArgs, |
| 3045 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3046 | bool SuppressUserConversions) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3047 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3048 | return; |
| 3049 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3050 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3052 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | // 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] | 3054 | // candidate functions in the usual way.113) A given name can refer to one |
| 3055 | // or more function templates and also to a set of overloaded non-template |
| 3056 | // functions. In such a case, the candidate functions generated from each |
| 3057 | // function template are combined with the set of non-template candidate |
| 3058 | // functions. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3059 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3060 | FunctionDecl *Specialization = 0; |
| 3061 | if (TemplateDeductionResult Result |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3062 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3063 | Args, NumArgs, Specialization, Info)) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3064 | CandidateSet.push_back(OverloadCandidate()); |
| 3065 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3066 | Candidate.FoundDecl = FoundDecl; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3067 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3068 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3069 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3070 | Candidate.IsSurrogate = false; |
| 3071 | Candidate.IgnoreObjectArgument = false; |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 3072 | |
| 3073 | // TODO: record more information about failed template arguments |
| 3074 | Candidate.DeductionFailure.Result = Result; |
| 3075 | Candidate.DeductionFailure.TemplateParameter = Info.Param.getOpaqueValue(); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3076 | return; |
| 3077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3079 | // Add the function template specialization produced by template argument |
| 3080 | // deduction as a candidate. |
| 3081 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3082 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | 7ec7752 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3083 | SuppressUserConversions); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3086 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3088 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | /// 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] | 3090 | /// (which may or may not be the same type as the type that the |
| 3091 | /// conversion function produces). |
| 3092 | void |
| 3093 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3094 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3095 | CXXRecordDecl *ActingContext, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3096 | Expr *From, QualType ToType, |
| 3097 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3098 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3099 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3100 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3101 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3102 | return; |
| 3103 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3104 | // Overload resolution is always an unevaluated context. |
| 3105 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3106 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3107 | // Add this candidate |
| 3108 | CandidateSet.push_back(OverloadCandidate()); |
| 3109 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3110 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3111 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3112 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3113 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3114 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3115 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3116 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3117 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3118 | // Determine the implicit conversion sequence for the implicit |
| 3119 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3120 | Candidate.Viable = true; |
| 3121 | Candidate.Conversions.resize(1); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3122 | Candidate.Conversions[0] |
| 3123 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 3124 | ActingContext); |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3125 | // Conversion functions to a different type in the base class is visible in |
| 3126 | // the derived class. So, a derived to base conversion should not participate |
| 3127 | // in overload resolution. |
| 3128 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 3129 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3130 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3131 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3132 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3133 | return; |
| 3134 | } |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3135 | |
| 3136 | // We won't go through a user-define type conversion function to convert a |
| 3137 | // derived to base as such conversions are given Conversion Rank. They only |
| 3138 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3139 | QualType FromCanon |
| 3140 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3141 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3142 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3143 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3144 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 3759a03 | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3145 | return; |
| 3146 | } |
| 3147 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3148 | // To determine what the conversion from the result of calling the |
| 3149 | // conversion function to the type we're eventually trying to |
| 3150 | // convert to (ToType), we need to synthesize a call to the |
| 3151 | // conversion function and attempt copy initialization from it. This |
| 3152 | // makes sure that we get the right semantics with respect to |
| 3153 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3154 | // call on the stack and we don't need its arguments to be |
| 3155 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3156 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3157 | From->getLocStart()); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3158 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3159 | CastExpr::CK_FunctionToPointerDecay, |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame^] | 3160 | &ConversionRef, /*InheritancePath=*/0, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
| 3162 | // 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] | 3163 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3164 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3166 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | 0a0d1ac | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3167 | From->getLocStart()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | ImplicitConversionSequence ICS = |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3169 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3170 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3171 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3173 | switch (ICS.getKind()) { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3174 | case ImplicitConversionSequence::StandardConversion: |
| 3175 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3176 | |
| 3177 | // C++ [over.ics.user]p3: |
| 3178 | // If the user-defined conversion is specified by a specialization of a |
| 3179 | // conversion function template, the second standard conversion sequence |
| 3180 | // shall have exact match rank. |
| 3181 | if (Conversion->getPrimaryTemplate() && |
| 3182 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3183 | Candidate.Viable = false; |
| 3184 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3185 | } |
| 3186 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3187 | break; |
| 3188 | |
| 3189 | case ImplicitConversionSequence::BadConversion: |
| 3190 | Candidate.Viable = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3191 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3192 | break; |
| 3193 | |
| 3194 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3195 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3196 | "Can only end up with a standard conversion sequence or failure"); |
| 3197 | } |
| 3198 | } |
| 3199 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3200 | /// \brief Adds a conversion function template specialization |
| 3201 | /// candidate to the overload set, using template argument deduction |
| 3202 | /// to deduce the template arguments of the conversion function |
| 3203 | /// template from the type that we are converting to (C++ |
| 3204 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3206 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3207 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3208 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3209 | Expr *From, QualType ToType, |
| 3210 | OverloadCandidateSet &CandidateSet) { |
| 3211 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3212 | "Only conversion function templates permitted here"); |
| 3213 | |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3214 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3215 | return; |
| 3216 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3217 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3218 | CXXConversionDecl *Specialization = 0; |
| 3219 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3221 | Specialization, Info)) { |
| 3222 | // FIXME: Record what happened with template argument deduction, so |
| 3223 | // that we can give the user a beautiful diagnostic. |
| 3224 | (void)Result; |
| 3225 | return; |
| 3226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3228 | // Add the conversion function template specialization produced by |
| 3229 | // template argument deduction as a candidate. |
| 3230 | assert(Specialization && "Missing function template specialization?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3231 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3232 | CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3235 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3236 | /// converts the given @c Object to a function pointer via the |
| 3237 | /// conversion function @c Conversion, and then attempts to call it |
| 3238 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3239 | /// the type of function that we'll eventually be calling. |
| 3240 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3241 | DeclAccessPair FoundDecl, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3242 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3243 | const FunctionProtoType *Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3244 | QualType ObjectType, |
| 3245 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3246 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3f39602 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3247 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3248 | return; |
| 3249 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3250 | // Overload resolution is always an unevaluated context. |
| 3251 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3252 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3253 | CandidateSet.push_back(OverloadCandidate()); |
| 3254 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3255 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3256 | Candidate.Function = 0; |
| 3257 | Candidate.Surrogate = Conversion; |
| 3258 | Candidate.Viable = true; |
| 3259 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3260 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3261 | Candidate.Conversions.resize(NumArgs + 1); |
| 3262 | |
| 3263 | // Determine the implicit conversion sequence for the implicit |
| 3264 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | ImplicitConversionSequence ObjectInit |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3266 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3267 | if (ObjectInit.isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3268 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3269 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3270 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3271 | return; |
| 3272 | } |
| 3273 | |
| 3274 | // The first conversion is actually a user-defined conversion whose |
| 3275 | // first conversion is ObjectInit's standard conversion (which is |
| 3276 | // effectively a reference binding). Record it as such. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3277 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3278 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3279 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3280 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3282 | = Candidate.Conversions[0].UserDefined.Before; |
| 3283 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3284 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3286 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3287 | |
| 3288 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3289 | // parameters is viable only if it has an ellipsis in its parameter |
| 3290 | // list (8.3.5). |
| 3291 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3292 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3293 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3294 | return; |
| 3295 | } |
| 3296 | |
| 3297 | // Function types don't have any default arguments, so just check if |
| 3298 | // we have enough arguments. |
| 3299 | if (NumArgs < NumArgsInProto) { |
| 3300 | // Not enough arguments. |
| 3301 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3302 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3303 | return; |
| 3304 | } |
| 3305 | |
| 3306 | // Determine the implicit conversion sequences for each of the |
| 3307 | // arguments. |
| 3308 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3309 | if (ArgIdx < NumArgsInProto) { |
| 3310 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3311 | // exist for each argument an implicit conversion sequence |
| 3312 | // (13.3.3.1) that converts that argument to the corresponding |
| 3313 | // parameter of F. |
| 3314 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3316 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3317 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3318 | /*InOverloadResolution=*/false); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3319 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3320 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3321 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3322 | break; |
| 3323 | } |
| 3324 | } else { |
| 3325 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3326 | // argument for which there is no corresponding parameter is |
| 3327 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3328 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | } |
| 3332 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3333 | /// \brief Add overload candidates for overloaded operators that are |
| 3334 | /// member functions. |
| 3335 | /// |
| 3336 | /// Add the overloaded operator candidates that are member functions |
| 3337 | /// for the operator Op that was used in an operator expression such |
| 3338 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3339 | /// CandidateSet will store the added overload candidates. (C++ |
| 3340 | /// [over.match.oper]). |
| 3341 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3342 | SourceLocation OpLoc, |
| 3343 | Expr **Args, unsigned NumArgs, |
| 3344 | OverloadCandidateSet& CandidateSet, |
| 3345 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3346 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3347 | |
| 3348 | // C++ [over.match.oper]p3: |
| 3349 | // For a unary operator @ with an operand of a type whose |
| 3350 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3351 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3352 | // a right operand of a type whose cv-unqualified version is T2, |
| 3353 | // three sets of candidate functions, designated member |
| 3354 | // candidates, non-member candidates and built-in candidates, are |
| 3355 | // constructed as follows: |
| 3356 | QualType T1 = Args[0]->getType(); |
| 3357 | QualType T2; |
| 3358 | if (NumArgs > 1) |
| 3359 | T2 = Args[1]->getType(); |
| 3360 | |
| 3361 | // -- If T1 is a class type, the set of member candidates is the |
| 3362 | // result of the qualified lookup of T1::operator@ |
| 3363 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3364 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3365 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3366 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3367 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3368 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3370 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3371 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3372 | Operators.suppressDiagnostics(); |
| 3373 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3375 | OperEnd = Operators.end(); |
| 3376 | Oper != OperEnd; |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3377 | ++Oper) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3378 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3379 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3380 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3381 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3384 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3385 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3386 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3387 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3388 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3389 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3390 | /// (at the beginning of the argument list) that will be contextually |
| 3391 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3393 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3394 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3395 | bool IsAssignmentOperator, |
| 3396 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3397 | // Overload resolution is always an unevaluated context. |
| 3398 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3399 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3400 | // Add this candidate |
| 3401 | CandidateSet.push_back(OverloadCandidate()); |
| 3402 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3403 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3404 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3405 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3406 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3407 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3408 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3409 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3410 | |
| 3411 | // Determine the implicit conversion sequences for each of the |
| 3412 | // arguments. |
| 3413 | Candidate.Viable = true; |
| 3414 | Candidate.Conversions.resize(NumArgs); |
| 3415 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3416 | // C++ [over.match.oper]p4: |
| 3417 | // For the built-in assignment operators, conversions of the |
| 3418 | // left operand are restricted as follows: |
| 3419 | // -- no temporaries are introduced to hold the left operand, and |
| 3420 | // -- no user-defined conversions are applied to the left |
| 3421 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3423 | // |
| 3424 | // We block these conversions by turning off user-defined |
| 3425 | // conversions, since that is the only way that initialization of |
| 3426 | // a reference to a non-class type can occur from something that |
| 3427 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3428 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3430 | "Contextual conversion to bool requires bool type"); |
| 3431 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3432 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3434 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3435 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3436 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3437 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3438 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3439 | Candidate.Viable = false; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3440 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3441 | break; |
| 3442 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3447 | /// candidate operator functions for built-in operators (C++ |
| 3448 | /// [over.built]). The types are separated into pointer types and |
| 3449 | /// enumeration types. |
| 3450 | class BuiltinCandidateTypeSet { |
| 3451 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3452 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3453 | |
| 3454 | /// PointerTypes - The set of pointer types that will be used in the |
| 3455 | /// built-in candidates. |
| 3456 | TypeSet PointerTypes; |
| 3457 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3458 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3459 | /// used in the built-in candidates. |
| 3460 | TypeSet MemberPointerTypes; |
| 3461 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3462 | /// EnumerationTypes - The set of enumeration types that will be |
| 3463 | /// used in the built-in candidates. |
| 3464 | TypeSet EnumerationTypes; |
| 3465 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3466 | /// Sema - The semantic analysis instance where we are building the |
| 3467 | /// candidate type set. |
| 3468 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3470 | /// Context - The AST context in which we will build the type sets. |
| 3471 | ASTContext &Context; |
| 3472 | |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3473 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3474 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3475 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3476 | |
| 3477 | public: |
| 3478 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3479 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3480 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3482 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3483 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3484 | void AddTypesConvertedFrom(QualType Ty, |
| 3485 | SourceLocation Loc, |
| 3486 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3487 | bool AllowExplicitConversions, |
| 3488 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3489 | |
| 3490 | /// pointer_begin - First pointer type found; |
| 3491 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3492 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3493 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3494 | iterator pointer_end() { return PointerTypes.end(); } |
| 3495 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3496 | /// member_pointer_begin - First member pointer type found; |
| 3497 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3498 | |
| 3499 | /// member_pointer_end - Past the last member pointer type found; |
| 3500 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3501 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3502 | /// enumeration_begin - First enumeration type found; |
| 3503 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3504 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3505 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3506 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3507 | }; |
| 3508 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3509 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3510 | /// the set of pointer types along with any more-qualified variants of |
| 3511 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3512 | /// will add "int const *", "int const volatile *", "int const |
| 3513 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3514 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3515 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3516 | /// |
| 3517 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3518 | bool |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3519 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3520 | const Qualifiers &VisibleQuals) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3521 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3522 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3523 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3524 | return false; |
| 3525 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3526 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3527 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3528 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3529 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3530 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3531 | // (the qualifier would sink to the element type), and for another, the |
| 3532 | // only overload situation where it matters is subscript or pointer +- int, |
| 3533 | // and those shouldn't have qualifier variants anyway. |
| 3534 | if (PointeeTy->isArrayType()) |
| 3535 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3536 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3537 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | d411b3f | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3538 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3539 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3540 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3541 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3542 | // Iterate through all strict supersets of BaseCVR. |
| 3543 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3544 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3545 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3546 | // in the types. |
| 3547 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3548 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3549 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3550 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | return true; |
| 3554 | } |
| 3555 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3556 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3557 | /// to the set of pointer types along with any more-qualified variants of |
| 3558 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3559 | /// will add "int const *", "int const volatile *", "int const |
| 3560 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3561 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3562 | /// false otherwise. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3563 | /// |
| 3564 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3565 | bool |
| 3566 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3567 | QualType Ty) { |
| 3568 | // Insert this type. |
| 3569 | if (!MemberPointerTypes.insert(Ty)) |
| 3570 | return false; |
| 3571 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3572 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3573 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3574 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3575 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3576 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3577 | // (the qualifier would sink to the element type), and for another, the |
| 3578 | // only overload situation where it matters is subscript or pointer +- int, |
| 3579 | // and those shouldn't have qualifier variants anyway. |
| 3580 | if (PointeeTy->isArrayType()) |
| 3581 | return true; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3582 | const Type *ClassTy = PointerTy->getClass(); |
| 3583 | |
| 3584 | // Iterate through all strict supersets of the pointee type's CVR |
| 3585 | // qualifiers. |
| 3586 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3587 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3588 | if ((CVR | BaseCVR) != CVR) continue; |
| 3589 | |
| 3590 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3591 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | return true; |
| 3595 | } |
| 3596 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3597 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3598 | /// 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] | 3599 | /// primarily interested in pointer types and enumeration types. We also |
| 3600 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3601 | /// AllowUserConversions is true if we should look at the conversion |
| 3602 | /// functions of a class type, and AllowExplicitConversions if we |
| 3603 | /// should also include the explicit conversion functions of a class |
| 3604 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3606 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3607 | SourceLocation Loc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3608 | bool AllowUserConversions, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3609 | bool AllowExplicitConversions, |
| 3610 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3611 | // Only deal with canonical types. |
| 3612 | Ty = Context.getCanonicalType(Ty); |
| 3613 | |
| 3614 | // Look through reference types; they aren't part of the type of an |
| 3615 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3616 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3617 | Ty = RefTy->getPointeeType(); |
| 3618 | |
| 3619 | // We don't care about qualifiers on the type. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3620 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3621 | |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3622 | // If we're dealing with an array type, decay to the pointer. |
| 3623 | if (Ty->isArrayType()) |
| 3624 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3625 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3626 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3627 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3628 | |
| 3629 | // Insert our type, and its more-qualified variants, into the set |
| 3630 | // of types. |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3631 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3632 | return; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3633 | } else if (Ty->isMemberPointerType()) { |
| 3634 | // Member pointers are far easier, since the pointee can't be converted. |
| 3635 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3636 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3637 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3638 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3639 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3640 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3641 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3642 | // No conversion functions in incomplete types. |
| 3643 | return; |
| 3644 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3646 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3647 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ca4fb04 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3648 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3649 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3650 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3651 | NamedDecl *D = I.getDecl(); |
| 3652 | if (isa<UsingShadowDecl>(D)) |
| 3653 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3655 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3656 | // about which builtin types we can convert to. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3657 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3658 | continue; |
| 3659 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3660 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3661 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3662 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3663 | VisibleQuals); |
| 3664 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3670 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3671 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3672 | /// given type to the candidate set. |
| 3673 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3674 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3676 | unsigned NumArgs, |
| 3677 | OverloadCandidateSet &CandidateSet) { |
| 3678 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3680 | // T& operator=(T&, T) |
| 3681 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3682 | ParamTypes[1] = T; |
| 3683 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3684 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3686 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3687 | // volatile T& operator=(volatile T&, T) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3688 | ParamTypes[0] |
| 3689 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3690 | ParamTypes[1] = T; |
| 3691 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3693 | } |
| 3694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3695 | |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3696 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3697 | /// 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] | 3698 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3699 | Qualifiers VRQuals; |
| 3700 | const RecordType *TyRec; |
| 3701 | if (const MemberPointerType *RHSMPType = |
| 3702 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3703 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3704 | else |
| 3705 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3706 | if (!TyRec) { |
Fariborz Jahanian | 1cad602 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3707 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3708 | VRQuals.addVolatile(); |
| 3709 | VRQuals.addRestrict(); |
| 3710 | return VRQuals; |
| 3711 | } |
| 3712 | |
| 3713 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3714 | if (!ClassDecl->hasDefinition()) |
| 3715 | return VRQuals; |
| 3716 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3717 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3718 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3719 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3720 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3721 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3722 | NamedDecl *D = I.getDecl(); |
| 3723 | if (isa<UsingShadowDecl>(D)) |
| 3724 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3725 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3726 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3727 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3728 | CanTy = ResTypeRef->getPointeeType(); |
| 3729 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3730 | // as see them. |
| 3731 | bool done = false; |
| 3732 | while (!done) { |
| 3733 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3734 | CanTy = ResTypePtr->getPointeeType(); |
| 3735 | else if (const MemberPointerType *ResTypeMPtr = |
| 3736 | CanTy->getAs<MemberPointerType>()) |
| 3737 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3738 | else |
| 3739 | done = true; |
| 3740 | if (CanTy.isVolatileQualified()) |
| 3741 | VRQuals.addVolatile(); |
| 3742 | if (CanTy.isRestrictQualified()) |
| 3743 | VRQuals.addRestrict(); |
| 3744 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3745 | return VRQuals; |
| 3746 | } |
| 3747 | } |
| 3748 | } |
| 3749 | return VRQuals; |
| 3750 | } |
| 3751 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3752 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3753 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3754 | /// on the operator @p Op and the arguments given. For example, if the |
| 3755 | /// operator is a binary '+', this routine might add "int |
| 3756 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3757 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3759 | SourceLocation OpLoc, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3760 | Expr **Args, unsigned NumArgs, |
| 3761 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3762 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3763 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3764 | // that the first few of these types are the promoted integral |
| 3765 | // types; these types need to be first. |
| 3766 | // FIXME: What about complex? |
| 3767 | const unsigned FirstIntegralType = 0; |
| 3768 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3770 | LastPromotedIntegralType = 13; |
| 3771 | const unsigned FirstPromotedArithmeticType = 7, |
| 3772 | LastPromotedArithmeticType = 16; |
| 3773 | const unsigned NumArithmeticTypes = 16; |
| 3774 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3776 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3777 | Context.SignedCharTy, Context.ShortTy, |
| 3778 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3779 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3780 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3781 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3782 | }; |
Douglas Gregor | 652371a | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3783 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3784 | "Invalid first promoted integral type"); |
| 3785 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3786 | == Context.UnsignedLongLongTy && |
| 3787 | "Invalid last promoted integral type"); |
| 3788 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3789 | "Invalid first promoted arithmetic type"); |
| 3790 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3791 | == Context.LongDoubleTy && |
| 3792 | "Invalid last promoted arithmetic type"); |
| 3793 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3794 | // Find all of the types that the arguments can convert to, but only |
| 3795 | // if the operator we're looking at has built-in operator candidates |
| 3796 | // that make use of these types. |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3797 | Qualifiers VisibleTypeConversionsQuals; |
| 3798 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3799 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3800 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3801 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3802 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3803 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3804 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3805 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3806 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3807 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3808 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3809 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3810 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3811 | OpLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3812 | true, |
| 3813 | (Op == OO_Exclaim || |
| 3814 | Op == OO_AmpAmp || |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3815 | Op == OO_PipePipe), |
| 3816 | VisibleTypeConversionsQuals); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | bool isComparison = false; |
| 3820 | switch (Op) { |
| 3821 | case OO_None: |
| 3822 | case NUM_OVERLOADED_OPERATORS: |
| 3823 | assert(false && "Expected an overloaded operator"); |
| 3824 | break; |
| 3825 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3826 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3828 | goto UnaryStar; |
| 3829 | else |
| 3830 | goto BinaryStar; |
| 3831 | break; |
| 3832 | |
| 3833 | case OO_Plus: // '+' is either unary or binary |
| 3834 | if (NumArgs == 1) |
| 3835 | goto UnaryPlus; |
| 3836 | else |
| 3837 | goto BinaryPlus; |
| 3838 | break; |
| 3839 | |
| 3840 | case OO_Minus: // '-' is either unary or binary |
| 3841 | if (NumArgs == 1) |
| 3842 | goto UnaryMinus; |
| 3843 | else |
| 3844 | goto BinaryMinus; |
| 3845 | break; |
| 3846 | |
| 3847 | case OO_Amp: // '&' is either unary or binary |
| 3848 | if (NumArgs == 1) |
| 3849 | goto UnaryAmp; |
| 3850 | else |
| 3851 | goto BinaryAmp; |
| 3852 | |
| 3853 | case OO_PlusPlus: |
| 3854 | case OO_MinusMinus: |
| 3855 | // C++ [over.built]p3: |
| 3856 | // |
| 3857 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3858 | // is either volatile or empty, there exist candidate operator |
| 3859 | // functions of the form |
| 3860 | // |
| 3861 | // VQ T& operator++(VQ T&); |
| 3862 | // T operator++(VQ T&, int); |
| 3863 | // |
| 3864 | // C++ [over.built]p4: |
| 3865 | // |
| 3866 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3867 | // than bool, and VQ is either volatile or empty, there exist |
| 3868 | // candidate operator functions of the form |
| 3869 | // |
| 3870 | // VQ T& operator--(VQ T&); |
| 3871 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3873 | Arith < NumArithmeticTypes; ++Arith) { |
| 3874 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3876 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3877 | |
| 3878 | // Non-volatile version. |
| 3879 | if (NumArgs == 1) |
| 3880 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3881 | else |
| 3882 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3883 | // heuristic to reduce number of builtin candidates in the set. |
| 3884 | // Add volatile version only if there are conversions to a volatile type. |
| 3885 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3886 | // Volatile version |
| 3887 | ParamTypes[0] |
| 3888 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3889 | if (NumArgs == 1) |
| 3890 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3891 | else |
| 3892 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3893 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | // C++ [over.built]p5: |
| 3897 | // |
| 3898 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3899 | // cv-unqualified object type, and VQ is either volatile or |
| 3900 | // empty, there exist candidate operator functions of the form |
| 3901 | // |
| 3902 | // T*VQ& operator++(T*VQ&); |
| 3903 | // T*VQ& operator--(T*VQ&); |
| 3904 | // T* operator++(T*VQ&, int); |
| 3905 | // T* operator--(T*VQ&, int); |
| 3906 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3907 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3908 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3909 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3910 | continue; |
| 3911 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3912 | QualType ParamTypes[2] = { |
| 3913 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3914 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3916 | // Without volatile |
| 3917 | if (NumArgs == 1) |
| 3918 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3919 | else |
| 3920 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3921 | |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3922 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3923 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3924 | // With volatile |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3925 | ParamTypes[0] |
| 3926 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3927 | if (NumArgs == 1) |
| 3928 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3929 | else |
| 3930 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3931 | } |
| 3932 | } |
| 3933 | break; |
| 3934 | |
| 3935 | UnaryStar: |
| 3936 | // C++ [over.built]p6: |
| 3937 | // For every cv-qualified or cv-unqualified object type T, there |
| 3938 | // exist candidate operator functions of the form |
| 3939 | // |
| 3940 | // T& operator*(T*); |
| 3941 | // |
| 3942 | // C++ [over.built]p7: |
| 3943 | // For every function type T, there exist candidate operator |
| 3944 | // functions of the form |
| 3945 | // T& operator*(T*); |
| 3946 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3947 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3948 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3949 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3951 | &ParamTy, Args, 1, CandidateSet); |
| 3952 | } |
| 3953 | break; |
| 3954 | |
| 3955 | UnaryPlus: |
| 3956 | // C++ [over.built]p8: |
| 3957 | // For every type T, there exist candidate operator functions of |
| 3958 | // the form |
| 3959 | // |
| 3960 | // T* operator+(T*); |
| 3961 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3962 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3963 | QualType ParamTy = *Ptr; |
| 3964 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3965 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3966 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3967 | // Fall through |
| 3968 | |
| 3969 | UnaryMinus: |
| 3970 | // C++ [over.built]p9: |
| 3971 | // For every promoted arithmetic type T, there exist candidate |
| 3972 | // operator functions of the form |
| 3973 | // |
| 3974 | // T operator+(T); |
| 3975 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3977 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3978 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3979 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3980 | } |
| 3981 | break; |
| 3982 | |
| 3983 | case OO_Tilde: |
| 3984 | // C++ [over.built]p10: |
| 3985 | // For every promoted integral type T, there exist candidate |
| 3986 | // operator functions of the form |
| 3987 | // |
| 3988 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3990 | Int < LastPromotedIntegralType; ++Int) { |
| 3991 | QualType IntTy = ArithmeticTypes[Int]; |
| 3992 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3993 | } |
| 3994 | break; |
| 3995 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3996 | case OO_New: |
| 3997 | case OO_Delete: |
| 3998 | case OO_Array_New: |
| 3999 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4000 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4001 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4002 | break; |
| 4003 | |
| 4004 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4005 | UnaryAmp: |
| 4006 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4007 | // C++ [over.match.oper]p3: |
| 4008 | // -- For the operator ',', the unary operator '&', or the |
| 4009 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4010 | break; |
| 4011 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4012 | case OO_EqualEqual: |
| 4013 | case OO_ExclaimEqual: |
| 4014 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4015 | // For every pointer to member type T, there exist candidate operator |
| 4016 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4017 | // |
| 4018 | // bool operator==(T,T); |
| 4019 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4020 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4021 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4022 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4023 | MemPtr != MemPtrEnd; |
| 4024 | ++MemPtr) { |
| 4025 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4026 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4027 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4029 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4031 | case OO_Less: |
| 4032 | case OO_Greater: |
| 4033 | case OO_LessEqual: |
| 4034 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4035 | // C++ [over.built]p15: |
| 4036 | // |
| 4037 | // For every pointer or enumeration type T, there exist |
| 4038 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4040 | // bool operator<(T, T); |
| 4041 | // bool operator>(T, T); |
| 4042 | // bool operator<=(T, T); |
| 4043 | // bool operator>=(T, T); |
| 4044 | // bool operator==(T, T); |
| 4045 | // bool operator!=(T, T); |
| 4046 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4047 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4048 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4049 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4050 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4052 | = CandidateTypes.enumeration_begin(); |
| 4053 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4054 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4055 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4056 | } |
| 4057 | |
| 4058 | // Fall through. |
| 4059 | isComparison = true; |
| 4060 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4061 | BinaryPlus: |
| 4062 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4063 | if (!isComparison) { |
| 4064 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4065 | |
| 4066 | // C++ [over.built]p13: |
| 4067 | // |
| 4068 | // For every cv-qualified or cv-unqualified object type T |
| 4069 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4071 | // T* operator+(T*, ptrdiff_t); |
| 4072 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4073 | // T* operator-(T*, ptrdiff_t); |
| 4074 | // T* operator+(ptrdiff_t, T*); |
| 4075 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4076 | // |
| 4077 | // C++ [over.built]p14: |
| 4078 | // |
| 4079 | // For every T, where T is a pointer to object type, there |
| 4080 | // exist candidate operator functions of the form |
| 4081 | // |
| 4082 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4084 | = CandidateTypes.pointer_begin(); |
| 4085 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4086 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4087 | |
| 4088 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4089 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4090 | |
| 4091 | if (Op == OO_Plus) { |
| 4092 | // T* operator+(ptrdiff_t, T*); |
| 4093 | ParamTypes[0] = ParamTypes[1]; |
| 4094 | ParamTypes[1] = *Ptr; |
| 4095 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4096 | } else { |
| 4097 | // ptrdiff_t operator-(T, T); |
| 4098 | ParamTypes[1] = *Ptr; |
| 4099 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4100 | Args, 2, CandidateSet); |
| 4101 | } |
| 4102 | } |
| 4103 | } |
| 4104 | // Fall through |
| 4105 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4106 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4107 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4108 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4109 | // C++ [over.built]p12: |
| 4110 | // |
| 4111 | // For every pair of promoted arithmetic types L and R, there |
| 4112 | // exist candidate operator functions of the form |
| 4113 | // |
| 4114 | // LR operator*(L, R); |
| 4115 | // LR operator/(L, R); |
| 4116 | // LR operator+(L, R); |
| 4117 | // LR operator-(L, R); |
| 4118 | // bool operator<(L, R); |
| 4119 | // bool operator>(L, R); |
| 4120 | // bool operator<=(L, R); |
| 4121 | // bool operator>=(L, R); |
| 4122 | // bool operator==(L, R); |
| 4123 | // bool operator!=(L, R); |
| 4124 | // |
| 4125 | // where LR is the result of the usual arithmetic conversions |
| 4126 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4127 | // |
| 4128 | // C++ [over.built]p24: |
| 4129 | // |
| 4130 | // For every pair of promoted arithmetic types L and R, there exist |
| 4131 | // candidate operator functions of the form |
| 4132 | // |
| 4133 | // LR operator?(bool, L, R); |
| 4134 | // |
| 4135 | // where LR is the result of the usual arithmetic conversions |
| 4136 | // between types L and R. |
| 4137 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4139 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4141 | Right < LastPromotedArithmeticType; ++Right) { |
| 4142 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4143 | QualType Result |
| 4144 | = isComparison |
| 4145 | ? Context.BoolTy |
| 4146 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4147 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4148 | } |
| 4149 | } |
| 4150 | break; |
| 4151 | |
| 4152 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4153 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4154 | case OO_Caret: |
| 4155 | case OO_Pipe: |
| 4156 | case OO_LessLess: |
| 4157 | case OO_GreaterGreater: |
| 4158 | // C++ [over.built]p17: |
| 4159 | // |
| 4160 | // For every pair of promoted integral types L and R, there |
| 4161 | // exist candidate operator functions of the form |
| 4162 | // |
| 4163 | // LR operator%(L, R); |
| 4164 | // LR operator&(L, R); |
| 4165 | // LR operator^(L, R); |
| 4166 | // LR operator|(L, R); |
| 4167 | // L operator<<(L, R); |
| 4168 | // L operator>>(L, R); |
| 4169 | // |
| 4170 | // where LR is the result of the usual arithmetic conversions |
| 4171 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4173 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4175 | Right < LastPromotedIntegralType; ++Right) { |
| 4176 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4177 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4178 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4179 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4180 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4181 | } |
| 4182 | } |
| 4183 | break; |
| 4184 | |
| 4185 | case OO_Equal: |
| 4186 | // C++ [over.built]p20: |
| 4187 | // |
| 4188 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4189 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4190 | // empty, there exist candidate operator functions of the form |
| 4191 | // |
| 4192 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4193 | for (BuiltinCandidateTypeSet::iterator |
| 4194 | Enum = CandidateTypes.enumeration_begin(), |
| 4195 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4196 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4198 | CandidateSet); |
| 4199 | for (BuiltinCandidateTypeSet::iterator |
| 4200 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4201 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4202 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4204 | CandidateSet); |
| 4205 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4206 | |
| 4207 | case OO_PlusEqual: |
| 4208 | case OO_MinusEqual: |
| 4209 | // C++ [over.built]p19: |
| 4210 | // |
| 4211 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4212 | // volatile or empty, there exist candidate operator functions |
| 4213 | // of the form |
| 4214 | // |
| 4215 | // T*VQ& operator=(T*VQ&, T*); |
| 4216 | // |
| 4217 | // C++ [over.built]p21: |
| 4218 | // |
| 4219 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4220 | // cv-unqualified object type and VQ is either volatile or |
| 4221 | // empty, there exist candidate operator functions of the form |
| 4222 | // |
| 4223 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4224 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4225 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4226 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4227 | QualType ParamTypes[2]; |
| 4228 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4229 | |
| 4230 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4231 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4232 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4233 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4234 | |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4235 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4236 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4237 | // volatile version |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4238 | ParamTypes[0] |
| 4239 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4240 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4241 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4242 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4243 | } |
| 4244 | // Fall through. |
| 4245 | |
| 4246 | case OO_StarEqual: |
| 4247 | case OO_SlashEqual: |
| 4248 | // C++ [over.built]p18: |
| 4249 | // |
| 4250 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 4251 | // VQ is either volatile or empty, and R is a promoted |
| 4252 | // arithmetic type, there exist candidate operator functions of |
| 4253 | // the form |
| 4254 | // |
| 4255 | // VQ L& operator=(VQ L&, R); |
| 4256 | // VQ L& operator*=(VQ L&, R); |
| 4257 | // VQ L& operator/=(VQ L&, R); |
| 4258 | // VQ L& operator+=(VQ L&, R); |
| 4259 | // VQ L& operator-=(VQ L&, R); |
| 4260 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4262 | Right < LastPromotedArithmeticType; ++Right) { |
| 4263 | QualType ParamTypes[2]; |
| 4264 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4265 | |
| 4266 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4267 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4268 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4269 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4270 | |
| 4271 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | 8621d01 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4272 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4273 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 4274 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4275 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4276 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4277 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | break; |
| 4281 | |
| 4282 | case OO_PercentEqual: |
| 4283 | case OO_LessLessEqual: |
| 4284 | case OO_GreaterGreaterEqual: |
| 4285 | case OO_AmpEqual: |
| 4286 | case OO_CaretEqual: |
| 4287 | case OO_PipeEqual: |
| 4288 | // C++ [over.built]p22: |
| 4289 | // |
| 4290 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 4291 | // is either volatile or empty, and R is a promoted integral |
| 4292 | // type, there exist candidate operator functions of the form |
| 4293 | // |
| 4294 | // VQ L& operator%=(VQ L&, R); |
| 4295 | // VQ L& operator<<=(VQ L&, R); |
| 4296 | // VQ L& operator>>=(VQ L&, R); |
| 4297 | // VQ L& operator&=(VQ L&, R); |
| 4298 | // VQ L& operator^=(VQ L&, R); |
| 4299 | // VQ L& operator|=(VQ L&, R); |
| 4300 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4301 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4302 | Right < LastPromotedIntegralType; ++Right) { |
| 4303 | QualType ParamTypes[2]; |
| 4304 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4305 | |
| 4306 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4307 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4308 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 035c46f | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4309 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4310 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4311 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4312 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4313 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4314 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4315 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | break; |
| 4319 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4320 | case OO_Exclaim: { |
| 4321 | // C++ [over.operator]p23: |
| 4322 | // |
| 4323 | // There also exist candidate operator functions of the form |
| 4324 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4326 | // bool operator&&(bool, bool); [BELOW] |
| 4327 | // bool operator||(bool, bool); [BELOW] |
| 4328 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4329 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4330 | /*IsAssignmentOperator=*/false, |
| 4331 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | } |
| 4334 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4335 | case OO_AmpAmp: |
| 4336 | case OO_PipePipe: { |
| 4337 | // C++ [over.operator]p23: |
| 4338 | // |
| 4339 | // There also exist candidate operator functions of the form |
| 4340 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4341 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4342 | // bool operator&&(bool, bool); |
| 4343 | // bool operator||(bool, bool); |
| 4344 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4345 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4346 | /*IsAssignmentOperator=*/false, |
| 4347 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4348 | break; |
| 4349 | } |
| 4350 | |
| 4351 | case OO_Subscript: |
| 4352 | // C++ [over.built]p13: |
| 4353 | // |
| 4354 | // For every cv-qualified or cv-unqualified object type T there |
| 4355 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4357 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4358 | // T& operator[](T*, ptrdiff_t); |
| 4359 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4360 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4361 | // T& operator[](ptrdiff_t, T*); |
| 4362 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4363 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4364 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4365 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4366 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4367 | |
| 4368 | // T& operator[](T*, ptrdiff_t) |
| 4369 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4370 | |
| 4371 | // T& operator[](ptrdiff_t, T*); |
| 4372 | ParamTypes[0] = ParamTypes[1]; |
| 4373 | ParamTypes[1] = *Ptr; |
| 4374 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4375 | } |
| 4376 | break; |
| 4377 | |
| 4378 | case OO_ArrowStar: |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4379 | // C++ [over.built]p11: |
| 4380 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4381 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4382 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4383 | // there exist candidate operator functions of the form |
| 4384 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4385 | // where CV12 is the union of CV1 and CV2. |
| 4386 | { |
| 4387 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4388 | CandidateTypes.pointer_begin(); |
| 4389 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4390 | QualType C1Ty = (*Ptr); |
| 4391 | QualType C1; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4392 | QualifierCollector Q1; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4393 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4394 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4395 | if (!isa<RecordType>(C1)) |
| 4396 | continue; |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4397 | // heuristic to reduce number of builtin candidates in the set. |
| 4398 | // Add volatile/restrict version only if there are conversions to a |
| 4399 | // volatile/restrict type. |
| 4400 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4401 | continue; |
| 4402 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4403 | continue; |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4404 | } |
| 4405 | for (BuiltinCandidateTypeSet::iterator |
| 4406 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4407 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4408 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4409 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4410 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 4303697 | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4411 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4412 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4413 | break; |
| 4414 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4415 | // build CV12 T& |
| 4416 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | a9cca89 | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4417 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4418 | T.isVolatileQualified()) |
| 4419 | continue; |
| 4420 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4421 | T.isRestrictQualified()) |
| 4422 | continue; |
Fariborz Jahanian | 5ecd539 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4423 | T = Q1.apply(T); |
Fariborz Jahanian | 4657a99 | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4424 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4425 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4426 | } |
| 4427 | } |
| 4428 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4429 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4430 | |
| 4431 | case OO_Conditional: |
| 4432 | // Note that we don't consider the first argument, since it has been |
| 4433 | // contextually converted to bool long ago. The candidates below are |
| 4434 | // therefore added as binary. |
| 4435 | // |
| 4436 | // C++ [over.built]p24: |
| 4437 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4438 | // there exist candidate operator functions of the form |
| 4439 | // |
| 4440 | // T operator?(bool, T, T); |
| 4441 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4442 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4443 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4444 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4445 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4446 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4447 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4448 | CandidateTypes.member_pointer_begin(), |
| 4449 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4450 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4451 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4452 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4453 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4457 | /// \brief Add function candidates found via argument-dependent lookup |
| 4458 | /// to the set of overloading candidates. |
| 4459 | /// |
| 4460 | /// This routine performs argument-dependent name lookup based on the |
| 4461 | /// given function name (which may also be an operator name) and adds |
| 4462 | /// all of the overload candidates found by ADL to the overload |
| 4463 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4465 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4466 | bool Operator, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4467 | Expr **Args, unsigned NumArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4468 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4469 | OverloadCandidateSet& CandidateSet, |
| 4470 | bool PartialOverloading) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4471 | ADLResult Fns; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4472 | |
John McCall | a113e72 | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 4473 | // FIXME: This approach for uniquing ADL results (and removing |
| 4474 | // redundant candidates from the set) relies on pointer-equality, |
| 4475 | // which means we need to key off the canonical decl. However, |
| 4476 | // always going back to the canonical decl might not get us the |
| 4477 | // right set of default arguments. What default arguments are |
| 4478 | // we supposed to consider on ADL candidates, anyway? |
| 4479 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4480 | // FIXME: Pass in the explicit template arguments? |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4481 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4483 | // Erase all of the candidates we already knew about. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4484 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4485 | CandEnd = CandidateSet.end(); |
| 4486 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4487 | if (Cand->Function) { |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4488 | Fns.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4489 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4490 | Fns.erase(FunTmpl); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4491 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4492 | |
| 4493 | // For each of the ADL candidates we found, add it to the overload |
| 4494 | // set. |
John McCall | 7edb5fd | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4495 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4496 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4497 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4498 | if (ExplicitTemplateArgs) |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4499 | continue; |
| 4500 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4501 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 4502 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4503 | } else |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4504 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4505 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4506 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4507 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4510 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4511 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4512 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4513 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4514 | const OverloadCandidate& Cand2, |
| 4515 | SourceLocation Loc) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4516 | // Define viable functions to be better candidates than non-viable |
| 4517 | // functions. |
| 4518 | if (!Cand2.Viable) |
| 4519 | return Cand1.Viable; |
| 4520 | else if (!Cand1.Viable) |
| 4521 | return false; |
| 4522 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4523 | // C++ [over.match.best]p1: |
| 4524 | // |
| 4525 | // -- if F is a static member function, ICS1(F) is defined such |
| 4526 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4527 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4528 | // better nor worse than ICS1(F). |
| 4529 | unsigned StartArg = 0; |
| 4530 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4531 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4532 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4533 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4534 | // A viable function F1 is defined to be a better function than another |
| 4535 | // 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] | 4536 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4537 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4538 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4539 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4540 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4541 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4542 | Cand2.Conversions[ArgIdx])) { |
| 4543 | case ImplicitConversionSequence::Better: |
| 4544 | // Cand1 has a better conversion sequence. |
| 4545 | HasBetterConversion = true; |
| 4546 | break; |
| 4547 | |
| 4548 | case ImplicitConversionSequence::Worse: |
| 4549 | // Cand1 can't be better than Cand2. |
| 4550 | return false; |
| 4551 | |
| 4552 | case ImplicitConversionSequence::Indistinguishable: |
| 4553 | // Do nothing. |
| 4554 | break; |
| 4555 | } |
| 4556 | } |
| 4557 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4558 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4559 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4560 | if (HasBetterConversion) |
| 4561 | return true; |
| 4562 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4563 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4564 | // specialization, or, if not that, |
| 4565 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4566 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4567 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4568 | |
| 4569 | // -- F1 and F2 are function template specializations, and the function |
| 4570 | // template for F1 is more specialized than the template for F2 |
| 4571 | // 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] | 4572 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4573 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4574 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4575 | if (FunctionTemplateDecl *BetterTemplate |
| 4576 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4577 | Cand2.Function->getPrimaryTemplate(), |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4578 | Loc, |
Douglas Gregor | 5d7d375 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4579 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4580 | : TPOC_Call)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4581 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4582 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4583 | // -- the context is an initialization by user-defined conversion |
| 4584 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4585 | // from the return type of F1 to the destination type (i.e., |
| 4586 | // the type of the entity being initialized) is a better |
| 4587 | // conversion sequence than the standard conversion sequence |
| 4588 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | if (Cand1.Function && Cand2.Function && |
| 4590 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4591 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4592 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4593 | Cand2.FinalConversion)) { |
| 4594 | case ImplicitConversionSequence::Better: |
| 4595 | // Cand1 has a better conversion sequence. |
| 4596 | return true; |
| 4597 | |
| 4598 | case ImplicitConversionSequence::Worse: |
| 4599 | // Cand1 can't be better than Cand2. |
| 4600 | return false; |
| 4601 | |
| 4602 | case ImplicitConversionSequence::Indistinguishable: |
| 4603 | // Do nothing |
| 4604 | break; |
| 4605 | } |
| 4606 | } |
| 4607 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4608 | return false; |
| 4609 | } |
| 4610 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4612 | /// within an overload candidate set. |
| 4613 | /// |
| 4614 | /// \param CandidateSet the set of candidate functions. |
| 4615 | /// |
| 4616 | /// \param Loc the location of the function name (or operator symbol) for |
| 4617 | /// which overload resolution occurs. |
| 4618 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4620 | /// function, Best points to the candidate function found. |
| 4621 | /// |
| 4622 | /// \returns The result of overload resolution. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4623 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4624 | SourceLocation Loc, |
| 4625 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4626 | // Find the best viable function. |
| 4627 | Best = CandidateSet.end(); |
| 4628 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4629 | Cand != CandidateSet.end(); ++Cand) { |
| 4630 | if (Cand->Viable) { |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4631 | if (Best == CandidateSet.end() || |
| 4632 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4633 | Best = Cand; |
| 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | // If we didn't find any viable functions, abort. |
| 4638 | if (Best == CandidateSet.end()) |
| 4639 | return OR_No_Viable_Function; |
| 4640 | |
| 4641 | // Make sure that this function is better than every other viable |
| 4642 | // function. If not, we have an ambiguity. |
| 4643 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4644 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4646 | Cand != Best && |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4647 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4648 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4649 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4650 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4653 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4654 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4656 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4657 | return OR_Deleted; |
| 4658 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4659 | // C++ [basic.def.odr]p2: |
| 4660 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4662 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4663 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4664 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4665 | if (Best->Function) |
| 4666 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4667 | return OR_Success; |
| 4668 | } |
| 4669 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4670 | namespace { |
| 4671 | |
| 4672 | enum OverloadCandidateKind { |
| 4673 | oc_function, |
| 4674 | oc_method, |
| 4675 | oc_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4676 | oc_function_template, |
| 4677 | oc_method_template, |
| 4678 | oc_constructor_template, |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4679 | oc_implicit_default_constructor, |
| 4680 | oc_implicit_copy_constructor, |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4681 | oc_implicit_copy_assignment |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4682 | }; |
| 4683 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4684 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4685 | FunctionDecl *Fn, |
| 4686 | std::string &Description) { |
| 4687 | bool isTemplate = false; |
| 4688 | |
| 4689 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4690 | isTemplate = true; |
| 4691 | Description = S.getTemplateArgumentBindingsText( |
| 4692 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4693 | } |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4694 | |
| 4695 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4696 | if (!Ctor->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4697 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4698 | |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4699 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4700 | : oc_implicit_default_constructor; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4704 | // This actually gets spelled 'candidate function' for now, but |
| 4705 | // it doesn't hurt to split it out. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4706 | if (!Meth->isImplicit()) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4707 | return isTemplate ? oc_method_template : oc_method; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4708 | |
| 4709 | assert(Meth->isCopyAssignment() |
| 4710 | && "implicit method is not copy assignment operator?"); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4711 | return oc_implicit_copy_assignment; |
| 4712 | } |
| 4713 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4714 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | } // end anonymous namespace |
| 4718 | |
| 4719 | // Notes the location of an overload candidate. |
| 4720 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4721 | std::string FnDesc; |
| 4722 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4723 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4724 | << (unsigned) K << FnDesc; |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4727 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4728 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4729 | /// target types of the conversion. |
| 4730 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4731 | SourceLocation CaretLoc, |
| 4732 | const PartialDiagnostic &PDiag) { |
| 4733 | Diag(CaretLoc, PDiag) |
| 4734 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4735 | for (AmbiguousConversionSequence::const_iterator |
| 4736 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4737 | NoteOverloadCandidate(*I); |
| 4738 | } |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4741 | namespace { |
| 4742 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4743 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4744 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4745 | assert(Conv.isBad()); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4746 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4747 | FunctionDecl *Fn = Cand->Function; |
| 4748 | |
| 4749 | // There's a conversion slot for the object argument if this is a |
| 4750 | // non-constructor method. Note that 'I' corresponds the |
| 4751 | // conversion-slot index. |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4752 | bool isObjectArgument = false; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4753 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4754 | if (I == 0) |
| 4755 | isObjectArgument = true; |
| 4756 | else |
| 4757 | I--; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4760 | std::string FnDesc; |
| 4761 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4762 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4763 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4764 | QualType FromTy = Conv.Bad.getFromType(); |
| 4765 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4766 | |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4767 | if (FromTy == S.Context.OverloadTy) { |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4768 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4769 | Expr *E = FromExpr->IgnoreParens(); |
| 4770 | if (isa<UnaryOperator>(E)) |
| 4771 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4772 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | 5920dbb | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4773 | |
| 4774 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 4775 | << (unsigned) FnKind << FnDesc |
| 4776 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4777 | << ToTy << Name << I+1; |
| 4778 | return; |
| 4779 | } |
| 4780 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4781 | // Do some hand-waving analysis to see if the non-viability is due |
| 4782 | // to a qualifier mismatch. |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4783 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4784 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4785 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4786 | CToTy = RT->getPointeeType(); |
| 4787 | else { |
| 4788 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4789 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4790 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4791 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4792 | } |
| 4793 | |
| 4794 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4795 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4796 | // It is dumb that we have to do this here. |
| 4797 | while (isa<ArrayType>(CFromTy)) |
| 4798 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4799 | while (isa<ArrayType>(CToTy)) |
| 4800 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4801 | |
| 4802 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4803 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4804 | |
| 4805 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4806 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4807 | << (unsigned) FnKind << FnDesc |
| 4808 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4809 | << FromTy |
| 4810 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4811 | << (unsigned) isObjectArgument << I+1; |
| 4812 | return; |
| 4813 | } |
| 4814 | |
| 4815 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4816 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4817 | |
| 4818 | if (isObjectArgument) { |
| 4819 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4820 | << (unsigned) FnKind << FnDesc |
| 4821 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4822 | << FromTy << (CVR - 1); |
| 4823 | } else { |
| 4824 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4825 | << (unsigned) FnKind << FnDesc |
| 4826 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4827 | << FromTy << (CVR - 1) << I+1; |
| 4828 | } |
| 4829 | return; |
| 4830 | } |
| 4831 | |
John McCall | 258b203 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4832 | // Diagnose references or pointers to incomplete types differently, |
| 4833 | // since it's far from impossible that the incompleteness triggered |
| 4834 | // the failure. |
| 4835 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 4836 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 4837 | TempFromTy = PTy->getPointeeType(); |
| 4838 | if (TempFromTy->isIncompleteType()) { |
| 4839 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 4840 | << (unsigned) FnKind << FnDesc |
| 4841 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4842 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4843 | return; |
| 4844 | } |
| 4845 | |
John McCall | 651f3ee | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4846 | // TODO: specialize more based on the kind of mismatch |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4847 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4848 | << (unsigned) FnKind << FnDesc |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4849 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | e81e15e | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 4850 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4854 | unsigned NumFormalArgs) { |
| 4855 | // TODO: treat calls to a missing default constructor as a special case |
| 4856 | |
| 4857 | FunctionDecl *Fn = Cand->Function; |
| 4858 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4859 | |
| 4860 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4861 | |
| 4862 | // at least / at most / exactly |
| 4863 | unsigned mode, modeCount; |
| 4864 | if (NumFormalArgs < MinParams) { |
| 4865 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4866 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4867 | mode = 0; // "at least" |
| 4868 | else |
| 4869 | mode = 2; // "exactly" |
| 4870 | modeCount = MinParams; |
| 4871 | } else { |
| 4872 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4873 | if (MinParams != FnTy->getNumArgs()) |
| 4874 | mode = 1; // "at most" |
| 4875 | else |
| 4876 | mode = 2; // "exactly" |
| 4877 | modeCount = FnTy->getNumArgs(); |
| 4878 | } |
| 4879 | |
| 4880 | std::string Description; |
| 4881 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4882 | |
| 4883 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4884 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4887 | /// Diagnose a failed template-argument deduction. |
| 4888 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 4889 | Expr **Args, unsigned NumArgs) { |
| 4890 | FunctionDecl *Fn = Cand->Function; // pattern |
| 4891 | |
| 4892 | TemplateParameter Param = TemplateParameter::getFromOpaqueValue( |
| 4893 | Cand->DeductionFailure.TemplateParameter); |
| 4894 | |
| 4895 | switch (Cand->DeductionFailure.Result) { |
| 4896 | case Sema::TDK_Success: |
| 4897 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 4898 | |
| 4899 | case Sema::TDK_Incomplete: { |
| 4900 | NamedDecl *ParamD; |
| 4901 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 4902 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 4903 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
| 4904 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 4905 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 4906 | << ParamD->getDeclName(); |
| 4907 | return; |
| 4908 | } |
| 4909 | |
| 4910 | // TODO: diagnose these individually, then kill off |
| 4911 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
| 4912 | case Sema::TDK_InstantiationDepth: |
| 4913 | case Sema::TDK_Inconsistent: |
| 4914 | case Sema::TDK_InconsistentQuals: |
| 4915 | case Sema::TDK_SubstitutionFailure: |
| 4916 | case Sema::TDK_NonDeducedMismatch: |
| 4917 | case Sema::TDK_TooManyArguments: |
| 4918 | case Sema::TDK_TooFewArguments: |
| 4919 | case Sema::TDK_InvalidExplicitArguments: |
| 4920 | case Sema::TDK_FailedOverloadResolution: |
| 4921 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 4922 | return; |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 4927 | /// already generated a primary error at the call site. |
| 4928 | /// |
| 4929 | /// It really does need to be a single diagnostic with its caret |
| 4930 | /// pointed at the candidate declaration. Yes, this creates some |
| 4931 | /// major challenges of technical writing. Yes, this makes pointing |
| 4932 | /// out problems with specific arguments quite awkward. It's still |
| 4933 | /// better than generating twenty screens of text for every failed |
| 4934 | /// overload. |
| 4935 | /// |
| 4936 | /// It would be great to be able to express per-candidate problems |
| 4937 | /// more richly for those diagnostic clients that cared, but we'd |
| 4938 | /// still have to be just as careful with the default diagnostics. |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4939 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4940 | Expr **Args, unsigned NumArgs) { |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4941 | FunctionDecl *Fn = Cand->Function; |
| 4942 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4943 | // Note deleted candidates, but only if they're viable. |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4944 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4945 | std::string FnDesc; |
| 4946 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 3c80f57 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4947 | |
| 4948 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4949 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4950 | return; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4953 | // We don't really have anything else to say about viable candidates. |
| 4954 | if (Cand->Viable) { |
| 4955 | S.NoteOverloadCandidate(Fn); |
| 4956 | return; |
| 4957 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4958 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4959 | switch (Cand->FailureKind) { |
| 4960 | case ovl_fail_too_many_arguments: |
| 4961 | case ovl_fail_too_few_arguments: |
| 4962 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4963 | |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4964 | case ovl_fail_bad_deduction: |
John McCall | 342fec4 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4965 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 4966 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4967 | case ovl_fail_trivial_conversion: |
| 4968 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | c520c84 | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4969 | case ovl_fail_final_conversion_not_exact: |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4970 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4971 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4972 | case ovl_fail_bad_conversion: { |
| 4973 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 4974 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | adbb8f8 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4975 | if (Cand->Conversions[I].isBad()) |
| 4976 | return DiagnoseBadConversion(S, Cand, I); |
| 4977 | |
| 4978 | // FIXME: this currently happens when we're called from SemaInit |
| 4979 | // when user-conversion overload fails. Figure out how to handle |
| 4980 | // those conditions and diagnose them well. |
| 4981 | return S.NoteOverloadCandidate(Fn); |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4982 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4983 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4987 | // Desugar the type of the surrogate down to a function type, |
| 4988 | // retaining as many typedefs as possible while still showing |
| 4989 | // the function type (and, therefore, its parameter types). |
| 4990 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4991 | bool isLValueReference = false; |
| 4992 | bool isRValueReference = false; |
| 4993 | bool isPointer = false; |
| 4994 | if (const LValueReferenceType *FnTypeRef = |
| 4995 | FnType->getAs<LValueReferenceType>()) { |
| 4996 | FnType = FnTypeRef->getPointeeType(); |
| 4997 | isLValueReference = true; |
| 4998 | } else if (const RValueReferenceType *FnTypeRef = |
| 4999 | FnType->getAs<RValueReferenceType>()) { |
| 5000 | FnType = FnTypeRef->getPointeeType(); |
| 5001 | isRValueReference = true; |
| 5002 | } |
| 5003 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5004 | FnType = FnTypePtr->getPointeeType(); |
| 5005 | isPointer = true; |
| 5006 | } |
| 5007 | // Desugar down to a function type. |
| 5008 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5009 | // Reconstruct the pointer/reference as appropriate. |
| 5010 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5011 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5012 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5013 | |
| 5014 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5015 | << FnType; |
| 5016 | } |
| 5017 | |
| 5018 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5019 | const char *Opc, |
| 5020 | SourceLocation OpLoc, |
| 5021 | OverloadCandidate *Cand) { |
| 5022 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5023 | std::string TypeStr("operator"); |
| 5024 | TypeStr += Opc; |
| 5025 | TypeStr += "("; |
| 5026 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5027 | if (Cand->Conversions.size() == 1) { |
| 5028 | TypeStr += ")"; |
| 5029 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5030 | } else { |
| 5031 | TypeStr += ", "; |
| 5032 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5033 | TypeStr += ")"; |
| 5034 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5039 | OverloadCandidate *Cand) { |
| 5040 | unsigned NoOperands = Cand->Conversions.size(); |
| 5041 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5042 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5043 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5044 | if (!ICS.isAmbiguous()) continue; |
| 5045 | |
| 5046 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5047 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5051 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5052 | if (Cand->Function) |
| 5053 | return Cand->Function->getLocation(); |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5054 | if (Cand->IsSurrogate) |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5055 | return Cand->Surrogate->getLocation(); |
| 5056 | return SourceLocation(); |
| 5057 | } |
| 5058 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5059 | struct CompareOverloadCandidatesForDisplay { |
| 5060 | Sema &S; |
| 5061 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5062 | |
| 5063 | bool operator()(const OverloadCandidate *L, |
| 5064 | const OverloadCandidate *R) { |
John McCall | f3cf22b | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5065 | // Fast-path this check. |
| 5066 | if (L == R) return false; |
| 5067 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5068 | // Order first by viability. |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5069 | if (L->Viable) { |
| 5070 | if (!R->Viable) return true; |
| 5071 | |
| 5072 | // TODO: introduce a tri-valued comparison for overload |
| 5073 | // candidates. Would be more worthwhile if we had a sort |
| 5074 | // that could exploit it. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5075 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 5076 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5077 | } else if (R->Viable) |
| 5078 | return false; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5079 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5080 | assert(L->Viable == R->Viable); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5081 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5082 | // Criteria by which we can sort non-viable candidates: |
| 5083 | if (!L->Viable) { |
| 5084 | // 1. Arity mismatches come after other candidates. |
| 5085 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5086 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5087 | return false; |
| 5088 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5089 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5090 | return true; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5091 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5092 | // 2. Bad conversions come first and are ordered by the number |
| 5093 | // of bad conversions and quality of good conversions. |
| 5094 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5095 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5096 | return true; |
| 5097 | |
| 5098 | // If there's any ordering between the defined conversions... |
| 5099 | // FIXME: this might not be transitive. |
| 5100 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5101 | |
| 5102 | int leftBetter = 0; |
John McCall | 3a81337 | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5103 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5104 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5105 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 5106 | R->Conversions[I])) { |
| 5107 | case ImplicitConversionSequence::Better: |
| 5108 | leftBetter++; |
| 5109 | break; |
| 5110 | |
| 5111 | case ImplicitConversionSequence::Worse: |
| 5112 | leftBetter--; |
| 5113 | break; |
| 5114 | |
| 5115 | case ImplicitConversionSequence::Indistinguishable: |
| 5116 | break; |
| 5117 | } |
| 5118 | } |
| 5119 | if (leftBetter > 0) return true; |
| 5120 | if (leftBetter < 0) return false; |
| 5121 | |
| 5122 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 5123 | return false; |
| 5124 | |
John McCall | 1b77e73 | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5125 | // TODO: others? |
| 5126 | } |
| 5127 | |
| 5128 | // Sort everything else by location. |
| 5129 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 5130 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 5131 | |
| 5132 | // Put candidates without locations (e.g. builtins) at the end. |
| 5133 | if (LLoc.isInvalid()) return false; |
| 5134 | if (RLoc.isInvalid()) return true; |
| 5135 | |
| 5136 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5137 | } |
| 5138 | }; |
| 5139 | |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5140 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 5141 | /// computes up to the first |
| 5142 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 5143 | Expr **Args, unsigned NumArgs) { |
| 5144 | assert(!Cand->Viable); |
| 5145 | |
| 5146 | // Don't do anything on failures other than bad conversion. |
| 5147 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 5148 | |
| 5149 | // Skip forward to the first bad conversion. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5150 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5151 | unsigned ConvCount = Cand->Conversions.size(); |
| 5152 | while (true) { |
| 5153 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 5154 | ConvIdx++; |
| 5155 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 5156 | break; |
| 5157 | } |
| 5158 | |
| 5159 | if (ConvIdx == ConvCount) |
| 5160 | return; |
| 5161 | |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5162 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 5163 | "remaining conversion is initialized?"); |
| 5164 | |
Douglas Gregor | 23ef6c0 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 5165 | // FIXME: this should probably be preserved from the overload |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5166 | // operation somehow. |
| 5167 | bool SuppressUserConversions = false; |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5168 | |
| 5169 | const FunctionProtoType* Proto; |
| 5170 | unsigned ArgIdx = ConvIdx; |
| 5171 | |
| 5172 | if (Cand->IsSurrogate) { |
| 5173 | QualType ConvType |
| 5174 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 5175 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5176 | ConvType = ConvPtrType->getPointeeType(); |
| 5177 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 5178 | ArgIdx--; |
| 5179 | } else if (Cand->Function) { |
| 5180 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 5181 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 5182 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 5183 | ArgIdx--; |
| 5184 | } else { |
| 5185 | // Builtin binary operator with a bad first conversion. |
| 5186 | assert(ConvCount <= 3); |
| 5187 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 5188 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5189 | = TryCopyInitialization(S, Args[ConvIdx], |
| 5190 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 5191 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5192 | /*InOverloadResolution*/ true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5193 | return; |
| 5194 | } |
| 5195 | |
| 5196 | // Fill in the rest of the conversions. |
| 5197 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5198 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 5199 | if (ArgIdx < NumArgsInProto) |
| 5200 | Cand->Conversions[ConvIdx] |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5201 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 5202 | SuppressUserConversions, |
Douglas Gregor | 74eb658 | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5203 | /*InOverloadResolution=*/true); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5204 | else |
| 5205 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 5206 | } |
| 5207 | } |
| 5208 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5209 | } // end anonymous namespace |
| 5210 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5211 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 5212 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5213 | /// set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5214 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5215 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5216 | OverloadCandidateDisplayKind OCD, |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5217 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5218 | const char *Opc, |
Fariborz Jahanian | 16a5eac | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 5219 | SourceLocation OpLoc) { |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5220 | // Sort the candidates by viability and position. Sorting directly would |
| 5221 | // be prohibitive, so we make a set of pointers and sort those. |
| 5222 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 5223 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 5224 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5225 | LastCand = CandidateSet.end(); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5226 | Cand != LastCand; ++Cand) { |
| 5227 | if (Cand->Viable) |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5228 | Cands.push_back(Cand); |
John McCall | 717e891 | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5229 | else if (OCD == OCD_AllCandidates) { |
| 5230 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 5231 | Cands.push_back(Cand); |
| 5232 | } |
| 5233 | } |
| 5234 | |
John McCall | bf65c0b | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5235 | std::sort(Cands.begin(), Cands.end(), |
| 5236 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5237 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5238 | bool ReportedAmbiguousConversions = false; |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5239 | |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5240 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 5241 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 5242 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 5243 | |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5244 | if (Cand->Function) |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5245 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5246 | else if (Cand->IsSurrogate) |
| 5247 | NoteSurrogateCandidate(*this, Cand); |
| 5248 | |
| 5249 | // This a builtin candidate. We do not, in general, want to list |
| 5250 | // every possible builtin candidate. |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5251 | else if (Cand->Viable) { |
| 5252 | // Generally we only see ambiguities including viable builtin |
| 5253 | // operators if overload resolution got screwed up by an |
| 5254 | // ambiguous user-defined conversion. |
| 5255 | // |
| 5256 | // FIXME: It's quite possible for different conversions to see |
| 5257 | // different ambiguities, though. |
| 5258 | if (!ReportedAmbiguousConversions) { |
| 5259 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 5260 | ReportedAmbiguousConversions = true; |
| 5261 | } |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5262 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5263 | // If this is a viable builtin, print it. |
John McCall | a1d7d62 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5264 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5265 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5266 | } |
| 5267 | } |
| 5268 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5269 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5270 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5271 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5272 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5273 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5276 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 5277 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 5278 | /// expression with overloaded function type and @p ToType is the type |
| 5279 | /// we're trying to resolve to. For example: |
| 5280 | /// |
| 5281 | /// @code |
| 5282 | /// int f(double); |
| 5283 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5285 | /// int (*pfd)(double) = f; // selects f(double) |
| 5286 | /// @endcode |
| 5287 | /// |
| 5288 | /// This routine returns the resulting FunctionDecl if it could be |
| 5289 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 5290 | /// routine will emit diagnostics if there is an error. |
| 5291 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5292 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5293 | bool Complain, |
| 5294 | DeclAccessPair &FoundResult) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5295 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5296 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5297 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5298 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5299 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 5300 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5301 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5302 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5303 | FunctionType = MemTypePtr->getPointeeType(); |
| 5304 | IsMember = true; |
| 5305 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5307 | // C++ [over.over]p1: |
| 5308 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5309 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5310 | // C++ [over.over]p1: |
| 5311 | // [...] The overloaded function name can be preceded by the & |
| 5312 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5313 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5314 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 5315 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 5316 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 5317 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5318 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5319 | |
| 5320 | // We expect a pointer or reference to function, or a function pointer. |
| 5321 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 5322 | if (!FunctionType->isFunctionType()) { |
| 5323 | if (Complain) |
| 5324 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 5325 | << OvlExpr->getName() << ToType; |
| 5326 | |
| 5327 | return 0; |
| 5328 | } |
| 5329 | |
| 5330 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5331 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5332 | // Look through all of the overloaded functions, searching for one |
| 5333 | // whose type matches exactly. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5334 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5335 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 5336 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5337 | bool FoundNonTemplateFunction = false; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5338 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5339 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5340 | // Look through any using declarations to find the underlying function. |
| 5341 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5342 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5343 | // C++ [over.over]p3: |
| 5344 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 5345 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5346 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5347 | // type "pointer-to-member-function." |
| 5348 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5349 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5350 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5351 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5352 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5353 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5354 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5355 | // static when converting to member pointer. |
| 5356 | if (Method->isStatic() == IsMember) |
| 5357 | continue; |
| 5358 | } else if (IsMember) |
| 5359 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5360 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5361 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5362 | // If the name is a function template, template argument deduction is |
| 5363 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5364 | // resulting template argument list is used to generate a single |
| 5365 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5366 | // overloaded functions considered. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5367 | // 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] | 5368 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5369 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5370 | if (TemplateDeductionResult Result |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5371 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5372 | FunctionType, Specialization, Info)) { |
| 5373 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5374 | (void)Result; |
| 5375 | } else { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5376 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5377 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5378 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5379 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5380 | Matches.push_back(std::make_pair(I.getPair(), |
| 5381 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5382 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5383 | |
| 5384 | continue; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5386 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5387 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5388 | // Skip non-static functions when converting to pointer, and static |
| 5389 | // when converting to member pointer. |
| 5390 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5391 | continue; |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5392 | |
| 5393 | // If we have explicit template arguments, skip non-templates. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5394 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5395 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5396 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5397 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5398 | |
Chandler Carruth | bd64729 | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5399 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 5400 | QualType ResultTy; |
| 5401 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5402 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5403 | ResultTy)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5404 | Matches.push_back(std::make_pair(I.getPair(), |
| 5405 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5406 | FoundNonTemplateFunction = true; |
| 5407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5408 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5411 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5412 | if (Matches.empty()) { |
| 5413 | if (Complain) { |
| 5414 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 5415 | << OvlExpr->getName() << FunctionType; |
| 5416 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5417 | E = OvlExpr->decls_end(); |
| 5418 | I != E; ++I) |
| 5419 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 5420 | NoteOverloadCandidate(F); |
| 5421 | } |
| 5422 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5423 | return 0; |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5424 | } else if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5425 | FunctionDecl *Result = Matches[0].second; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5426 | FoundResult = Matches[0].first; |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5427 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5428 | if (Complain) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5429 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5430 | return Result; |
| 5431 | } |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5432 | |
| 5433 | // C++ [over.over]p4: |
| 5434 | // If more than one function is selected, [...] |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5435 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5436 | // [...] and any given function template specialization F1 is |
| 5437 | // eliminated if the set contains a second function template |
| 5438 | // specialization whose function template is more specialized |
| 5439 | // than the function template of F1 according to the partial |
| 5440 | // ordering rules of 14.5.5.2. |
| 5441 | |
| 5442 | // The algorithm specified above is quadratic. We instead use a |
| 5443 | // two-pass algorithm (similar to the one used to identify the |
| 5444 | // best viable function in an overload set) that identifies the |
| 5445 | // best function template (if it exists). |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5446 | |
| 5447 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 5448 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5449 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5450 | |
| 5451 | UnresolvedSetIterator Result = |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5452 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5453 | TPOC_Other, From->getLocStart(), |
| 5454 | PDiag(), |
| 5455 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5456 | << Matches[0].second->getDeclName(), |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5457 | PDiag(diag::note_ovl_candidate) |
| 5458 | << (unsigned) oc_function_template); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5459 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5460 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5461 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
| 5462 | if (Complain) |
| 5463 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5464 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5466 | |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5467 | // [...] any function template specializations in the set are |
| 5468 | // eliminated if the set also contains a non-template function, [...] |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5469 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5470 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5471 | ++I; |
| 5472 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5473 | Matches[I] = Matches[--N]; |
| 5474 | Matches.set_size(N); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5475 | } |
| 5476 | } |
Douglas Gregor | 312a202 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5477 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5478 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5479 | // selected function. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5480 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5481 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5482 | FoundResult = Matches[0].first; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5483 | if (Complain) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5484 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
| 5485 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | 07ab202 | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5488 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5489 | // returns (even if we issue the diagnostics here). |
| 5490 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5491 | << Matches[0].second->getDeclName(); |
| 5492 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5493 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5494 | return 0; |
| 5495 | } |
| 5496 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5497 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5498 | /// resolve that overloaded function expression down to a single function. |
| 5499 | /// |
| 5500 | /// This routine can only resolve template-ids that refer to a single function |
| 5501 | /// template, where that template-id refers to a single template whose template |
| 5502 | /// arguments are either provided by the template-id or have defaults, |
| 5503 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5504 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5505 | // C++ [over.over]p1: |
| 5506 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5507 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5508 | // C++ [over.over]p1: |
| 5509 | // [...] The overloaded function name can be preceded by the & |
| 5510 | // operator. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5511 | |
| 5512 | if (From->getType() != Context.OverloadTy) |
| 5513 | return 0; |
| 5514 | |
| 5515 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5516 | |
| 5517 | // If we didn't actually find any template-ids, we're done. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5518 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5519 | return 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5520 | |
| 5521 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5522 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5523 | |
| 5524 | // Look through all of the overloaded functions, searching for one |
| 5525 | // whose type matches exactly. |
| 5526 | FunctionDecl *Matched = 0; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5527 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5528 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5529 | // C++0x [temp.arg.explicit]p3: |
| 5530 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5531 | // where deduction is not done, if a template argument list is |
| 5532 | // specified and it, along with any default template arguments, |
| 5533 | // identifies a single function template specialization, then the |
| 5534 | // template-id is an lvalue for the function template specialization. |
| 5535 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5536 | |
| 5537 | // C++ [over.over]p2: |
| 5538 | // If the name is a function template, template argument deduction is |
| 5539 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5540 | // resulting template argument list is used to generate a single |
| 5541 | // function template specialization, which is added to the set of |
| 5542 | // overloaded functions considered. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5543 | FunctionDecl *Specialization = 0; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5544 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5545 | if (TemplateDeductionResult Result |
| 5546 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5547 | Specialization, Info)) { |
| 5548 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5549 | (void)Result; |
| 5550 | continue; |
| 5551 | } |
| 5552 | |
| 5553 | // Multiple matches; we can't resolve to a single declaration. |
| 5554 | if (Matched) |
| 5555 | return 0; |
| 5556 | |
| 5557 | Matched = Specialization; |
| 5558 | } |
| 5559 | |
| 5560 | return Matched; |
| 5561 | } |
| 5562 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5563 | /// \brief Add a single candidate to the overload set. |
| 5564 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5565 | DeclAccessPair FoundDecl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5566 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5567 | Expr **Args, unsigned NumArgs, |
| 5568 | OverloadCandidateSet &CandidateSet, |
| 5569 | bool PartialOverloading) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5570 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5571 | if (isa<UsingShadowDecl>(Callee)) |
| 5572 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5573 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5574 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5575 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5576 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | c27d6c5 | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5577 | false, PartialOverloading); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5578 | return; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | if (FunctionTemplateDecl *FuncTemplate |
| 5582 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5583 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 5584 | ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5585 | Args, NumArgs, CandidateSet); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5586 | return; |
| 5587 | } |
| 5588 | |
| 5589 | assert(false && "unhandled case in overloaded call candidate"); |
| 5590 | |
| 5591 | // do nothing? |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5595 | /// dependent lookup to the given overload set. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5596 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5597 | Expr **Args, unsigned NumArgs, |
| 5598 | OverloadCandidateSet &CandidateSet, |
| 5599 | bool PartialOverloading) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5600 | |
| 5601 | #ifndef NDEBUG |
| 5602 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5603 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5604 | // |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5605 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5606 | // and let Y be the lookup set produced by argument dependent |
| 5607 | // lookup (defined as follows). If X contains |
| 5608 | // |
| 5609 | // -- a declaration of a class member, or |
| 5610 | // |
| 5611 | // -- a block-scope function declaration that is not a |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5612 | // using-declaration, or |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5613 | // |
| 5614 | // -- a declaration that is neither a function or a function |
| 5615 | // template |
| 5616 | // |
| 5617 | // then Y is empty. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5618 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5619 | if (ULE->requiresADL()) { |
| 5620 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5621 | E = ULE->decls_end(); I != E; ++I) { |
| 5622 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5623 | assert(isa<UsingShadowDecl>(*I) || |
| 5624 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5625 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5626 | } |
| 5627 | } |
| 5628 | #endif |
| 5629 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5630 | // It would be nice to avoid this copy. |
| 5631 | TemplateArgumentListInfo TABuffer; |
| 5632 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5633 | if (ULE->hasExplicitTemplateArgs()) { |
| 5634 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5635 | ExplicitTemplateArgs = &TABuffer; |
| 5636 | } |
| 5637 | |
| 5638 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5639 | E = ULE->decls_end(); I != E; ++I) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5640 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5641 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5642 | PartialOverloading); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5643 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5644 | if (ULE->requiresADL()) |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5645 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 5646 | Args, NumArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5647 | ExplicitTemplateArgs, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5648 | CandidateSet, |
| 5649 | PartialOverloading); |
| 5650 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5651 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5652 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5653 | Expr **Args, unsigned NumArgs) { |
| 5654 | Fn->Destroy(SemaRef.Context); |
| 5655 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5656 | Args[Arg]->Destroy(SemaRef.Context); |
| 5657 | return SemaRef.ExprError(); |
| 5658 | } |
| 5659 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5660 | /// Attempts to recover from a call where no functions were found. |
| 5661 | /// |
| 5662 | /// Returns true if new candidates were found. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5663 | static Sema::OwningExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5664 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5665 | UnresolvedLookupExpr *ULE, |
| 5666 | SourceLocation LParenLoc, |
| 5667 | Expr **Args, unsigned NumArgs, |
| 5668 | SourceLocation *CommaLocs, |
| 5669 | SourceLocation RParenLoc) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5670 | |
| 5671 | CXXScopeSpec SS; |
| 5672 | if (ULE->getQualifier()) { |
| 5673 | SS.setScopeRep(ULE->getQualifier()); |
| 5674 | SS.setRange(ULE->getQualifierRange()); |
| 5675 | } |
| 5676 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5677 | TemplateArgumentListInfo TABuffer; |
| 5678 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5679 | if (ULE->hasExplicitTemplateArgs()) { |
| 5680 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5681 | ExplicitTemplateArgs = &TABuffer; |
| 5682 | } |
| 5683 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5684 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5685 | Sema::LookupOrdinaryName); |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5686 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R)) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5687 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5688 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5689 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5690 | |
| 5691 | // Build an implicit member call if appropriate. Just drop the |
| 5692 | // casts and such from the call, we don't really care. |
| 5693 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5694 | if ((*R.begin())->isCXXClassMember()) |
| 5695 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5696 | else if (ExplicitTemplateArgs) |
| 5697 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5698 | else |
| 5699 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5700 | |
| 5701 | if (NewFn.isInvalid()) |
| 5702 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5703 | |
| 5704 | Fn->Destroy(SemaRef.Context); |
| 5705 | |
| 5706 | // This shouldn't cause an infinite loop because we're giving it |
| 5707 | // an expression with non-empty lookup results, which should never |
| 5708 | // end up here. |
| 5709 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5710 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5711 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5712 | } |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5713 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5714 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5715 | /// (which eventually refers to the declaration Func) and the call |
| 5716 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5717 | /// to a specific function. If overload resolution succeeds, returns |
| 5718 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 5719 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5720 | /// arguments and Fn, and returns NULL. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5721 | Sema::OwningExprResult |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5722 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5723 | SourceLocation LParenLoc, |
| 5724 | Expr **Args, unsigned NumArgs, |
| 5725 | SourceLocation *CommaLocs, |
| 5726 | SourceLocation RParenLoc) { |
| 5727 | #ifndef NDEBUG |
| 5728 | if (ULE->requiresADL()) { |
| 5729 | // To do ADL, we must have found an unqualified name. |
| 5730 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5731 | |
| 5732 | // We don't perform ADL for implicit declarations of builtins. |
| 5733 | // Verify that this was correctly set up. |
| 5734 | FunctionDecl *F; |
| 5735 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5736 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5737 | F->getBuiltinID() && F->isImplicit()) |
| 5738 | assert(0 && "performing ADL for builtin"); |
| 5739 | |
| 5740 | // We don't perform ADL in C. |
| 5741 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5742 | } |
| 5743 | #endif |
| 5744 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5745 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5746 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5747 | // Add the functions denoted by the callee to the set of candidate |
| 5748 | // functions, including those from argument-dependent lookup. |
| 5749 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5750 | |
| 5751 | // If we found nothing, try to recover. |
| 5752 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5753 | // bailout out if it fails. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5754 | if (CandidateSet.empty()) |
Douglas Gregor | 1aae80b | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5755 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5756 | CommaLocs, RParenLoc); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5757 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5758 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5759 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5760 | case OR_Success: { |
| 5761 | FunctionDecl *FDecl = Best->Function; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5762 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5763 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5764 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5765 | } |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5766 | |
| 5767 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5768 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5769 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5770 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5771 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5772 | break; |
| 5773 | |
| 5774 | case OR_Ambiguous: |
| 5775 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5776 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5777 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5778 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5779 | |
| 5780 | case OR_Deleted: |
| 5781 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5782 | << Best->Function->isDeleted() |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5783 | << ULE->getName() |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5784 | << Fn->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5785 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5786 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5790 | // return NULL. |
| 5791 | Fn->Destroy(Context); |
| 5792 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5793 | Args[Arg]->Destroy(Context); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5794 | return ExprError(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5797 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5798 | return Functions.size() > 1 || |
| 5799 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5800 | } |
| 5801 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5802 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5803 | /// operator. |
| 5804 | /// |
| 5805 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5806 | /// |
| 5807 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5808 | /// operator. |
| 5809 | /// |
| 5810 | /// \param Functions The set of non-member functions that will be |
| 5811 | /// considered by overload resolution. The caller needs to build this |
| 5812 | /// set based on the context using, e.g., |
| 5813 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5814 | /// set should not contain any member functions; those will be added |
| 5815 | /// by CreateOverloadedUnaryOp(). |
| 5816 | /// |
| 5817 | /// \param input The input argument. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5818 | Sema::OwningExprResult |
| 5819 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 5820 | const UnresolvedSetImpl &Fns, |
| 5821 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5822 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5823 | Expr *Input = (Expr *)input.get(); |
| 5824 | |
| 5825 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5826 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5827 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5828 | |
| 5829 | Expr *Args[2] = { Input, 0 }; |
| 5830 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5831 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5832 | // For post-increment and post-decrement, add the implicit '0' as |
| 5833 | // the second argument, so that we know this is a post-increment or |
| 5834 | // post-decrement. |
| 5835 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5836 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5837 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5838 | SourceLocation()); |
| 5839 | NumArgs = 2; |
| 5840 | } |
| 5841 | |
| 5842 | if (Input->isTypeDependent()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5843 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5844 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5845 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5846 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5847 | /*ADL*/ true, IsOverloaded(Fns)); |
| 5848 | Fn->addDecls(Fns.begin(), Fns.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5849 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5850 | input.release(); |
| 5851 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5852 | &Args[0], NumArgs, |
| 5853 | Context.DependentTy, |
| 5854 | OpLoc)); |
| 5855 | } |
| 5856 | |
| 5857 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5858 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5859 | |
| 5860 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5861 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5862 | |
| 5863 | // Add operator candidates that are member functions. |
| 5864 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5865 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5866 | // Add candidates from ADL. |
| 5867 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 5868 | Args, NumArgs, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5869 | /*ExplicitTemplateArgs*/ 0, |
| 5870 | CandidateSet); |
| 5871 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5872 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5873 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5874 | |
| 5875 | // Perform overload resolution. |
| 5876 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5877 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5878 | case OR_Success: { |
| 5879 | // We found a built-in operator or an overloaded operator. |
| 5880 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5881 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5882 | if (FnDecl) { |
| 5883 | // We matched an overloaded operator. Build a call to that |
| 5884 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5885 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5886 | // Convert the arguments. |
| 5887 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5888 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5889 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5890 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 5891 | Best->FoundDecl, Method)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5892 | return ExprError(); |
| 5893 | } else { |
| 5894 | // Convert the arguments. |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5895 | OwningExprResult InputInit |
| 5896 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5897 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5898 | SourceLocation(), |
| 5899 | move(input)); |
| 5900 | if (InputInit.isInvalid()) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5901 | return ExprError(); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5902 | |
Douglas Gregor | e1a5c17 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5903 | input = move(InputInit); |
Douglas Gregor | baecfed | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5904 | Input = (Expr *)input.get(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | // Determine the result type |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5908 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5909 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5910 | // Build the actual expression node. |
| 5911 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5912 | SourceLocation()); |
| 5913 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5915 | input.release(); |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5916 | Args[0] = Input; |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5917 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5918 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 4c3b896 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5919 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | 26a2a07 | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5920 | |
| 5921 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5922 | FnDecl)) |
| 5923 | return ExprError(); |
| 5924 | |
| 5925 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5926 | } else { |
| 5927 | // We matched a built-in operator. Convert the arguments, then |
| 5928 | // break out so that we will build the appropriate built-in |
| 5929 | // operator node. |
| 5930 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5931 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5932 | return ExprError(); |
| 5933 | |
| 5934 | break; |
| 5935 | } |
| 5936 | } |
| 5937 | |
| 5938 | case OR_No_Viable_Function: |
| 5939 | // No viable function; fall through to handling this as a |
| 5940 | // built-in operator, which will produce an error message for us. |
| 5941 | break; |
| 5942 | |
| 5943 | case OR_Ambiguous: |
| 5944 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5945 | << UnaryOperator::getOpcodeStr(Opc) |
| 5946 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5947 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5948 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5949 | return ExprError(); |
| 5950 | |
| 5951 | case OR_Deleted: |
| 5952 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5953 | << Best->Function->isDeleted() |
| 5954 | << UnaryOperator::getOpcodeStr(Opc) |
| 5955 | << Input->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5956 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5957 | return ExprError(); |
| 5958 | } |
| 5959 | |
| 5960 | // Either we found no viable overloaded operator or we matched a |
| 5961 | // built-in operator. In either case, fall through to trying to |
| 5962 | // build a built-in operation. |
| 5963 | input.release(); |
| 5964 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5965 | } |
| 5966 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5967 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5968 | /// operator. |
| 5969 | /// |
| 5970 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5971 | /// |
| 5972 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5973 | /// operator. |
| 5974 | /// |
| 5975 | /// \param Functions The set of non-member functions that will be |
| 5976 | /// considered by overload resolution. The caller needs to build this |
| 5977 | /// set based on the context using, e.g., |
| 5978 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5979 | /// set should not contain any member functions; those will be added |
| 5980 | /// by CreateOverloadedBinOp(). |
| 5981 | /// |
| 5982 | /// \param LHS Left-hand argument. |
| 5983 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5984 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5985 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5986 | unsigned OpcIn, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5987 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5988 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5989 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5990 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5991 | |
| 5992 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5993 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5994 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5995 | |
| 5996 | // If either side is type-dependent, create an appropriate dependent |
| 5997 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5998 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5999 | if (Fns.empty()) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6000 | // If there are no functions to store, just build a dependent |
| 6001 | // BinaryOperator or CompoundAssignment. |
| 6002 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 6003 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6004 | Context.DependentTy, OpLoc)); |
| 6005 | |
| 6006 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6007 | Context.DependentTy, |
| 6008 | Context.DependentTy, |
| 6009 | Context.DependentTy, |
| 6010 | OpLoc)); |
| 6011 | } |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6012 | |
| 6013 | // FIXME: save results of ADL from here? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6014 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6015 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6016 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6017 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6018 | /*ADL*/ true, IsOverloaded(Fns)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6019 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6020 | Fn->addDecls(Fns.begin(), Fns.end()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6021 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6022 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6023 | Context.DependentTy, |
| 6024 | OpLoc)); |
| 6025 | } |
| 6026 | |
| 6027 | // If this is the .* operator, which is not overloadable, just |
| 6028 | // create a built-in binary operator. |
| 6029 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6030 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6031 | |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6032 | // If this is the assignment operator, we only perform overload resolution |
| 6033 | // if the left-hand side is a class or enumeration type. This is actually |
| 6034 | // a hack. The standard requires that we do overload resolution between the |
| 6035 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6036 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6037 | // Note that we go the traditional code path for compound assignment forms. |
| 6038 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6039 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6040 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6041 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6042 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6043 | |
| 6044 | // Add the candidates from the given function set. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6045 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6046 | |
| 6047 | // Add operator candidates that are member functions. |
| 6048 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6049 | |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6050 | // Add candidates from ADL. |
| 6051 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6052 | Args, 2, |
| 6053 | /*ExplicitTemplateArgs*/ 0, |
| 6054 | CandidateSet); |
| 6055 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6056 | // Add builtin operator candidates. |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6057 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6058 | |
| 6059 | // Perform overload resolution. |
| 6060 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6061 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6062 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6063 | // We found a built-in operator or an overloaded operator. |
| 6064 | FunctionDecl *FnDecl = Best->Function; |
| 6065 | |
| 6066 | if (FnDecl) { |
| 6067 | // We matched an overloaded operator. Build a call to that |
| 6068 | // operator. |
| 6069 | |
| 6070 | // Convert the arguments. |
| 6071 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6072 | // Best->Access is only meaningful for class members. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6073 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 5357b61 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6074 | |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6075 | OwningExprResult Arg1 |
| 6076 | = PerformCopyInitialization( |
| 6077 | InitializedEntity::InitializeParameter( |
| 6078 | FnDecl->getParamDecl(0)), |
| 6079 | SourceLocation(), |
| 6080 | Owned(Args[1])); |
| 6081 | if (Arg1.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6082 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6083 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6084 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6085 | Best->FoundDecl, Method)) |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6086 | return ExprError(); |
| 6087 | |
| 6088 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6089 | } else { |
| 6090 | // Convert the arguments. |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6091 | OwningExprResult Arg0 |
| 6092 | = PerformCopyInitialization( |
| 6093 | InitializedEntity::InitializeParameter( |
| 6094 | FnDecl->getParamDecl(0)), |
| 6095 | SourceLocation(), |
| 6096 | Owned(Args[0])); |
| 6097 | if (Arg0.isInvalid()) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6098 | return ExprError(); |
Douglas Gregor | 4c2458a | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6099 | |
| 6100 | OwningExprResult Arg1 |
| 6101 | = PerformCopyInitialization( |
| 6102 | InitializedEntity::InitializeParameter( |
| 6103 | FnDecl->getParamDecl(1)), |
| 6104 | SourceLocation(), |
| 6105 | Owned(Args[1])); |
| 6106 | if (Arg1.isInvalid()) |
| 6107 | return ExprError(); |
| 6108 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 6109 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | // Determine the result type |
| 6113 | QualType ResultTy |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6114 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6115 | ResultTy = ResultTy.getNonReferenceType(); |
| 6116 | |
| 6117 | // Build the actual expression node. |
| 6118 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 6119 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6120 | UsualUnaryConversions(FnExpr); |
| 6121 | |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6122 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6123 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 6124 | Args, 2, ResultTy, |
| 6125 | OpLoc)); |
| 6126 | |
| 6127 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6128 | FnDecl)) |
| 6129 | return ExprError(); |
| 6130 | |
| 6131 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6132 | } else { |
| 6133 | // We matched a built-in operator. Convert the arguments, then |
| 6134 | // break out so that we will build the appropriate built-in |
| 6135 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6136 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6137 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6138 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6139 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6140 | return ExprError(); |
| 6141 | |
| 6142 | break; |
| 6143 | } |
| 6144 | } |
| 6145 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6146 | case OR_No_Viable_Function: { |
| 6147 | // C++ [over.match.oper]p9: |
| 6148 | // If the operator is the operator , [...] and there are no |
| 6149 | // viable functions, then the operator is assumed to be the |
| 6150 | // built-in operator and interpreted according to clause 5. |
| 6151 | if (Opc == BinaryOperator::Comma) |
| 6152 | break; |
| 6153 | |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6154 | // For class as left operand for assignment or compound assigment operator |
| 6155 | // do not fall through to handling in built-in, but report that no overloaded |
| 6156 | // assignment operator found |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6157 | OwningExprResult Result = ExprError(); |
| 6158 | if (Args[0]->getType()->isRecordType() && |
| 6159 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6160 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6161 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6162 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6163 | } else { |
| 6164 | // No viable function; try to create a built-in operation, which will |
| 6165 | // produce an error. Then, show the non-viable candidates. |
| 6166 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6167 | } |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6168 | assert(Result.isInvalid() && |
| 6169 | "C++ binary operator overloading is missing candidates!"); |
| 6170 | if (Result.isInvalid()) |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6171 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6172 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6173 | return move(Result); |
| 6174 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6175 | |
| 6176 | case OR_Ambiguous: |
| 6177 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6178 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6179 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6180 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | 2ebe7eb | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6181 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6182 | return ExprError(); |
| 6183 | |
| 6184 | case OR_Deleted: |
| 6185 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6186 | << Best->Function->isDeleted() |
| 6187 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6188 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6189 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6190 | return ExprError(); |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6191 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6192 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6193 | // We matched a built-in operator; build it. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6194 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6195 | } |
| 6196 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6197 | Action::OwningExprResult |
| 6198 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 6199 | SourceLocation RLoc, |
| 6200 | ExprArg Base, ExprArg Idx) { |
| 6201 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 6202 | static_cast<Expr*>(Idx.get()) }; |
| 6203 | DeclarationName OpName = |
| 6204 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 6205 | |
| 6206 | // If either side is type-dependent, create an appropriate dependent |
| 6207 | // expression. |
| 6208 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 6209 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6210 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6211 | UnresolvedLookupExpr *Fn |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6212 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6213 | 0, SourceRange(), OpName, LLoc, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6214 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6215 | // Can't add any actual overloads yet |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6216 | |
| 6217 | Base.release(); |
| 6218 | Idx.release(); |
| 6219 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 6220 | Args, 2, |
| 6221 | Context.DependentTy, |
| 6222 | RLoc)); |
| 6223 | } |
| 6224 | |
| 6225 | // Build an empty overload set. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6226 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6227 | |
| 6228 | // Subscript can only be overloaded as a member function. |
| 6229 | |
| 6230 | // Add operator candidates that are member functions. |
| 6231 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6232 | |
| 6233 | // Add builtin operator candidates. |
| 6234 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6235 | |
| 6236 | // Perform overload resolution. |
| 6237 | OverloadCandidateSet::iterator Best; |
| 6238 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 6239 | case OR_Success: { |
| 6240 | // We found a built-in operator or an overloaded operator. |
| 6241 | FunctionDecl *FnDecl = Best->Function; |
| 6242 | |
| 6243 | if (FnDecl) { |
| 6244 | // We matched an overloaded operator. Build a call to that |
| 6245 | // operator. |
| 6246 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6247 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6248 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6249 | // Convert the arguments. |
| 6250 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6251 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6252 | Best->FoundDecl, Method)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6253 | return ExprError(); |
| 6254 | |
Anders Carlsson | 38f88ab | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 6255 | // Convert the arguments. |
| 6256 | OwningExprResult InputInit |
| 6257 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6258 | FnDecl->getParamDecl(0)), |
| 6259 | SourceLocation(), |
| 6260 | Owned(Args[1])); |
| 6261 | if (InputInit.isInvalid()) |
| 6262 | return ExprError(); |
| 6263 | |
| 6264 | Args[1] = InputInit.takeAs<Expr>(); |
| 6265 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6266 | // Determine the result type |
| 6267 | QualType ResultTy |
| 6268 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 6269 | ResultTy = ResultTy.getNonReferenceType(); |
| 6270 | |
| 6271 | // Build the actual expression node. |
| 6272 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6273 | LLoc); |
| 6274 | UsualUnaryConversions(FnExpr); |
| 6275 | |
| 6276 | Base.release(); |
| 6277 | Idx.release(); |
| 6278 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6279 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 6280 | FnExpr, Args, 2, |
| 6281 | ResultTy, RLoc)); |
| 6282 | |
| 6283 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 6284 | FnDecl)) |
| 6285 | return ExprError(); |
| 6286 | |
| 6287 | return MaybeBindToTemporary(TheCall.release()); |
| 6288 | } else { |
| 6289 | // We matched a built-in operator. Convert the arguments, then |
| 6290 | // break out so that we will build the appropriate built-in |
| 6291 | // operator node. |
| 6292 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6293 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6294 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6295 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6296 | return ExprError(); |
| 6297 | |
| 6298 | break; |
| 6299 | } |
| 6300 | } |
| 6301 | |
| 6302 | case OR_No_Viable_Function: { |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6303 | if (CandidateSet.empty()) |
| 6304 | Diag(LLoc, diag::err_ovl_no_oper) |
| 6305 | << Args[0]->getType() << /*subscript*/ 0 |
| 6306 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 6307 | else |
| 6308 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 6309 | << Args[0]->getType() |
| 6310 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6311 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6312 | "[]", LLoc); |
| 6313 | return ExprError(); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6314 | } |
| 6315 | |
| 6316 | case OR_Ambiguous: |
| 6317 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 6318 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6319 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6320 | "[]", LLoc); |
| 6321 | return ExprError(); |
| 6322 | |
| 6323 | case OR_Deleted: |
| 6324 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 6325 | << Best->Function->isDeleted() << "[]" |
| 6326 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6327 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6328 | "[]", LLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6329 | return ExprError(); |
| 6330 | } |
| 6331 | |
| 6332 | // We matched a built-in operator; build it. |
| 6333 | Base.release(); |
| 6334 | Idx.release(); |
| 6335 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 6336 | Owned(Args[1]), RLoc); |
| 6337 | } |
| 6338 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6339 | /// BuildCallToMemberFunction - Build a call to a member |
| 6340 | /// function. MemExpr is the expression that refers to the member |
| 6341 | /// function (and includes the object parameter), Args/NumArgs are the |
| 6342 | /// arguments to the function call (not including the object |
| 6343 | /// parameter). The caller needs to validate that the member |
| 6344 | /// expression refers to a member function or an overloaded member |
| 6345 | /// function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6346 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6347 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 6348 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6349 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 6350 | SourceLocation RParenLoc) { |
| 6351 | // Dig out the member expression. This holds both the object |
| 6352 | // argument and the member function we're referring to. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6353 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6354 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6355 | MemberExpr *MemExpr; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6356 | CXXMethodDecl *Method = 0; |
John McCall | bb6fb46 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 6357 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6358 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6359 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6360 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6361 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6362 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6363 | Qualifier = MemExpr->getQualifier(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6364 | } else { |
| 6365 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6366 | Qualifier = UnresExpr->getQualifier(); |
| 6367 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6368 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6369 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6370 | // Add overload candidates |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6371 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6372 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6373 | // FIXME: avoid copy. |
| 6374 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6375 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6376 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6377 | TemplateArgs = &TemplateArgsBuffer; |
| 6378 | } |
| 6379 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6380 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6381 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6382 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6383 | NamedDecl *Func = *I; |
| 6384 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6385 | if (isa<UsingShadowDecl>(Func)) |
| 6386 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6387 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6388 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6389 | // If explicit template arguments were provided, we can't call a |
| 6390 | // non-template member function. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6391 | if (TemplateArgs) |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6392 | continue; |
| 6393 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6394 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6395 | Args, NumArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6396 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6397 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6398 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6399 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6400 | ObjectType, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6401 | CandidateSet, |
| 6402 | /*SuppressUsedConversions=*/false); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6403 | } |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6404 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6405 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6406 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6407 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6408 | OverloadCandidateSet::iterator Best; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6409 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6410 | case OR_Success: |
| 6411 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6412 | FoundDecl = Best->FoundDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6413 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6414 | break; |
| 6415 | |
| 6416 | case OR_No_Viable_Function: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6417 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6418 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6419 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6420 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6421 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6422 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6423 | |
| 6424 | case OR_Ambiguous: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6425 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6426 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6427 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6428 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6429 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6430 | |
| 6431 | case OR_Deleted: |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6432 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6433 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6434 | << DeclName << MemExprE->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6435 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6436 | // FIXME: Leaking incoming expressions! |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6437 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6440 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6441 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6442 | // If overload resolution picked a static member, build a |
| 6443 | // non-member call based on that function. |
| 6444 | if (Method->isStatic()) { |
| 6445 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6446 | Args, NumArgs, RParenLoc); |
| 6447 | } |
| 6448 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6449 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6453 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6454 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6455 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6456 | Method->getResultType().getNonReferenceType(), |
| 6457 | RParenLoc)); |
| 6458 | |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6459 | // Check for a valid return type. |
| 6460 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6461 | TheCall.get(), Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6462 | return ExprError(); |
Anders Carlsson | eed3e69 | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6463 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6464 | // Convert the object argument (for a non-static member function call). |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6465 | // We only need to do this if there was actually an overload; otherwise |
| 6466 | // it was done at lookup. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6467 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6468 | if (!Method->isStatic() && |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6469 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 6470 | FoundDecl, Method)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6471 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6472 | MemExpr->setBase(ObjectArg); |
| 6473 | |
| 6474 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6475 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6476 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6477 | RParenLoc)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6478 | return ExprError(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6479 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6480 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6481 | return ExprError(); |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 6482 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6483 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6486 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6487 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6488 | /// overloaded function call operator (@c operator()) or performing a |
| 6489 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6490 | Sema::ExprResult |
| 6491 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 6492 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6493 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6494 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6495 | SourceLocation RParenLoc) { |
| 6496 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6497 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6498 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6499 | // C++ [over.call.object]p1: |
| 6500 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6501 | // 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] | 6502 | // candidate functions includes at least the function call |
| 6503 | // operators of T. The function call operators of T are obtained by |
| 6504 | // ordinary lookup of the name operator() in the context of |
| 6505 | // (E).operator(). |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6506 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6507 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6508 | |
| 6509 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6510 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6511 | << Object->getSourceRange())) |
| 6512 | return true; |
| 6513 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6514 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6515 | LookupQualifiedName(R, Record->getDecl()); |
| 6516 | R.suppressDiagnostics(); |
| 6517 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6518 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6519 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6520 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6521 | Args, NumArgs, CandidateSet, |
John McCall | 314be4e | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6522 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 3734c21 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6523 | } |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6524 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6525 | // C++ [over.call.object]p2: |
| 6526 | // In addition, for each conversion function declared in T of the |
| 6527 | // form |
| 6528 | // |
| 6529 | // operator conversion-type-id () cv-qualifier; |
| 6530 | // |
| 6531 | // where cv-qualifier is the same cv-qualification as, or a |
| 6532 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 6533 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6534 | // R", or the type "reference to pointer to function of |
| 6535 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6536 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6537 | // is also considered as a candidate function. Similarly, |
| 6538 | // surrogate call functions are added to the set of candidate |
| 6539 | // functions for each conversion function declared in an |
| 6540 | // accessible base class provided the function is not hidden |
| 6541 | // within T by another intervening declaration. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6542 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 6543 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6544 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6545 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6546 | NamedDecl *D = *I; |
| 6547 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6548 | if (isa<UsingShadowDecl>(D)) |
| 6549 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6550 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6551 | // Skip over templated conversion functions; they aren't |
| 6552 | // surrogates. |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6553 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6554 | continue; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6555 | |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6556 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6557 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6558 | // Strip the reference type (if any) and then the pointer type (if |
| 6559 | // any) to get down to what might be a function type. |
| 6560 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6561 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6562 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6563 | |
Douglas Gregor | 4a27d70 | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6564 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6565 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6566 | Object->getType(), Args, NumArgs, |
| 6567 | CandidateSet); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6569 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6570 | // Perform overload resolution. |
| 6571 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6572 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6573 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6574 | // Overload resolution succeeded; we'll build the appropriate call |
| 6575 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6576 | break; |
| 6577 | |
| 6578 | case OR_No_Viable_Function: |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6579 | if (CandidateSet.empty()) |
| 6580 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6581 | << Object->getType() << /*call*/ 1 |
| 6582 | << Object->getSourceRange(); |
| 6583 | else |
| 6584 | Diag(Object->getSourceRange().getBegin(), |
| 6585 | diag::err_ovl_no_viable_object_call) |
| 6586 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6587 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6588 | break; |
| 6589 | |
| 6590 | case OR_Ambiguous: |
| 6591 | Diag(Object->getSourceRange().getBegin(), |
| 6592 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6593 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6594 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6595 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6596 | |
| 6597 | case OR_Deleted: |
| 6598 | Diag(Object->getSourceRange().getBegin(), |
| 6599 | diag::err_ovl_deleted_object_call) |
| 6600 | << Best->Function->isDeleted() |
| 6601 | << Object->getType() << Object->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6602 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6603 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6604 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6605 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6606 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6607 | // We had an error; delete all of the subexpressions and return |
| 6608 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6609 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6610 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6611 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6612 | return true; |
| 6613 | } |
| 6614 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6615 | if (Best->Function == 0) { |
| 6616 | // Since there is no function declaration, this is one of the |
| 6617 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6618 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6619 | = cast<CXXConversionDecl>( |
| 6620 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6621 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6622 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6623 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6624 | // We selected one of the surrogate functions that converts the |
| 6625 | // object parameter to a function pointer. Perform the conversion |
| 6626 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6627 | |
| 6628 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6629 | // and then call it. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6630 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 6631 | Conv); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6632 | |
Fariborz Jahanian | d8307b1 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6633 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6634 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
Douglas Gregor | aa0be17 | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 6635 | CommaLocs, RParenLoc).result(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6638 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 41d8903 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6639 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6640 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6641 | // that calls this method, using Object for the implicit object |
| 6642 | // parameter and passing along the remaining arguments. |
| 6643 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6644 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6645 | |
| 6646 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6647 | unsigned NumArgsToCheck = NumArgs; |
| 6648 | |
| 6649 | // Build the full argument list for the method call (the |
| 6650 | // implicit object parameter is placed at the beginning of the |
| 6651 | // list). |
| 6652 | Expr **MethodArgs; |
| 6653 | if (NumArgs < NumArgsInProto) { |
| 6654 | NumArgsToCheck = NumArgsInProto; |
| 6655 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6656 | } else { |
| 6657 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6658 | } |
| 6659 | MethodArgs[0] = Object; |
| 6660 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6661 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6662 | |
| 6663 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6664 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6665 | UsualUnaryConversions(NewFn); |
| 6666 | |
| 6667 | // Once we've built TheCall, all of the expressions are properly |
| 6668 | // owned. |
| 6669 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6670 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6671 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6672 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6673 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6674 | delete [] MethodArgs; |
| 6675 | |
Anders Carlsson | 07d68f1 | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6676 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6677 | Method)) |
| 6678 | return true; |
| 6679 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6680 | // We may have default arguments. If so, we need to allocate more |
| 6681 | // slots in the call for them. |
| 6682 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6683 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6684 | else if (NumArgs > NumArgsInProto) |
| 6685 | NumArgsToCheck = NumArgsInProto; |
| 6686 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6687 | bool IsError = false; |
| 6688 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6689 | // Initialize the implicit object parameter. |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6690 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6691 | Best->FoundDecl, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6692 | TheCall->setArg(0, Object); |
| 6693 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6695 | // Check the argument types. |
| 6696 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6697 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6698 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6699 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6700 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6701 | // Pass the argument. |
Anders Carlsson | 3faa486 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 6702 | |
| 6703 | OwningExprResult InputInit |
| 6704 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6705 | Method->getParamDecl(i)), |
| 6706 | SourceLocation(), Owned(Arg)); |
| 6707 | |
| 6708 | IsError |= InputInit.isInvalid(); |
| 6709 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6710 | } else { |
Douglas Gregor | d47c47d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 6711 | OwningExprResult DefArg |
| 6712 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6713 | if (DefArg.isInvalid()) { |
| 6714 | IsError = true; |
| 6715 | break; |
| 6716 | } |
| 6717 | |
| 6718 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6719 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6720 | |
| 6721 | TheCall->setArg(i + 1, Arg); |
| 6722 | } |
| 6723 | |
| 6724 | // If this is a variadic call, handle args passed through "...". |
| 6725 | if (Proto->isVariadic()) { |
| 6726 | // Promote the arguments (C99 6.5.2.2p7). |
| 6727 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6728 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6729 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6730 | TheCall->setArg(i + 1, Arg); |
| 6731 | } |
| 6732 | } |
| 6733 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6734 | if (IsError) return true; |
| 6735 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6736 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6737 | return true; |
| 6738 | |
Douglas Gregor | aa0be17 | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 6739 | return MaybeBindToTemporary(TheCall.release()).result(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6742 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6743 | /// (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] | 6744 | /// @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] | 6745 | Sema::OwningExprResult |
| 6746 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6747 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6748 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6749 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6750 | SourceLocation Loc = Base->getExprLoc(); |
| 6751 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6752 | // C++ [over.ref]p1: |
| 6753 | // |
| 6754 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6755 | // for a class object x of type T if T::operator->() exists and if |
| 6756 | // the operator is selected as the best match function by the |
| 6757 | // overload resolution mechanism (13.3). |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6758 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6759 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6760 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6761 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6762 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | f43fb72 | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 6763 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6764 | << Base->getSourceRange())) |
| 6765 | return ExprError(); |
| 6766 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6767 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6768 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6769 | R.suppressDiagnostics(); |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6770 | |
| 6771 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6772 | Oper != OperEnd; ++Oper) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6773 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6774 | /*SuppressUserConversions=*/false); |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6775 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6776 | |
| 6777 | // Perform overload resolution. |
| 6778 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6779 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6780 | case OR_Success: |
| 6781 | // Overload resolution succeeded; we'll build the call below. |
| 6782 | break; |
| 6783 | |
| 6784 | case OR_No_Viable_Function: |
| 6785 | if (CandidateSet.empty()) |
| 6786 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6787 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6788 | else |
| 6789 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6790 | << "operator->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6791 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6792 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6793 | |
| 6794 | case OR_Ambiguous: |
| 6795 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6796 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6797 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6798 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6799 | |
| 6800 | case OR_Deleted: |
| 6801 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6802 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6803 | << "->" << Base->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6804 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6805 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6808 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 6809 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6810 | // Convert the object parameter. |
| 6811 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6812 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 6813 | Best->FoundDecl, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6814 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6815 | |
| 6816 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6817 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6818 | |
| 6819 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6820 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6821 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6822 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6823 | |
| 6824 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6825 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6826 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6827 | &Base, 1, ResultTy, OpLoc)); |
| 6828 | |
| 6829 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6830 | Method)) |
| 6831 | return ExprError(); |
| 6832 | return move(TheCall); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6833 | } |
| 6834 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6835 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6836 | /// a C++ overloaded function (possibly with some parentheses and |
| 6837 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6838 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6839 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 6840 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6841 | FunctionDecl *Fn) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6842 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6843 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 6844 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6845 | if (SubExpr == PE->getSubExpr()) |
| 6846 | return PE->Retain(); |
| 6847 | |
| 6848 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6849 | } |
| 6850 | |
| 6851 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6852 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 6853 | Found, Fn); |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6854 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6855 | SubExpr->getType()) && |
Douglas Gregor | 097bfb1 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6856 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6857 | if (SubExpr == ICE->getSubExpr()) |
| 6858 | return ICE->Retain(); |
| 6859 | |
| 6860 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6861 | ICE->getCastKind(), |
| 6862 | SubExpr, |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame^] | 6863 | /*InheritancePath=*/0, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6864 | ICE->isLvalueCast()); |
| 6865 | } |
| 6866 | |
| 6867 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6868 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6869 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6870 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6871 | if (Method->isStatic()) { |
| 6872 | // Do nothing: static member functions aren't any different |
| 6873 | // from non-member functions. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6874 | } else { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6875 | // Fix the sub expression, which really has to be an |
| 6876 | // UnresolvedLookupExpr holding an overloaded member function |
| 6877 | // or template. |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6878 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 6879 | Found, Fn); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6880 | if (SubExpr == UnOp->getSubExpr()) |
| 6881 | return UnOp->Retain(); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6882 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6883 | assert(isa<DeclRefExpr>(SubExpr) |
| 6884 | && "fixed to something other than a decl ref"); |
| 6885 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6886 | && "fixed to a member ref with no nested name qualifier"); |
| 6887 | |
| 6888 | // We have taken the address of a pointer to member |
| 6889 | // function. Perform the computation here so that we get the |
| 6890 | // appropriate pointer to member type. |
| 6891 | QualType ClassType |
| 6892 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6893 | QualType MemPtrType |
| 6894 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6895 | |
| 6896 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6897 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6898 | } |
| 6899 | } |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6900 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 6901 | Found, Fn); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6902 | if (SubExpr == UnOp->getSubExpr()) |
| 6903 | return UnOp->Retain(); |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6904 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6905 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6906 | Context.getPointerType(SubExpr->getType()), |
| 6907 | UnOp->getOperatorLoc()); |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6908 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6909 | |
| 6910 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6911 | // FIXME: avoid copy. |
| 6912 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6913 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6914 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6915 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6918 | return DeclRefExpr::Create(Context, |
| 6919 | ULE->getQualifier(), |
| 6920 | ULE->getQualifierRange(), |
| 6921 | Fn, |
| 6922 | ULE->getNameLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6923 | Fn->getType(), |
| 6924 | TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6925 | } |
| 6926 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6927 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6928 | // FIXME: avoid copy. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6929 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6930 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6931 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6932 | TemplateArgs = &TemplateArgsBuffer; |
| 6933 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6934 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6935 | Expr *Base; |
| 6936 | |
| 6937 | // If we're filling in |
| 6938 | if (MemExpr->isImplicitAccess()) { |
| 6939 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6940 | return DeclRefExpr::Create(Context, |
| 6941 | MemExpr->getQualifier(), |
| 6942 | MemExpr->getQualifierRange(), |
| 6943 | Fn, |
| 6944 | MemExpr->getMemberLoc(), |
| 6945 | Fn->getType(), |
| 6946 | TemplateArgs); |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6947 | } else { |
| 6948 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6949 | if (MemExpr->getQualifier()) |
| 6950 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6951 | Base = new (Context) CXXThisExpr(Loc, |
| 6952 | MemExpr->getBaseType(), |
| 6953 | /*isImplicit=*/true); |
| 6954 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6955 | } else |
| 6956 | Base = MemExpr->getBase()->Retain(); |
| 6957 | |
| 6958 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6959 | MemExpr->isArrow(), |
| 6960 | MemExpr->getQualifier(), |
| 6961 | MemExpr->getQualifierRange(), |
| 6962 | Fn, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6963 | Found, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6964 | MemExpr->getMemberLoc(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6965 | TemplateArgs, |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6966 | Fn->getType()); |
| 6967 | } |
| 6968 | |
Douglas Gregor | 699ee52 | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6969 | assert(false && "Invalid reference to overloaded function"); |
| 6970 | return E->Retain(); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6973 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 6974 | DeclAccessPair Found, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6975 | FunctionDecl *Fn) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6976 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6977 | } |
| 6978 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6979 | } // end namespace clang |