Douglas Gregor | 5251f1b | 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 | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 16 | #include "SemaInit.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 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 | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 41 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 42 | ICC_Qualification_Adjustment, |
| 43 | ICC_Promotion, |
| 44 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 45 | ICC_Promotion, |
| 46 | ICC_Conversion, |
| 47 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 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 | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 54 | ICC_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 55 | ICC_Conversion, |
| 56 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 57 | ICC_Conversion |
| 58 | }; |
| 59 | return Category[(int)Kind]; |
| 60 | } |
| 61 | |
| 62 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 63 | /// corresponding to the given implicit conversion kind. |
| 64 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 65 | static const ImplicitConversionRank |
| 66 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
| 70 | ICR_Exact_Match, |
| 71 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 72 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 73 | ICR_Promotion, |
| 74 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 75 | ICR_Promotion, |
| 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
| 81 | ICR_Conversion, |
| 82 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 83 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 84 | ICR_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 85 | ICR_Conversion, |
| 86 | ICR_Conversion, |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 87 | ICR_Complex_Real_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 88 | }; |
| 89 | return Rank[(int)Kind]; |
| 90 | } |
| 91 | |
| 92 | /// GetImplicitConversionName - Return the name of this kind of |
| 93 | /// implicit conversion. |
| 94 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 95 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 96 | "No conversion", |
| 97 | "Lvalue-to-rvalue", |
| 98 | "Array-to-pointer", |
| 99 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 100 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Qualification", |
| 102 | "Integral promotion", |
| 103 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 104 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 105 | "Integral conversion", |
| 106 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 107 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | "Floating-integral conversion", |
| 109 | "Pointer conversion", |
| 110 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 111 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 112 | "Compatible-types conversion", |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 113 | "Derived-to-base conversion", |
| 114 | "Vector conversion", |
| 115 | "Vector splat", |
| 116 | "Complex-real conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 117 | }; |
| 118 | return Name[Kind]; |
| 119 | } |
| 120 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 121 | /// StandardConversionSequence - Set the standard conversion |
| 122 | /// sequence to the identity conversion. |
| 123 | void StandardConversionSequence::setAsIdentityConversion() { |
| 124 | First = ICK_Identity; |
| 125 | Second = ICK_Identity; |
| 126 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 127 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 128 | ReferenceBinding = false; |
| 129 | DirectBinding = false; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 130 | RRefBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 131 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 134 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 135 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 136 | /// implicit conversions. |
| 137 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 138 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 139 | if (GetConversionRank(First) > Rank) |
| 140 | Rank = GetConversionRank(First); |
| 141 | if (GetConversionRank(Second) > Rank) |
| 142 | Rank = GetConversionRank(Second); |
| 143 | if (GetConversionRank(Third) > Rank) |
| 144 | Rank = GetConversionRank(Third); |
| 145 | return Rank; |
| 146 | } |
| 147 | |
| 148 | /// isPointerConversionToBool - Determines whether this conversion is |
| 149 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 151 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | // Note that FromType has not necessarily been transformed by the |
| 154 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 155 | // check for their presence as well as checking whether FromType is |
| 156 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 157 | if (getToType(1)->isBooleanType() && |
John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 158 | (getFromType()->isPointerType() || |
| 159 | getFromType()->isObjCObjectPointerType() || |
| 160 | getFromType()->isBlockPointerType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 161 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 162 | return true; |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 167 | /// isPointerConversionToVoidPointer - Determines whether this |
| 168 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 169 | /// used as part of the ranking of standard conversion sequences (C++ |
| 170 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 172 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 174 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 175 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 176 | |
| 177 | // Note that FromType has not necessarily been transformed by the |
| 178 | // array-to-pointer implicit conversion, so check for its presence |
| 179 | // and redo the conversion to get a pointer. |
| 180 | if (First == ICK_Array_To_Pointer) |
| 181 | FromType = Context.getArrayDecayedType(FromType); |
| 182 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 183 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 184 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 185 | return ToPtrType->getPointeeType()->isVoidType(); |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 190 | /// DebugPrint - Print this standard conversion sequence to standard |
| 191 | /// error. Useful for debugging overloading issues. |
| 192 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 193 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 194 | bool PrintedSomething = false; |
| 195 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 196 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 197 | PrintedSomething = true; |
| 198 | } |
| 199 | |
| 200 | if (Second != ICK_Identity) { |
| 201 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 202 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 203 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 204 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 205 | |
| 206 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 207 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 208 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 209 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 210 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 211 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 212 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (Third != ICK_Identity) { |
| 217 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 218 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 220 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 221 | PrintedSomething = true; |
| 222 | } |
| 223 | |
| 224 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 225 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 230 | /// error. Useful for debugging overloading issues. |
| 231 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 232 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 233 | if (Before.First || Before.Second || Before.Third) { |
| 234 | Before.DebugPrint(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 235 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 236 | } |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 237 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 238 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 239 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 240 | After.DebugPrint(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 245 | /// error. Useful for debugging overloading issues. |
| 246 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 247 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 248 | switch (ConversionKind) { |
| 249 | case StandardConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 250 | OS << "Standard conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 251 | Standard.DebugPrint(); |
| 252 | break; |
| 253 | case UserDefinedConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 254 | OS << "User-defined conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 255 | UserDefined.DebugPrint(); |
| 256 | break; |
| 257 | case EllipsisConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 258 | OS << "Ellipsis conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 259 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 260 | case AmbiguousConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 261 | OS << "Ambiguous conversion"; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 262 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 263 | case BadConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 264 | OS << "Bad conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 268 | OS << "\n"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 269 | } |
| 270 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 271 | void AmbiguousConversionSequence::construct() { |
| 272 | new (&conversions()) ConversionSet(); |
| 273 | } |
| 274 | |
| 275 | void AmbiguousConversionSequence::destruct() { |
| 276 | conversions().~ConversionSet(); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 281 | FromTypePtr = O.FromTypePtr; |
| 282 | ToTypePtr = O.ToTypePtr; |
| 283 | new (&conversions()) ConversionSet(O.conversions()); |
| 284 | } |
| 285 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 286 | namespace { |
| 287 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 288 | // template parameter and template argument information. |
| 289 | struct DFIParamWithArguments { |
| 290 | TemplateParameter Param; |
| 291 | TemplateArgument FirstArg; |
| 292 | TemplateArgument SecondArg; |
| 293 | }; |
| 294 | } |
| 295 | |
| 296 | /// \brief Convert from Sema's representation of template deduction information |
| 297 | /// to the form used in overload-candidate information. |
| 298 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 299 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 300 | Sema::TemplateDeductionResult TDK, |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 301 | Sema::TemplateDeductionInfo &Info) { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 302 | OverloadCandidate::DeductionFailureInfo Result; |
| 303 | Result.Result = static_cast<unsigned>(TDK); |
| 304 | Result.Data = 0; |
| 305 | switch (TDK) { |
| 306 | case Sema::TDK_Success: |
| 307 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 308 | case Sema::TDK_TooManyArguments: |
| 309 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 313 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 314 | Result.Data = Info.Param.getOpaqueValue(); |
| 315 | break; |
| 316 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 317 | case Sema::TDK_Inconsistent: |
| 318 | case Sema::TDK_InconsistentQuals: { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 319 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 320 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 321 | Saved->Param = Info.Param; |
| 322 | Saved->FirstArg = Info.FirstArg; |
| 323 | Saved->SecondArg = Info.SecondArg; |
| 324 | Result.Data = Saved; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 329 | Result.Data = Info.take(); |
| 330 | break; |
| 331 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 332 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 333 | case Sema::TDK_FailedOverloadResolution: |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | return Result; |
| 338 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 340 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 341 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 342 | case Sema::TDK_Success: |
| 343 | case Sema::TDK_InstantiationDepth: |
| 344 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 345 | case Sema::TDK_TooManyArguments: |
| 346 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 347 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 348 | break; |
| 349 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 350 | case Sema::TDK_Inconsistent: |
| 351 | case Sema::TDK_InconsistentQuals: |
Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 352 | // FIXME: Destroy the data? |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 353 | Data = 0; |
| 354 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 355 | |
| 356 | case Sema::TDK_SubstitutionFailure: |
| 357 | // FIXME: Destroy the template arugment list? |
| 358 | Data = 0; |
| 359 | break; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 361 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 362 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 363 | case Sema::TDK_FailedOverloadResolution: |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | TemplateParameter |
| 369 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 370 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 371 | case Sema::TDK_Success: |
| 372 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 373 | case Sema::TDK_TooManyArguments: |
| 374 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 375 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 376 | return TemplateParameter(); |
| 377 | |
| 378 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 379 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 380 | return TemplateParameter::getFromOpaqueValue(Data); |
| 381 | |
| 382 | case Sema::TDK_Inconsistent: |
| 383 | case Sema::TDK_InconsistentQuals: |
| 384 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
| 385 | |
| 386 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 387 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 388 | case Sema::TDK_FailedOverloadResolution: |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | return TemplateParameter(); |
| 393 | } |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 394 | |
| 395 | TemplateArgumentList * |
| 396 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 397 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 398 | case Sema::TDK_Success: |
| 399 | case Sema::TDK_InstantiationDepth: |
| 400 | case Sema::TDK_TooManyArguments: |
| 401 | case Sema::TDK_TooFewArguments: |
| 402 | case Sema::TDK_Incomplete: |
| 403 | case Sema::TDK_InvalidExplicitArguments: |
| 404 | case Sema::TDK_Inconsistent: |
| 405 | case Sema::TDK_InconsistentQuals: |
| 406 | return 0; |
| 407 | |
| 408 | case Sema::TDK_SubstitutionFailure: |
| 409 | return static_cast<TemplateArgumentList*>(Data); |
| 410 | |
| 411 | // Unhandled |
| 412 | case Sema::TDK_NonDeducedMismatch: |
| 413 | case Sema::TDK_FailedOverloadResolution: |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 420 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 421 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 422 | case Sema::TDK_Success: |
| 423 | case Sema::TDK_InstantiationDepth: |
| 424 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 425 | case Sema::TDK_TooManyArguments: |
| 426 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 427 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 428 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 429 | return 0; |
| 430 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 431 | case Sema::TDK_Inconsistent: |
| 432 | case Sema::TDK_InconsistentQuals: |
| 433 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
| 434 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 435 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 436 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 437 | case Sema::TDK_FailedOverloadResolution: |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | const TemplateArgument * |
| 445 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 446 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 447 | case Sema::TDK_Success: |
| 448 | case Sema::TDK_InstantiationDepth: |
| 449 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 450 | case Sema::TDK_TooManyArguments: |
| 451 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 452 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 453 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 454 | return 0; |
| 455 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 456 | case Sema::TDK_Inconsistent: |
| 457 | case Sema::TDK_InconsistentQuals: |
| 458 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 459 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 460 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 461 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 462 | case Sema::TDK_FailedOverloadResolution: |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | void OverloadCandidateSet::clear() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 470 | inherited::clear(); |
| 471 | Functions.clear(); |
| 472 | } |
| 473 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 475 | // overload of the declarations in Old. This routine returns false if |
| 476 | // New and Old cannot be overloaded, e.g., if New has the same |
| 477 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 478 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 479 | // it does return false, MatchedDecl will point to the decl that New |
| 480 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 481 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 482 | // |
| 483 | // Example: Given the following input: |
| 484 | // |
| 485 | // void f(int, float); // #1 |
| 486 | // void f(int, int); // #2 |
| 487 | // int f(int, int); // #3 |
| 488 | // |
| 489 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 492 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 493 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 494 | // (since they have different signatures), so this routine returns |
| 495 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 496 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 497 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 498 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 499 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 500 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 501 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 502 | // point to the FunctionDecl for #2. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 503 | // |
| 504 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 505 | // into a class by a using declaration. The rules for whether to hide |
| 506 | // shadow declarations ignore some properties which otherwise figure |
| 507 | // into a function template's signature. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 508 | Sema::OverloadKind |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 509 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 510 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 511 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 512 | I != E; ++I) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 513 | NamedDecl *OldD = *I; |
| 514 | |
| 515 | bool OldIsUsingDecl = false; |
| 516 | if (isa<UsingShadowDecl>(OldD)) { |
| 517 | OldIsUsingDecl = true; |
| 518 | |
| 519 | // We can always introduce two using declarations into the same |
| 520 | // context, even if they have identical signatures. |
| 521 | if (NewIsUsingDecl) continue; |
| 522 | |
| 523 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 524 | } |
| 525 | |
| 526 | // If either declaration was introduced by a using declaration, |
| 527 | // we'll need to use slightly different rules for matching. |
| 528 | // Essentially, these rules are the normal rules, except that |
| 529 | // function templates hide function templates with different |
| 530 | // return types or template parameter lists. |
| 531 | bool UseMemberUsingDeclRules = |
| 532 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 533 | |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 534 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 535 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 536 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 537 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 538 | continue; |
| 539 | } |
| 540 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 541 | Match = *I; |
| 542 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 543 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 544 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 545 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 546 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 547 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 548 | continue; |
| 549 | } |
| 550 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 551 | Match = *I; |
| 552 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 553 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 554 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 555 | // We can overload with these, which can show up when doing |
| 556 | // redeclaration checks for UsingDecls. |
| 557 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 558 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 559 | // Optimistically assume that an unresolved using decl will |
| 560 | // overload; if it doesn't, we'll have to diagnose during |
| 561 | // template instantiation. |
| 562 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 563 | // (C++ 13p1): |
| 564 | // Only function declarations can be overloaded; object and type |
| 565 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 566 | Match = *I; |
| 567 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 568 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 569 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 570 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 571 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 572 | } |
| 573 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 574 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 575 | bool UseUsingDeclRules) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 576 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 577 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 578 | |
| 579 | // C++ [temp.fct]p2: |
| 580 | // A function template can be overloaded with other function templates |
| 581 | // and with normal (non-template) functions. |
| 582 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 583 | return true; |
| 584 | |
| 585 | // Is the function New an overload of the function Old? |
| 586 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 587 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 588 | |
| 589 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 590 | // determine whether they are overloads. If we find any mismatch |
| 591 | // in the signature, they are overloads. |
| 592 | |
| 593 | // If either of these functions is a K&R-style function (no |
| 594 | // prototype), then we consider them to have matching signatures. |
| 595 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 596 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 597 | return false; |
| 598 | |
| 599 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 600 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 601 | |
| 602 | // The signature of a function includes the types of its |
| 603 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 604 | // of the ellipsis; see C++ DR 357). |
| 605 | if (OldQType != NewQType && |
| 606 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 607 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 608 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 609 | return true; |
| 610 | |
| 611 | // C++ [temp.over.link]p4: |
| 612 | // The signature of a function template consists of its function |
| 613 | // signature, its return type and its template parameter list. The names |
| 614 | // of the template parameters are significant only for establishing the |
| 615 | // relationship between the template parameters and the rest of the |
| 616 | // signature. |
| 617 | // |
| 618 | // We check the return type and template parameter lists for function |
| 619 | // templates first; the remaining checks follow. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 620 | // |
| 621 | // However, we don't consider either of these when deciding whether |
| 622 | // a member introduced by a shadow declaration is hidden. |
| 623 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 624 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 625 | OldTemplate->getTemplateParameters(), |
| 626 | false, TPL_TemplateMatch) || |
| 627 | OldType->getResultType() != NewType->getResultType())) |
| 628 | return true; |
| 629 | |
| 630 | // If the function is a class member, its signature includes the |
| 631 | // cv-qualifiers (if any) on the function itself. |
| 632 | // |
| 633 | // As part of this, also check whether one of the member functions |
| 634 | // is static, in which case they are not overloads (C++ |
| 635 | // 13.1p2). While not part of the definition of the signature, |
| 636 | // this check is important to determine whether these functions |
| 637 | // can be overloaded. |
| 638 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 639 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 640 | if (OldMethod && NewMethod && |
| 641 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 642 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 643 | return true; |
| 644 | |
| 645 | // The signatures match; this is not an overload. |
| 646 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 649 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 650 | /// from the given expression (Expr) to the given type (ToType). This |
| 651 | /// function returns an implicit conversion sequence that can be used |
| 652 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 653 | /// |
| 654 | /// void f(float f); |
| 655 | /// void g(int i) { f(i); } |
| 656 | /// |
| 657 | /// this routine would produce an implicit conversion sequence to |
| 658 | /// describe the initialization of f from i, which will be a standard |
| 659 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 660 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 661 | // |
| 662 | /// Note that this routine only determines how the conversion can be |
| 663 | /// performed; it does not actually perform the conversion. As such, |
| 664 | /// it will not produce any diagnostics if no conversion is available, |
| 665 | /// but will instead return an implicit conversion sequence of kind |
| 666 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 667 | /// |
| 668 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 669 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 670 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 671 | /// permitted. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 672 | ImplicitConversionSequence |
Anders Carlsson | 5ec4abf | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 673 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 674 | bool SuppressUserConversions, |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 675 | bool AllowExplicit, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 676 | bool InOverloadResolution) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 677 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 678 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 679 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 680 | return ICS; |
| 681 | } |
| 682 | |
| 683 | if (!getLangOptions().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 684 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 685 | return ICS; |
| 686 | } |
| 687 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 688 | if (SuppressUserConversions) { |
| 689 | // C++ [over.ics.user]p4: |
| 690 | // A conversion of an expression of class type to the same class |
| 691 | // type is given Exact Match rank, and a conversion of an |
| 692 | // expression of class type to a base class of that type is |
| 693 | // given Conversion rank, in spite of the fact that a copy/move |
| 694 | // constructor (i.e., a user-defined conversion function) is |
| 695 | // called for those cases. |
| 696 | QualType FromType = From->getType(); |
| 697 | if (!ToType->getAs<RecordType>() || !FromType->getAs<RecordType>() || |
| 698 | !(Context.hasSameUnqualifiedType(FromType, ToType) || |
| 699 | IsDerivedFrom(FromType, ToType))) { |
| 700 | // We're not in the case above, so there is no conversion that |
| 701 | // we can perform. |
| 702 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 703 | return ICS; |
| 704 | } |
| 705 | |
| 706 | ICS.setStandard(); |
| 707 | ICS.Standard.setAsIdentityConversion(); |
| 708 | ICS.Standard.setFromType(FromType); |
| 709 | ICS.Standard.setAllToTypes(ToType); |
| 710 | |
| 711 | // We don't actually check at this point whether there is a valid |
| 712 | // copy/move constructor, since overloading just assumes that it |
| 713 | // exists. When we actually perform initialization, we'll find the |
| 714 | // appropriate constructor to copy the returned object, if needed. |
| 715 | ICS.Standard.CopyConstructor = 0; |
| 716 | |
| 717 | // Determine whether this is considered a derived-to-base conversion. |
| 718 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 719 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 720 | |
| 721 | return ICS; |
| 722 | } |
| 723 | |
| 724 | // Attempt user-defined conversion. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 725 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 726 | OverloadingResult UserDefResult |
| 727 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 728 | AllowExplicit); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 729 | |
| 730 | if (UserDefResult == OR_Success) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 731 | ICS.setUserDefined(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 732 | // C++ [over.ics.user]p4: |
| 733 | // A conversion of an expression of class type to the same class |
| 734 | // type is given Exact Match rank, and a conversion of an |
| 735 | // expression of class type to a base class of that type is |
| 736 | // given Conversion rank, in spite of the fact that a copy |
| 737 | // constructor (i.e., a user-defined conversion function) is |
| 738 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 740 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | QualType FromCanon |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 742 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 743 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 744 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 4141d5b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 745 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 746 | // Turn this into a "standard" conversion sequence, so that it |
| 747 | // gets ranked with standard conversion sequences. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 748 | ICS.setStandard(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 749 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 750 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 751 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 752 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 753 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 754 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 755 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 756 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 757 | |
| 758 | // C++ [over.best.ics]p4: |
| 759 | // However, when considering the argument of a user-defined |
| 760 | // conversion function that is a candidate by 13.3.1.3 when |
| 761 | // invoked for the copying of the temporary in the second step |
| 762 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 763 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 764 | // ellipsis conversion sequences are allowed. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 765 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 766 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 767 | } |
John McCall | e8c8cd2 | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 768 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 769 | ICS.setAmbiguous(); |
| 770 | ICS.Ambiguous.setFromType(From->getType()); |
| 771 | ICS.Ambiguous.setToType(ToType); |
| 772 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 773 | Cand != Conversions.end(); ++Cand) |
| 774 | if (Cand->Viable) |
| 775 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 776 | } else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 777 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 778 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 779 | |
| 780 | return ICS; |
| 781 | } |
| 782 | |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 783 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 784 | /// expression From to the type ToType. Returns true if there was an |
| 785 | /// error, false otherwise. The expression From is replaced with the |
| 786 | /// converted expression. Flavor is the kind of conversion we're |
| 787 | /// performing, used in the error message. If @p AllowExplicit, |
| 788 | /// explicit user-defined conversions are permitted. |
| 789 | bool |
| 790 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 791 | AssignmentAction Action, bool AllowExplicit) { |
| 792 | ImplicitConversionSequence ICS; |
| 793 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 794 | } |
| 795 | |
| 796 | bool |
| 797 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 798 | AssignmentAction Action, bool AllowExplicit, |
| 799 | ImplicitConversionSequence& ICS) { |
| 800 | ICS = TryImplicitConversion(From, ToType, |
| 801 | /*SuppressUserConversions=*/false, |
| 802 | AllowExplicit, |
| 803 | /*InOverloadResolution=*/false); |
| 804 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 805 | } |
| 806 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 807 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 808 | /// conversion that strips "noreturn" off the nested function type. |
| 809 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 810 | QualType ToType, QualType &ResultTy) { |
| 811 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 812 | return false; |
| 813 | |
| 814 | // Strip the noreturn off the type we're converting from; noreturn can |
| 815 | // safely be removed. |
| 816 | FromType = Context.getNoReturnType(FromType, false); |
| 817 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 818 | return false; |
| 819 | |
| 820 | ResultTy = FromType; |
| 821 | return true; |
| 822 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 823 | |
| 824 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 825 | /// vector conversion. |
| 826 | /// |
| 827 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 828 | /// conversion. |
| 829 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 830 | QualType ToType, ImplicitConversionKind &ICK) { |
| 831 | // We need at least one of these types to be a vector type to have a vector |
| 832 | // conversion. |
| 833 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 834 | return false; |
| 835 | |
| 836 | // Identical types require no conversions. |
| 837 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 838 | return false; |
| 839 | |
| 840 | // There are no conversions between extended vector types, only identity. |
| 841 | if (ToType->isExtVectorType()) { |
| 842 | // There are no conversions between extended vector types other than the |
| 843 | // identity conversion. |
| 844 | if (FromType->isExtVectorType()) |
| 845 | return false; |
| 846 | |
| 847 | // Vector splat from any arithmetic type to a vector. |
| 848 | if (!FromType->isVectorType() && FromType->isArithmeticType()) { |
| 849 | ICK = ICK_Vector_Splat; |
| 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | // If lax vector conversions are permitted and the vector types are of the |
| 855 | // same size, we can perform the conversion. |
| 856 | if (Context.getLangOptions().LaxVectorConversions && |
| 857 | FromType->isVectorType() && ToType->isVectorType() && |
| 858 | Context.getTypeSize(FromType) == Context.getTypeSize(ToType)) { |
| 859 | ICK = ICK_Vector_Conversion; |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | return false; |
| 864 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 865 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 866 | /// IsStandardConversion - Determines whether there is a standard |
| 867 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 868 | /// expression From to the type ToType. Standard conversion sequences |
| 869 | /// only consider non-class types; for conversions that involve class |
| 870 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 871 | /// contain the standard conversion sequence required to perform this |
| 872 | /// conversion and this routine will return true. Otherwise, this |
| 873 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | bool |
| 875 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 876 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 878 | QualType FromType = From->getType(); |
| 879 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 880 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 881 | SCS.setAsIdentityConversion(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 882 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 883 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 884 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 885 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 887 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 889 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 890 | if (getLangOptions().CPlusPlus) |
| 891 | return false; |
| 892 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 896 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 897 | // array-to-pointer conversion, or function-to-pointer conversion |
| 898 | // (C++ 4p1). |
| 899 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 900 | if (FromType == Context.OverloadTy) { |
| 901 | DeclAccessPair AccessPair; |
| 902 | if (FunctionDecl *Fn |
| 903 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 904 | AccessPair)) { |
| 905 | // We were able to resolve the address of the overloaded function, |
| 906 | // so we can convert to the type of that function. |
| 907 | FromType = Fn->getType(); |
| 908 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 909 | if (!Method->isStatic()) { |
| 910 | Type *ClassType |
| 911 | = Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 912 | FromType = Context.getMemberPointerType(FromType, ClassType); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // If the "from" expression takes the address of the overloaded |
| 917 | // function, update the type of the resulting expression accordingly. |
| 918 | if (FromType->getAs<FunctionType>()) |
| 919 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(From->IgnoreParens())) |
| 920 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 921 | FromType = Context.getPointerType(FromType); |
| 922 | |
| 923 | // Check that we've computed the proper type after overload resolution. |
| 924 | assert(Context.hasSameType(FromType, |
| 925 | FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
| 926 | } else { |
| 927 | return false; |
| 928 | } |
| 929 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 931 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 932 | // converted to an rvalue. |
| 933 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 935 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 936 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 937 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 938 | |
| 939 | // If T is a non-class type, the type of the rvalue is the |
| 940 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 941 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 942 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 943 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 944 | } else if (FromType->isArrayType()) { |
| 945 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 946 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 947 | |
| 948 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 949 | // bound of T" can be converted to an rvalue of type "pointer to |
| 950 | // T" (C++ 4.2p1). |
| 951 | FromType = Context.getArrayDecayedType(FromType); |
| 952 | |
| 953 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 954 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 955 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 956 | |
| 957 | // For the purpose of ranking in overload resolution |
| 958 | // (13.3.3.1.1), this conversion is considered an |
| 959 | // array-to-pointer conversion followed by a qualification |
| 960 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 961 | SCS.Second = ICK_Identity; |
| 962 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 963 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 964 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 965 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 966 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 967 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 968 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 969 | |
| 970 | // An lvalue of function type T can be converted to an rvalue of |
| 971 | // type "pointer to T." The result is a pointer to the |
| 972 | // function. (C++ 4.3p1). |
| 973 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 974 | } else { |
| 975 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 976 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 977 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 978 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 979 | |
| 980 | // The second conversion can be an integral promotion, floating |
| 981 | // point promotion, integral conversion, floating point conversion, |
| 982 | // floating-integral conversion, pointer conversion, |
| 983 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 984 | // For overloading in C, this can also be a "compatible-type" |
| 985 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 986 | bool IncompatibleObjC = false; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 987 | ImplicitConversionKind SecondICK = ICK_Identity; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 988 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 989 | // The unqualified versions of the types are the same: there's no |
| 990 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 991 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 992 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 994 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 995 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 996 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 997 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 998 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 999 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1000 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 1001 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1002 | SCS.Second = ICK_Complex_Promotion; |
| 1003 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1004 | } else if (FromType->isIntegralOrEnumerationType() && |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1005 | ToType->isIntegralType(Context)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1006 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1007 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1008 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1009 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 1010 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1011 | SCS.Second = ICK_Complex_Conversion; |
| 1012 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1013 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 1014 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 1015 | // Complex-real conversions (C99 6.3.1.7) |
| 1016 | SCS.Second = ICK_Complex_Real; |
| 1017 | FromType = ToType.getUnqualifiedType(); |
| 1018 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 1019 | // Floating point conversions (C++ 4.8). |
| 1020 | SCS.Second = ICK_Floating_Conversion; |
| 1021 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1022 | } else if ((FromType->isFloatingType() && |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1023 | ToType->isIntegralType(Context) && !ToType->isBooleanType()) || |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1024 | (FromType->isIntegralOrEnumerationType() && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1025 | ToType->isFloatingType())) { |
| 1026 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1027 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1028 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1029 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1030 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1031 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1032 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1033 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1034 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 1035 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1036 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1037 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1038 | } else if (ToType->isBooleanType() && |
| 1039 | (FromType->isArithmeticType() || |
| 1040 | FromType->isEnumeralType() || |
Fariborz Jahanian | 8811885 | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 1041 | FromType->isAnyPointerType() || |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1042 | FromType->isBlockPointerType() || |
| 1043 | FromType->isMemberPointerType() || |
| 1044 | FromType->isNullPtrType())) { |
| 1045 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1046 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1047 | FromType = Context.BoolTy; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1048 | } else if (IsVectorConversion(Context, FromType, ToType, SecondICK)) { |
| 1049 | SCS.Second = SecondICK; |
| 1050 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1052 | Context.typesAreCompatible(ToType, FromType)) { |
| 1053 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1054 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1055 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1056 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 1057 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1058 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1059 | } else { |
| 1060 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1061 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1062 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1063 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1064 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1065 | QualType CanonFrom; |
| 1066 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1067 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1068 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1069 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1070 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1071 | CanonFrom = Context.getCanonicalType(FromType); |
| 1072 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1075 | SCS.Third = ICK_Identity; |
| 1076 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1078 | // [...] Any difference in top-level cv-qualification is |
| 1079 | // subsumed by the initialization itself and does not constitute |
| 1080 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1081 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1083 | if (CanonFrom.getLocalUnqualifiedType() |
| 1084 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1085 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1086 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1087 | FromType = ToType; |
| 1088 | CanonFrom = CanonTo; |
| 1089 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1090 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1091 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1092 | |
| 1093 | // If we have not converted the argument type to the parameter type, |
| 1094 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1095 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1096 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1098 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1102 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1103 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1104 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1106 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1107 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1108 | if (!To) { |
| 1109 | return false; |
| 1110 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1111 | |
| 1112 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1113 | // unsigned short int can be converted to an rvalue of type int if |
| 1114 | // int can represent all the values of the source type; otherwise, |
| 1115 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1116 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1117 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1118 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1119 | if (// We can promote any signed, promotable integer type to an int |
| 1120 | (FromType->isSignedIntegerType() || |
| 1121 | // We can promote any unsigned integer type whose size is |
| 1122 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1124 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1125 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1128 | return To->getKind() == BuiltinType::UInt; |
| 1129 | } |
| 1130 | |
| 1131 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 1132 | // can be converted to an rvalue of the first of the following types |
| 1133 | // that can represent all the values of its underlying type: int, |
| 1134 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1135 | |
| 1136 | // We pre-calculate the promotion type for enum types. |
| 1137 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 1138 | if (ToType->isIntegerType()) |
| 1139 | return Context.hasSameUnqualifiedType(ToType, |
| 1140 | FromEnumType->getDecl()->getPromotionType()); |
| 1141 | |
| 1142 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1143 | // Determine whether the type we're converting from is signed or |
| 1144 | // unsigned. |
| 1145 | bool FromIsSigned; |
| 1146 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1147 | |
| 1148 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1149 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1150 | |
| 1151 | // The types we'll try to promote to, in the appropriate |
| 1152 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | QualType PromoteTypes[6] = { |
| 1154 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1155 | Context.LongTy, Context.UnsignedLongTy , |
| 1156 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1157 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1158 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1159 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1160 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1162 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1163 | // We found the type that we can promote to. If this is the |
| 1164 | // type we wanted, we have a promotion. Otherwise, no |
| 1165 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1166 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1172 | // rvalue of type int if int can represent all the values of the |
| 1173 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1174 | // unsigned int can represent all the values of the bit-field. If |
| 1175 | // the bit-field is larger yet, no integral promotion applies to |
| 1176 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1177 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1178 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1179 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1180 | using llvm::APSInt; |
| 1181 | if (From) |
| 1182 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1183 | APSInt BitWidth; |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1184 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1185 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1186 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1187 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1189 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1190 | if (BitWidth < ToSize || |
| 1191 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1192 | return To->getKind() == BuiltinType::Int; |
| 1193 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1195 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1196 | // that fits into an unsigned int? |
| 1197 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1198 | return To->getKind() == BuiltinType::UInt; |
| 1199 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1201 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1202 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1203 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1205 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1206 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1207 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1208 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1209 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1210 | |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1215 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1216 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1218 | /// An rvalue of type float can be converted to an rvalue of type |
| 1219 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1220 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1221 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1222 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1223 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1224 | return true; |
| 1225 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1226 | // C99 6.3.1.5p1: |
| 1227 | // When a float is promoted to double or long double, or a |
| 1228 | // double is promoted to long double [...]. |
| 1229 | if (!getLangOptions().CPlusPlus && |
| 1230 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1231 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1232 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1236 | return false; |
| 1237 | } |
| 1238 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1239 | /// \brief Determine if a conversion is a complex promotion. |
| 1240 | /// |
| 1241 | /// A complex promotion is defined as a complex -> complex conversion |
| 1242 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1243 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1244 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1245 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1246 | if (!FromComplex) |
| 1247 | return false; |
| 1248 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1249 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1250 | if (!ToComplex) |
| 1251 | return false; |
| 1252 | |
| 1253 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1254 | ToComplex->getElementType()) || |
| 1255 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1256 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1259 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1260 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1261 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1262 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1263 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | static QualType |
| 1265 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1266 | QualType ToPointee, QualType ToType, |
| 1267 | ASTContext &Context) { |
| 1268 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 1269 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1270 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1273 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1274 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1275 | if (!ToType.isNull()) |
Douglas Gregor | b9f907b | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1276 | return ToType.getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1279 | // already. |
| 1280 | return Context.getPointerType(ToPointee); |
| 1281 | } |
| 1282 | |
| 1283 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1284 | return Context.getPointerType( |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1285 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1286 | Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1289 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1290 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1291 | /// not have the right set of qualifiers. |
| 1292 | static QualType |
| 1293 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1294 | QualType ToType, |
| 1295 | ASTContext &Context) { |
| 1296 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1297 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1298 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1299 | |
| 1300 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1301 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1302 | return ToType; |
| 1303 | |
| 1304 | // Just build a canonical type that has the right qualifiers. |
| 1305 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1306 | } |
| 1307 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1309 | bool InOverloadResolution, |
| 1310 | ASTContext &Context) { |
| 1311 | // Handle value-dependent integral null pointer constants correctly. |
| 1312 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1313 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1314 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1315 | return !InOverloadResolution; |
| 1316 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1317 | return Expr->isNullPointerConstant(Context, |
| 1318 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1319 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1320 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1322 | /// IsPointerConversion - Determines whether the conversion of the |
| 1323 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1324 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1325 | /// 4.10). If so, returns true and places the converted type (that |
| 1326 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1327 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1328 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1329 | /// This routine also supports conversions to and from block pointers |
| 1330 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1331 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1332 | /// appropriate overloading rules for Objective-C, we may want to |
| 1333 | /// split the Objective-C checks into a different routine; however, |
| 1334 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1335 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1336 | /// set if the conversion is an allowed Objective-C conversion that |
| 1337 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1338 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1339 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1340 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1342 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1343 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1344 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1345 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1347 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1348 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1349 | ConvertedType = ToType; |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1353 | // Blocks: Block pointers can be converted to void*. |
| 1354 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1355 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1356 | ConvertedType = ToType; |
| 1357 | return true; |
| 1358 | } |
| 1359 | // Blocks: A null pointer constant can be converted to a block |
| 1360 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1362 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1363 | ConvertedType = ToType; |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1367 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1368 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1370 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1371 | ConvertedType = ToType; |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1375 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1376 | if (!ToTypePtr) |
| 1377 | return false; |
| 1378 | |
| 1379 | // A null pointer constant can be converted to a pointer type (C++ 4.10p1). |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1380 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1381 | ConvertedType = ToType; |
| 1382 | return true; |
| 1383 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1384 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1385 | // Beyond this point, both types need to be pointers |
| 1386 | // , including objective-c pointers. |
| 1387 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1388 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1389 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1390 | ToType, Context); |
| 1391 | return true; |
| 1392 | |
| 1393 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1394 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1395 | if (!FromTypePtr) |
| 1396 | return false; |
| 1397 | |
| 1398 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1400 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1401 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1402 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1403 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1405 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1406 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
| 1409 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1410 | // When we're overloading in C, we allow a special kind of pointer |
| 1411 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1413 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1415 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1417 | return true; |
| 1418 | } |
| 1419 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1420 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1421 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1422 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1423 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1424 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1425 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1426 | // necessitates this conversion is ill-formed. The result of the |
| 1427 | // conversion is a pointer to the base class sub-object of the |
| 1428 | // derived class object. The null pointer value is converted to |
| 1429 | // the null pointer value of the destination type. |
| 1430 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1431 | // Note that we do not check for ambiguity or inaccessibility |
| 1432 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1433 | if (getLangOptions().CPlusPlus && |
| 1434 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1435 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1436 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1437 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1439 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1440 | ToType, Context); |
| 1441 | return true; |
| 1442 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
| 1446 | |
| 1447 | /// isObjCPointerConversion - Determines whether this is an |
| 1448 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1449 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1451 | QualType& ConvertedType, |
| 1452 | bool &IncompatibleObjC) { |
| 1453 | if (!getLangOptions().ObjC1) |
| 1454 | return false; |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1455 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1456 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1457 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1459 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1460 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1461 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1462 | // Objective C++: We're able to convert between "id" or "Class" and a |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1463 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1464 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1465 | ConvertedType = ToType; |
| 1466 | return true; |
| 1467 | } |
| 1468 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1470 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1472 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1473 | ConvertedType = ToType; |
| 1474 | return true; |
| 1475 | } |
| 1476 | // Objective C++: We're able to convert from a pointer to an |
| 1477 | // interface to a pointer to a different interface. |
| 1478 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1479 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1480 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1481 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1482 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1483 | FromObjCPtr->getPointeeType())) |
| 1484 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1485 | ConvertedType = ToType; |
| 1486 | return true; |
| 1487 | } |
| 1488 | |
| 1489 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1490 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1491 | // interfaces, which is permitted. However, we're going to |
| 1492 | // complain about it. |
| 1493 | IncompatibleObjC = true; |
| 1494 | ConvertedType = FromType; |
| 1495 | return true; |
| 1496 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1498 | // Beyond this point, both types need to be C pointers or block pointers. |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1499 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1500 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1501 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1502 | else if (const BlockPointerType *ToBlockPtr = |
| 1503 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1504 | // Objective C++: We're able to convert from a pointer to any object |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1505 | // to a block pointer type. |
| 1506 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1507 | ConvertedType = ToType; |
| 1508 | return true; |
| 1509 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1510 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1511 | } |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1512 | else if (FromType->getAs<BlockPointerType>() && |
| 1513 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1514 | // Objective C++: We're able to convert from a block pointer type to a |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1515 | // pointer to any object. |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1516 | ConvertedType = ToType; |
| 1517 | return true; |
| 1518 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1519 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1520 | return false; |
| 1521 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1522 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1523 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1524 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1525 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1526 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1527 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1528 | return false; |
| 1529 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1530 | // If we have pointers to pointers, recursively check whether this |
| 1531 | // is an Objective-C conversion. |
| 1532 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1533 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1534 | IncompatibleObjC)) { |
| 1535 | // We always complain about this conversion. |
| 1536 | IncompatibleObjC = true; |
| 1537 | ConvertedType = ToType; |
| 1538 | return true; |
| 1539 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1540 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1541 | // as in I* to id. |
| 1542 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1543 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1544 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1545 | IncompatibleObjC)) { |
| 1546 | ConvertedType = ToType; |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1550 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1551 | // differences in the argument and result types are in Objective-C |
| 1552 | // pointer conversions. If so, we permit the conversion (but |
| 1553 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1555 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1556 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1557 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1558 | if (FromFunctionType && ToFunctionType) { |
| 1559 | // If the function types are exactly the same, this isn't an |
| 1560 | // Objective-C pointer conversion. |
| 1561 | if (Context.getCanonicalType(FromPointeeType) |
| 1562 | == Context.getCanonicalType(ToPointeeType)) |
| 1563 | return false; |
| 1564 | |
| 1565 | // Perform the quick checks that will tell us whether these |
| 1566 | // function types are obviously different. |
| 1567 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1568 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1569 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1570 | return false; |
| 1571 | |
| 1572 | bool HasObjCConversion = false; |
| 1573 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1574 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1575 | // Okay, the types match exactly. Nothing to do. |
| 1576 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1577 | ToFunctionType->getResultType(), |
| 1578 | ConvertedType, IncompatibleObjC)) { |
| 1579 | // Okay, we have an Objective-C pointer conversion. |
| 1580 | HasObjCConversion = true; |
| 1581 | } else { |
| 1582 | // Function types are too different. Abort. |
| 1583 | return false; |
| 1584 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1586 | // Check argument types. |
| 1587 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1588 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1589 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1590 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1591 | if (Context.getCanonicalType(FromArgType) |
| 1592 | == Context.getCanonicalType(ToArgType)) { |
| 1593 | // Okay, the types match exactly. Nothing to do. |
| 1594 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1595 | ConvertedType, IncompatibleObjC)) { |
| 1596 | // Okay, we have an Objective-C pointer conversion. |
| 1597 | HasObjCConversion = true; |
| 1598 | } else { |
| 1599 | // Argument types are too different. Abort. |
| 1600 | return false; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | if (HasObjCConversion) { |
| 1605 | // We had an Objective-C conversion. Allow this pointer |
| 1606 | // conversion, but complain about it. |
| 1607 | ConvertedType = ToType; |
| 1608 | IncompatibleObjC = true; |
| 1609 | return true; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1613 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1614 | } |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1615 | |
| 1616 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1617 | /// for equlity of their argument types. Caller has already checked that |
| 1618 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1619 | /// pointer types which only differ in their protocol qualifiers are equal. |
| 1620 | bool Sema::FunctionArgTypesAreEqual(FunctionProtoType* OldType, |
| 1621 | FunctionProtoType* NewType){ |
| 1622 | if (!getLangOptions().ObjC1) |
| 1623 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1624 | NewType->arg_type_begin()); |
| 1625 | |
| 1626 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1627 | N = NewType->arg_type_begin(), |
| 1628 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1629 | QualType ToType = (*O); |
| 1630 | QualType FromType = (*N); |
| 1631 | if (ToType != FromType) { |
| 1632 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1633 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 27c9fe9 | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1634 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1635 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1636 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1637 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1638 | continue; |
| 1639 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1640 | else if (const ObjCObjectPointerType *PTTo = |
| 1641 | ToType->getAs<ObjCObjectPointerType>()) { |
| 1642 | if (const ObjCObjectPointerType *PTFr = |
| 1643 | FromType->getAs<ObjCObjectPointerType>()) |
| 1644 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1645 | continue; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1646 | } |
| 1647 | return false; |
| 1648 | } |
| 1649 | } |
| 1650 | return true; |
| 1651 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1652 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1653 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1654 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1655 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1656 | /// conversions for which IsPointerConversion has already returned |
| 1657 | /// true. It returns true and produces a diagnostic if there was an |
| 1658 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1659 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1660 | CastExpr::CastKind &Kind, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1661 | CXXBaseSpecifierArray& BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1662 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1663 | QualType FromType = From->getType(); |
| 1664 | |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1665 | if (CXXBoolLiteralExpr* LitBool |
| 1666 | = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens())) |
| 1667 | if (LitBool->getValue() == false) |
| 1668 | Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false) |
| 1669 | << ToType; |
| 1670 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1671 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1672 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1673 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1674 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1676 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1677 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1678 | // We must have a derived-to-base conversion. Check an |
| 1679 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1680 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1681 | From->getExprLoc(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1682 | From->getSourceRange(), &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1683 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1684 | return true; |
| 1685 | |
| 1686 | // The conversion was successful. |
| 1687 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1691 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1693 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1694 | // Objective-C++ conversions are always okay. |
| 1695 | // FIXME: We should have a different class of conversions for the |
| 1696 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1697 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1698 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1699 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1700 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1701 | return false; |
| 1702 | } |
| 1703 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1704 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1705 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1706 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1707 | /// If so, returns true and places the converted type (that might differ from |
| 1708 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1709 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1710 | QualType ToType, |
| 1711 | bool InOverloadResolution, |
| 1712 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1713 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1714 | if (!ToTypePtr) |
| 1715 | return false; |
| 1716 | |
| 1717 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1718 | if (From->isNullPointerConstant(Context, |
| 1719 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1720 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1721 | ConvertedType = ToType; |
| 1722 | return true; |
| 1723 | } |
| 1724 | |
| 1725 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1726 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1727 | if (!FromTypePtr) |
| 1728 | return false; |
| 1729 | |
| 1730 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1731 | // where D is derived from B (C++ 4.11p2). |
| 1732 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1733 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1734 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1735 | |
| 1736 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1737 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1738 | ToClass.getTypePtr()); |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | return false; |
| 1743 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1744 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1745 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1746 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1747 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1748 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1749 | /// true and produces a diagnostic if there was an error, or returns false |
| 1750 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1752 | CastExpr::CastKind &Kind, |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1753 | CXXBaseSpecifierArray &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1754 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1755 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1756 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1757 | if (!FromPtrType) { |
| 1758 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1759 | assert(From->isNullPointerConstant(Context, |
| 1760 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1761 | "Expr must be null pointer constant!"); |
| 1762 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1763 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1764 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1765 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1766 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1767 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1768 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1769 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1770 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1771 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1772 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1773 | // FIXME: What about dependent types? |
| 1774 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1775 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1776 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1777 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1778 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1779 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1780 | assert(DerivationOkay && |
| 1781 | "Should not have been called if derivation isn't OK."); |
| 1782 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1783 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1784 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1785 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1786 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1787 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1788 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1789 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1790 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1792 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1793 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1794 | << FromClass << ToClass << QualType(VBase, 0) |
| 1795 | << From->getSourceRange(); |
| 1796 | return true; |
| 1797 | } |
| 1798 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1799 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1800 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1801 | Paths.front(), |
| 1802 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1803 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1804 | // Must be a base to derived member conversion. |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1805 | BuildBasePathArray(Paths, BasePath); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1806 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1807 | return false; |
| 1808 | } |
| 1809 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1810 | /// IsQualificationConversion - Determines whether the conversion from |
| 1811 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1812 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | bool |
| 1814 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1815 | FromType = Context.getCanonicalType(FromType); |
| 1816 | ToType = Context.getCanonicalType(ToType); |
| 1817 | |
| 1818 | // If FromType and ToType are the same type, this is not a |
| 1819 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1820 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1821 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1823 | // (C++ 4.4p4): |
| 1824 | // A conversion can add cv-qualifiers at levels other than the first |
| 1825 | // in multi-level pointers, subject to the following rules: [...] |
| 1826 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1827 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 1828 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1829 | // Within each iteration of the loop, we check the qualifiers to |
| 1830 | // determine if this still looks like a qualification |
| 1831 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1832 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1833 | // until there are no more pointers or pointers-to-members left to |
| 1834 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1835 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1836 | |
| 1837 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1838 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1839 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1840 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1842 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1843 | // every cv for 0 < k < j. |
| 1844 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1845 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1846 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1848 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1849 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1851 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1853 | |
| 1854 | // We are left with FromType and ToType being the pointee types |
| 1855 | // after unwrapping the original FromType and ToType the same number |
| 1856 | // of types. If we unwrapped any pointers, and if FromType and |
| 1857 | // ToType have the same unqualified type (since we checked |
| 1858 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1859 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1862 | /// Determines whether there is a user-defined conversion sequence |
| 1863 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1864 | /// ToType. If such a conversion exists, User will contain the |
| 1865 | /// user-defined conversion sequence that performs such a conversion |
| 1866 | /// and this routine will return true. Otherwise, this routine returns |
| 1867 | /// false and User is unspecified. |
| 1868 | /// |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1869 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1870 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1871 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1872 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1873 | UserDefinedConversionSequence& User, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1874 | OverloadCandidateSet& CandidateSet, |
| 1875 | bool AllowExplicit) { |
| 1876 | // Whether we will only visit constructors. |
| 1877 | bool ConstructorsOnly = false; |
| 1878 | |
| 1879 | // If the type we are conversion to is a class type, enumerate its |
| 1880 | // constructors. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1881 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1882 | // C++ [over.match.ctor]p1: |
| 1883 | // When objects of class type are direct-initialized (8.5), or |
| 1884 | // copy-initialized from an expression of the same or a |
| 1885 | // derived class type (8.5), overload resolution selects the |
| 1886 | // constructor. [...] For copy-initialization, the candidate |
| 1887 | // functions are all the converting constructors (12.3.1) of |
| 1888 | // that class. The argument list is the expression-list within |
| 1889 | // the parentheses of the initializer. |
| 1890 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1891 | (From->getType()->getAs<RecordType>() && |
| 1892 | IsDerivedFrom(From->getType(), ToType))) |
| 1893 | ConstructorsOnly = true; |
| 1894 | |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1895 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1896 | // We're not going to find any constructors. |
| 1897 | } else if (CXXRecordDecl *ToRecordDecl |
| 1898 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1900 | = Context.DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1901 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1902 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1904 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1905 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1906 | NamedDecl *D = *Con; |
| 1907 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1908 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1909 | // Find the constructor (which may be a template). |
| 1910 | CXXConstructorDecl *Constructor = 0; |
| 1911 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1912 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1913 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1915 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1916 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1917 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1918 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1919 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1920 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1921 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1922 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1923 | /*ExplicitArgs*/ 0, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1924 | &From, 1, CandidateSet, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1925 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1926 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1927 | // Allow one user-defined conversion when user specifies a |
| 1928 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1929 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1930 | &From, 1, CandidateSet, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1931 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1932 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1933 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1937 | // Enumerate conversion functions, if we're allowed to. |
| 1938 | if (ConstructorsOnly) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1940 | PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1941 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1943 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1945 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1946 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1947 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1948 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1949 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1950 | E = Conversions->end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1951 | DeclAccessPair FoundDecl = I.getPair(); |
| 1952 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1953 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1954 | if (isa<UsingShadowDecl>(D)) |
| 1955 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1956 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1957 | CXXConversionDecl *Conv; |
| 1958 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1959 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1960 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1961 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1962 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1963 | |
| 1964 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1965 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1966 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1967 | ActingContext, From, ToType, |
| 1968 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1969 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1970 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1971 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1975 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1976 | |
| 1977 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1978 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1979 | case OR_Success: |
| 1980 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1982 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1983 | // C++ [over.ics.user]p1: |
| 1984 | // If the user-defined conversion is specified by a |
| 1985 | // constructor (12.3.1), the initial standard conversion |
| 1986 | // sequence converts the source type to the type required by |
| 1987 | // the argument of the constructor. |
| 1988 | // |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1989 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1990 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1991 | User.EllipsisConversion = true; |
| 1992 | else { |
| 1993 | User.Before = Best->Conversions[0].Standard; |
| 1994 | User.EllipsisConversion = false; |
| 1995 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1996 | User.ConversionFunction = Constructor; |
| 1997 | User.After.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1998 | User.After.setFromType( |
| 1999 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2000 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2001 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2002 | } else if (CXXConversionDecl *Conversion |
| 2003 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 2004 | // C++ [over.ics.user]p1: |
| 2005 | // |
| 2006 | // [...] If the user-defined conversion is specified by a |
| 2007 | // conversion function (12.3.2), the initial standard |
| 2008 | // conversion sequence converts the source type to the |
| 2009 | // implicit object parameter of the conversion function. |
| 2010 | User.Before = Best->Conversions[0].Standard; |
| 2011 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2012 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
| 2014 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2015 | // The second standard conversion sequence converts the |
| 2016 | // result of the user-defined conversion to the target type |
| 2017 | // for the sequence. Since an implicit conversion sequence |
| 2018 | // is an initialization, the special rules for |
| 2019 | // initialization by user-defined conversion apply when |
| 2020 | // selecting the best user-defined conversion for a |
| 2021 | // user-defined conversion sequence (see 13.3.3 and |
| 2022 | // 13.3.3.1). |
| 2023 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2024 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2025 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2026 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2027 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2028 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2030 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2031 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2032 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2033 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2034 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2035 | |
| 2036 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2037 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2040 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2041 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2042 | |
| 2043 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2044 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2045 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2046 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2047 | OverloadingResult OvResult = |
| 2048 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2049 | CandidateSet, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2050 | if (OvResult == OR_Ambiguous) |
| 2051 | Diag(From->getSourceRange().getBegin(), |
| 2052 | diag::err_typecheck_ambiguous_condition) |
| 2053 | << From->getType() << ToType << From->getSourceRange(); |
| 2054 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2055 | Diag(From->getSourceRange().getBegin(), |
| 2056 | diag::err_typecheck_nonviable_condition) |
| 2057 | << From->getType() << ToType << From->getSourceRange(); |
| 2058 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2059 | return false; |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 2060 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2061 | return true; |
| 2062 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2063 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2064 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2065 | /// conversion sequences to determine whether one is better than the |
| 2066 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2068 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 2069 | const ImplicitConversionSequence& ICS2) |
| 2070 | { |
| 2071 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2072 | // conversion sequences (as defined in 13.3.3.1) |
| 2073 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2074 | // conversion sequence than a user-defined conversion sequence or |
| 2075 | // an ellipsis conversion sequence, and |
| 2076 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2077 | // conversion sequence than an ellipsis conversion sequence |
| 2078 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2080 | // C++0x [over.best.ics]p10: |
| 2081 | // For the purpose of ranking implicit conversion sequences as |
| 2082 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2083 | // treated as a user-defined sequence that is indistinguishable |
| 2084 | // from any other user-defined conversion sequence. |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2085 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2086 | return ImplicitConversionSequence::Better; |
| 2087 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2088 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2089 | |
Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2090 | // The following checks require both conversion sequences to be of |
| 2091 | // the same kind. |
| 2092 | if (ICS1.getKind() != ICS2.getKind()) |
| 2093 | return ImplicitConversionSequence::Indistinguishable; |
| 2094 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2095 | // Two implicit conversion sequences of the same form are |
| 2096 | // indistinguishable conversion sequences unless one of the |
| 2097 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2098 | if (ICS1.isStandard()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2099 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2100 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2101 | // User-defined conversion sequence U1 is a better conversion |
| 2102 | // sequence than another user-defined conversion sequence U2 if |
| 2103 | // they contain the same user-defined conversion function or |
| 2104 | // constructor and if the second standard conversion sequence of |
| 2105 | // U1 is better than the second standard conversion sequence of |
| 2106 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2108 | ICS2.UserDefined.ConversionFunction) |
| 2109 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 2110 | ICS2.UserDefined.After); |
| 2111 | } |
| 2112 | |
| 2113 | return ImplicitConversionSequence::Indistinguishable; |
| 2114 | } |
| 2115 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2116 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2117 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2118 | Qualifiers Quals; |
| 2119 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
| 2120 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
| 2121 | } |
| 2122 | |
| 2123 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2124 | } |
| 2125 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2126 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2127 | // determine if one is a proper subset of the other. |
| 2128 | static ImplicitConversionSequence::CompareKind |
| 2129 | compareStandardConversionSubsets(ASTContext &Context, |
| 2130 | const StandardConversionSequence& SCS1, |
| 2131 | const StandardConversionSequence& SCS2) { |
| 2132 | ImplicitConversionSequence::CompareKind Result |
| 2133 | = ImplicitConversionSequence::Indistinguishable; |
| 2134 | |
Douglas Gregor | e87561a | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2135 | // the identity conversion sequence is considered to be a subsequence of |
| 2136 | // any non-identity conversion sequence |
| 2137 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2138 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2139 | return ImplicitConversionSequence::Better; |
| 2140 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2141 | return ImplicitConversionSequence::Worse; |
| 2142 | } |
| 2143 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2144 | if (SCS1.Second != SCS2.Second) { |
| 2145 | if (SCS1.Second == ICK_Identity) |
| 2146 | Result = ImplicitConversionSequence::Better; |
| 2147 | else if (SCS2.Second == ICK_Identity) |
| 2148 | Result = ImplicitConversionSequence::Worse; |
| 2149 | else |
| 2150 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2151 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2152 | return ImplicitConversionSequence::Indistinguishable; |
| 2153 | |
| 2154 | if (SCS1.Third == SCS2.Third) { |
| 2155 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2156 | : ImplicitConversionSequence::Indistinguishable; |
| 2157 | } |
| 2158 | |
| 2159 | if (SCS1.Third == ICK_Identity) |
| 2160 | return Result == ImplicitConversionSequence::Worse |
| 2161 | ? ImplicitConversionSequence::Indistinguishable |
| 2162 | : ImplicitConversionSequence::Better; |
| 2163 | |
| 2164 | if (SCS2.Third == ICK_Identity) |
| 2165 | return Result == ImplicitConversionSequence::Better |
| 2166 | ? ImplicitConversionSequence::Indistinguishable |
| 2167 | : ImplicitConversionSequence::Worse; |
| 2168 | |
| 2169 | return ImplicitConversionSequence::Indistinguishable; |
| 2170 | } |
| 2171 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2172 | /// CompareStandardConversionSequences - Compare two standard |
| 2173 | /// conversion sequences to determine whether one is better than the |
| 2174 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2176 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 2177 | const StandardConversionSequence& SCS2) |
| 2178 | { |
| 2179 | // Standard conversion sequence S1 is a better conversion sequence |
| 2180 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2181 | |
| 2182 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2183 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2184 | // excluding any Lvalue Transformation; the identity conversion |
| 2185 | // sequence is considered to be a subsequence of any |
| 2186 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2187 | if (ImplicitConversionSequence::CompareKind CK |
| 2188 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 2189 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2190 | |
| 2191 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2192 | // defined below), or, if not that, |
| 2193 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2194 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2195 | if (Rank1 < Rank2) |
| 2196 | return ImplicitConversionSequence::Better; |
| 2197 | else if (Rank2 < Rank1) |
| 2198 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2200 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2201 | // are indistinguishable unless one of the following rules |
| 2202 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2204 | // A conversion that is not a conversion of a pointer, or |
| 2205 | // pointer to member, to bool is better than another conversion |
| 2206 | // that is such a conversion. |
| 2207 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2208 | return SCS2.isPointerConversionToBool() |
| 2209 | ? ImplicitConversionSequence::Better |
| 2210 | : ImplicitConversionSequence::Worse; |
| 2211 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2212 | // C++ [over.ics.rank]p4b2: |
| 2213 | // |
| 2214 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2215 | // conversion of B* to A* is better than conversion of B* to |
| 2216 | // void*, and conversion of A* to void* is better than conversion |
| 2217 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2219 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2221 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2222 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2223 | // Exactly one of the conversion sequences is a conversion to |
| 2224 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2225 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2226 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2227 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2228 | // Neither conversion sequence converts to a void pointer; compare |
| 2229 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2230 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 2231 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 2232 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2233 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2234 | // Both conversion sequences are conversions to void |
| 2235 | // pointers. Compare the source types to determine if there's an |
| 2236 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2237 | QualType FromType1 = SCS1.getFromType(); |
| 2238 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2239 | |
| 2240 | // Adjust the types we're converting from via the array-to-pointer |
| 2241 | // conversion, if we need to. |
| 2242 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2243 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2244 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2245 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2246 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2247 | QualType FromPointee1 |
| 2248 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2249 | QualType FromPointee2 |
| 2250 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2251 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2252 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2253 | return ImplicitConversionSequence::Better; |
| 2254 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2255 | return ImplicitConversionSequence::Worse; |
| 2256 | |
| 2257 | // Objective-C++: If one interface is more specific than the |
| 2258 | // other, it is the better one. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2259 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2260 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2261 | if (FromIface1 && FromIface1) { |
| 2262 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2263 | return ImplicitConversionSequence::Better; |
| 2264 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2265 | return ImplicitConversionSequence::Worse; |
| 2266 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2267 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2268 | |
| 2269 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2270 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2272 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2273 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2275 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2276 | // C++0x [over.ics.rank]p3b4: |
| 2277 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2278 | // implicit object parameter of a non-static member function declared |
| 2279 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 2280 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2281 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 2282 | // or if the member function in this case has a ref qualifier. |
| 2283 | // (Of course, we don't have ref qualifiers yet.) |
| 2284 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 2285 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 2286 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2287 | |
| 2288 | // C++ [over.ics.rank]p3b4: |
| 2289 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2290 | // which the references refer are the same type except for |
| 2291 | // top-level cv-qualifiers, and the type to which the reference |
| 2292 | // initialized by S2 refers is more cv-qualified than the type |
| 2293 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2294 | QualType T1 = SCS1.getToType(2); |
| 2295 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2296 | T1 = Context.getCanonicalType(T1); |
| 2297 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2298 | Qualifiers T1Quals, T2Quals; |
| 2299 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2300 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2301 | if (UnqualT1 == UnqualT2) { |
| 2302 | // If the type is an array type, promote the element qualifiers to the type |
| 2303 | // for comparison. |
| 2304 | if (isa<ArrayType>(T1) && T1Quals) |
| 2305 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2306 | if (isa<ArrayType>(T2) && T2Quals) |
| 2307 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2308 | if (T2.isMoreQualifiedThan(T1)) |
| 2309 | return ImplicitConversionSequence::Better; |
| 2310 | else if (T1.isMoreQualifiedThan(T2)) |
| 2311 | return ImplicitConversionSequence::Worse; |
| 2312 | } |
| 2313 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2314 | |
| 2315 | return ImplicitConversionSequence::Indistinguishable; |
| 2316 | } |
| 2317 | |
| 2318 | /// CompareQualificationConversions - Compares two standard conversion |
| 2319 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2321 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2322 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2324 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2325 | // -- S1 and S2 differ only in their qualification conversion and |
| 2326 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2327 | // cv-qualification signature of type T1 is a proper subset of |
| 2328 | // the cv-qualification signature of type T2, and S1 is not the |
| 2329 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2330 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2331 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2332 | return ImplicitConversionSequence::Indistinguishable; |
| 2333 | |
| 2334 | // FIXME: the example in the standard doesn't use a qualification |
| 2335 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2336 | QualType T1 = SCS1.getToType(2); |
| 2337 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2338 | T1 = Context.getCanonicalType(T1); |
| 2339 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2340 | Qualifiers T1Quals, T2Quals; |
| 2341 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2342 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2343 | |
| 2344 | // If the types are the same, we won't learn anything by unwrapped |
| 2345 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2346 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2347 | return ImplicitConversionSequence::Indistinguishable; |
| 2348 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2349 | // If the type is an array type, promote the element qualifiers to the type |
| 2350 | // for comparison. |
| 2351 | if (isa<ArrayType>(T1) && T1Quals) |
| 2352 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2353 | if (isa<ArrayType>(T2) && T2Quals) |
| 2354 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2355 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2357 | = ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2358 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2359 | // Within each iteration of the loop, we check the qualifiers to |
| 2360 | // determine if this still looks like a qualification |
| 2361 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2362 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2363 | // until there are no more pointers or pointers-to-members left |
| 2364 | // to unwrap. This essentially mimics what |
| 2365 | // IsQualificationConversion does, but here we're checking for a |
| 2366 | // strict subset of qualifiers. |
| 2367 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2368 | // The qualifiers are the same, so this doesn't tell us anything |
| 2369 | // about how the sequences rank. |
| 2370 | ; |
| 2371 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2372 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2373 | if (Result == ImplicitConversionSequence::Worse) |
| 2374 | // Neither has qualifiers that are a subset of the other's |
| 2375 | // qualifiers. |
| 2376 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2378 | Result = ImplicitConversionSequence::Better; |
| 2379 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2380 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2381 | if (Result == ImplicitConversionSequence::Better) |
| 2382 | // Neither has qualifiers that are a subset of the other's |
| 2383 | // qualifiers. |
| 2384 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2386 | Result = ImplicitConversionSequence::Worse; |
| 2387 | } else { |
| 2388 | // Qualifiers are disjoint. |
| 2389 | return ImplicitConversionSequence::Indistinguishable; |
| 2390 | } |
| 2391 | |
| 2392 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2393 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2394 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2397 | // Check that the winning standard conversion sequence isn't using |
| 2398 | // the deprecated string literal array to pointer conversion. |
| 2399 | switch (Result) { |
| 2400 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2401 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2402 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2403 | break; |
| 2404 | |
| 2405 | case ImplicitConversionSequence::Indistinguishable: |
| 2406 | break; |
| 2407 | |
| 2408 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2409 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2410 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2411 | break; |
| 2412 | } |
| 2413 | |
| 2414 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2417 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2418 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2419 | /// various kinds of derived-to-base conversions (C++ |
| 2420 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2421 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2422 | ImplicitConversionSequence::CompareKind |
| 2423 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2424 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2425 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2426 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2427 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2428 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2429 | |
| 2430 | // Adjust the types we're converting from via the array-to-pointer |
| 2431 | // conversion, if we need to. |
| 2432 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2433 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2434 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2435 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2436 | |
| 2437 | // Canonicalize all of the types. |
| 2438 | FromType1 = Context.getCanonicalType(FromType1); |
| 2439 | ToType1 = Context.getCanonicalType(ToType1); |
| 2440 | FromType2 = Context.getCanonicalType(FromType2); |
| 2441 | ToType2 = Context.getCanonicalType(ToType2); |
| 2442 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2443 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2444 | // |
| 2445 | // If class B is derived directly or indirectly from class A and |
| 2446 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2447 | // |
| 2448 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2449 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2450 | |
| 2451 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2453 | SCS2.Second == ICK_Pointer_Conversion && |
| 2454 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2455 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2456 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2458 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2460 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2461 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2462 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2463 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2464 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2465 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2466 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2467 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
| 2468 | const ObjCObjectType* ToIface1 = ToPointee1->getAs<ObjCObjectType>(); |
| 2469 | const ObjCObjectType* ToIface2 = ToPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2471 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2472 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2473 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2474 | return ImplicitConversionSequence::Better; |
| 2475 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2476 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2477 | |
| 2478 | if (ToIface1 && ToIface2) { |
| 2479 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2480 | return ImplicitConversionSequence::Better; |
| 2481 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2482 | return ImplicitConversionSequence::Worse; |
| 2483 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2484 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2485 | |
| 2486 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2487 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2488 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2489 | return ImplicitConversionSequence::Better; |
| 2490 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2491 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2493 | if (FromIface1 && FromIface2) { |
| 2494 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2495 | return ImplicitConversionSequence::Better; |
| 2496 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2497 | return ImplicitConversionSequence::Worse; |
| 2498 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2499 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2502 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2503 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2504 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2505 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2506 | const MemberPointerType * FromMemPointer1 = |
| 2507 | FromType1->getAs<MemberPointerType>(); |
| 2508 | const MemberPointerType * ToMemPointer1 = |
| 2509 | ToType1->getAs<MemberPointerType>(); |
| 2510 | const MemberPointerType * FromMemPointer2 = |
| 2511 | FromType2->getAs<MemberPointerType>(); |
| 2512 | const MemberPointerType * ToMemPointer2 = |
| 2513 | ToType2->getAs<MemberPointerType>(); |
| 2514 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2515 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2516 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2517 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2518 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2519 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2520 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2521 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2522 | // conversion of A::* to B::* is better than conversion of A::* to C::*, |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2523 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2524 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2525 | return ImplicitConversionSequence::Worse; |
| 2526 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2527 | return ImplicitConversionSequence::Better; |
| 2528 | } |
| 2529 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2530 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2531 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2532 | return ImplicitConversionSequence::Better; |
| 2533 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2534 | return ImplicitConversionSequence::Worse; |
| 2535 | } |
| 2536 | } |
| 2537 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2538 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2539 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2540 | // -- binding of an expression of type C to a reference of type |
| 2541 | // B& is better than binding an expression of type C to a |
| 2542 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2543 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2544 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2545 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2546 | return ImplicitConversionSequence::Better; |
| 2547 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2548 | return ImplicitConversionSequence::Worse; |
| 2549 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2550 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2551 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2552 | // -- binding of an expression of type B to a reference of type |
| 2553 | // A& is better than binding an expression of type C to a |
| 2554 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2555 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2556 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2557 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2558 | return ImplicitConversionSequence::Better; |
| 2559 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2560 | return ImplicitConversionSequence::Worse; |
| 2561 | } |
| 2562 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2563 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2564 | return ImplicitConversionSequence::Indistinguishable; |
| 2565 | } |
| 2566 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2567 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2568 | /// determine whether they are reference-related, |
| 2569 | /// reference-compatible, reference-compatible with added |
| 2570 | /// qualification, or incompatible, for use in C++ initialization by |
| 2571 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2572 | /// type, and the first type (T1) is the pointee type of the reference |
| 2573 | /// type being initialized. |
| 2574 | Sema::ReferenceCompareResult |
| 2575 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2576 | QualType OrigT1, QualType OrigT2, |
| 2577 | bool& DerivedToBase) { |
| 2578 | assert(!OrigT1->isReferenceType() && |
| 2579 | "T1 must be the pointee type of the reference type"); |
| 2580 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2581 | |
| 2582 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2583 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2584 | Qualifiers T1Quals, T2Quals; |
| 2585 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2586 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2587 | |
| 2588 | // C++ [dcl.init.ref]p4: |
| 2589 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2590 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2591 | // T1 is a base class of T2. |
| 2592 | if (UnqualT1 == UnqualT2) |
| 2593 | DerivedToBase = false; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2594 | else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2595 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2596 | DerivedToBase = true; |
| 2597 | else |
| 2598 | return Ref_Incompatible; |
| 2599 | |
| 2600 | // At this point, we know that T1 and T2 are reference-related (at |
| 2601 | // least). |
| 2602 | |
| 2603 | // If the type is an array type, promote the element qualifiers to the type |
| 2604 | // for comparison. |
| 2605 | if (isa<ArrayType>(T1) && T1Quals) |
| 2606 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2607 | if (isa<ArrayType>(T2) && T2Quals) |
| 2608 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2609 | |
| 2610 | // C++ [dcl.init.ref]p4: |
| 2611 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2612 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2613 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2614 | // overload resolution, cases for which cv1 is greater |
| 2615 | // cv-qualification than cv2 are identified as |
| 2616 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2617 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2618 | return Ref_Compatible; |
| 2619 | else if (T1.isMoreQualifiedThan(T2)) |
| 2620 | return Ref_Compatible_With_Added_Qualification; |
| 2621 | else |
| 2622 | return Ref_Related; |
| 2623 | } |
| 2624 | |
| 2625 | /// \brief Compute an implicit conversion sequence for reference |
| 2626 | /// initialization. |
| 2627 | static ImplicitConversionSequence |
| 2628 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2629 | SourceLocation DeclLoc, |
| 2630 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2631 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2632 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2633 | |
| 2634 | // Most paths end in a failed conversion. |
| 2635 | ImplicitConversionSequence ICS; |
| 2636 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2637 | |
| 2638 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2639 | QualType T2 = Init->getType(); |
| 2640 | |
| 2641 | // If the initializer is the address of an overloaded function, try |
| 2642 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2643 | // type of the resulting function. |
| 2644 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2645 | DeclAccessPair Found; |
| 2646 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2647 | false, Found)) |
| 2648 | T2 = Fn->getType(); |
| 2649 | } |
| 2650 | |
| 2651 | // Compute some basic properties of the types and the initializer. |
| 2652 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2653 | bool DerivedToBase = false; |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2654 | Expr::isLvalueResult InitLvalue = Init->isLvalue(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2655 | Sema::ReferenceCompareResult RefRelationship |
| 2656 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
| 2657 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2658 | |
| 2659 | // C++ [over.ics.ref]p3: |
| 2660 | // Except for an implicit object parameter, for which see 13.3.1, |
| 2661 | // a standard conversion sequence cannot be formed if it requires |
| 2662 | // binding an lvalue reference to non-const to an rvalue or |
| 2663 | // binding an rvalue reference to an lvalue. |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2664 | // |
| 2665 | // FIXME: DPG doesn't trust this code. It seems far too early to |
| 2666 | // abort because of a binding of an rvalue reference to an lvalue. |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2667 | if (isRValRef && InitLvalue == Expr::LV_Valid) |
| 2668 | return ICS; |
| 2669 | |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2670 | // C++0x [dcl.init.ref]p16: |
| 2671 | // A reference to type "cv1 T1" is initialized by an expression |
| 2672 | // of type "cv2 T2" as follows: |
| 2673 | |
| 2674 | // -- If the initializer expression |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2675 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2676 | // reference-compatible with "cv2 T2," or |
| 2677 | // |
| 2678 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2679 | if (InitLvalue == Expr::LV_Valid && |
| 2680 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2681 | // C++ [over.ics.ref]p1: |
| 2682 | // When a parameter of reference type binds directly (8.5.3) |
| 2683 | // to an argument expression, the implicit conversion sequence |
| 2684 | // is the identity conversion, unless the argument expression |
| 2685 | // has a type that is a derived class of the parameter type, |
| 2686 | // in which case the implicit conversion sequence is a |
| 2687 | // derived-to-base Conversion (13.3.3.1). |
| 2688 | ICS.setStandard(); |
| 2689 | ICS.Standard.First = ICK_Identity; |
| 2690 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2691 | ICS.Standard.Third = ICK_Identity; |
| 2692 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2693 | ICS.Standard.setToType(0, T2); |
| 2694 | ICS.Standard.setToType(1, T1); |
| 2695 | ICS.Standard.setToType(2, T1); |
| 2696 | ICS.Standard.ReferenceBinding = true; |
| 2697 | ICS.Standard.DirectBinding = true; |
| 2698 | ICS.Standard.RRefBinding = false; |
| 2699 | ICS.Standard.CopyConstructor = 0; |
| 2700 | |
| 2701 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2702 | // derived-to-base conversions is suppressed when we're |
| 2703 | // computing the implicit conversion sequence (C++ |
| 2704 | // [over.best.ics]p2). |
| 2705 | return ICS; |
| 2706 | } |
| 2707 | |
| 2708 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2709 | // not reference-related to T2, and can be implicitly |
| 2710 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2711 | // is reference-compatible with "cv3 T3" 92) (this |
| 2712 | // conversion is selected by enumerating the applicable |
| 2713 | // conversion functions (13.3.1.6) and choosing the best |
| 2714 | // one through overload resolution (13.3)), |
| 2715 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
| 2716 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2717 | RefRelationship == Sema::Ref_Incompatible) { |
| 2718 | CXXRecordDecl *T2RecordDecl |
| 2719 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2720 | |
| 2721 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2722 | const UnresolvedSetImpl *Conversions |
| 2723 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2724 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2725 | E = Conversions->end(); I != E; ++I) { |
| 2726 | NamedDecl *D = *I; |
| 2727 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2728 | if (isa<UsingShadowDecl>(D)) |
| 2729 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2730 | |
| 2731 | FunctionTemplateDecl *ConvTemplate |
| 2732 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2733 | CXXConversionDecl *Conv; |
| 2734 | if (ConvTemplate) |
| 2735 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2736 | else |
| 2737 | Conv = cast<CXXConversionDecl>(D); |
| 2738 | |
| 2739 | // If the conversion function doesn't return a reference type, |
| 2740 | // it can't be considered for this conversion. |
| 2741 | if (Conv->getConversionType()->isLValueReferenceType() && |
| 2742 | (AllowExplicit || !Conv->isExplicit())) { |
| 2743 | if (ConvTemplate) |
| 2744 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2745 | Init, DeclType, CandidateSet); |
| 2746 | else |
| 2747 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2748 | DeclType, CandidateSet); |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | OverloadCandidateSet::iterator Best; |
| 2753 | switch (S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2754 | case OR_Success: |
| 2755 | // C++ [over.ics.ref]p1: |
| 2756 | // |
| 2757 | // [...] If the parameter binds directly to the result of |
| 2758 | // applying a conversion function to the argument |
| 2759 | // expression, the implicit conversion sequence is a |
| 2760 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2761 | // second standard conversion sequence either an identity |
| 2762 | // conversion or, if the conversion function returns an |
| 2763 | // entity of a type that is a derived class of the parameter |
| 2764 | // type, a derived-to-base Conversion. |
| 2765 | if (!Best->FinalConversion.DirectBinding) |
| 2766 | break; |
| 2767 | |
| 2768 | ICS.setUserDefined(); |
| 2769 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2770 | ICS.UserDefined.After = Best->FinalConversion; |
| 2771 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2772 | ICS.UserDefined.EllipsisConversion = false; |
| 2773 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2774 | ICS.UserDefined.After.DirectBinding && |
| 2775 | "Expected a direct reference binding!"); |
| 2776 | return ICS; |
| 2777 | |
| 2778 | case OR_Ambiguous: |
| 2779 | ICS.setAmbiguous(); |
| 2780 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2781 | Cand != CandidateSet.end(); ++Cand) |
| 2782 | if (Cand->Viable) |
| 2783 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2784 | return ICS; |
| 2785 | |
| 2786 | case OR_No_Viable_Function: |
| 2787 | case OR_Deleted: |
| 2788 | // There was no suitable conversion, or we found a deleted |
| 2789 | // conversion; continue with other checks. |
| 2790 | break; |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | // -- Otherwise, the reference shall be to a non-volatile const |
| 2795 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 2796 | // rvalue reference and the initializer expression shall be an rvalue. |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2797 | // |
| 2798 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2799 | // point, which is that, due to p2 (which short-circuits reference |
| 2800 | // binding by only attempting a simple conversion for non-direct |
| 2801 | // bindings) and p3's strange wording, we allow a const volatile |
| 2802 | // reference to bind to an rvalue. Hence the check for the presence |
| 2803 | // of "const" rather than checking for "const" being the only |
| 2804 | // qualifier. |
| 2805 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2806 | return ICS; |
| 2807 | |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2808 | // -- if T2 is a class type and |
| 2809 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2810 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2811 | // |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2812 | // -- T1 is not reference-related to T2 and the initializer |
| 2813 | // expression can be implicitly converted to an rvalue |
| 2814 | // of type "cv3 T3" (this conversion is selected by |
| 2815 | // enumerating the applicable conversion functions |
| 2816 | // (13.3.1.6) and choosing the best one through overload |
| 2817 | // resolution (13.3)), |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2818 | // |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2819 | // then the reference is bound to the initializer |
| 2820 | // expression rvalue in the first case and to the object |
| 2821 | // that is the result of the conversion in the second case |
| 2822 | // (or, in either case, to the appropriate base class |
| 2823 | // subobject of the object). |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2824 | // |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2825 | // We're only checking the first case here, which is a direct |
| 2826 | // binding in C++0x but not in C++03. |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2827 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
| 2828 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2829 | ICS.setStandard(); |
| 2830 | ICS.Standard.First = ICK_Identity; |
| 2831 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2832 | ICS.Standard.Third = ICK_Identity; |
| 2833 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2834 | ICS.Standard.setToType(0, T2); |
| 2835 | ICS.Standard.setToType(1, T1); |
| 2836 | ICS.Standard.setToType(2, T1); |
| 2837 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2838 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2839 | ICS.Standard.RRefBinding = isRValRef; |
| 2840 | ICS.Standard.CopyConstructor = 0; |
| 2841 | return ICS; |
| 2842 | } |
| 2843 | |
| 2844 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2845 | // initialized from the initializer expression using the |
| 2846 | // rules for a non-reference copy initialization (8.5). The |
| 2847 | // reference is then bound to the temporary. If T1 is |
| 2848 | // reference-related to T2, cv1 must be the same |
| 2849 | // cv-qualification as, or greater cv-qualification than, |
| 2850 | // cv2; otherwise, the program is ill-formed. |
| 2851 | if (RefRelationship == Sema::Ref_Related) { |
| 2852 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2853 | // we would be reference-compatible or reference-compatible with |
| 2854 | // added qualification. But that wasn't the case, so the reference |
| 2855 | // initialization fails. |
| 2856 | return ICS; |
| 2857 | } |
| 2858 | |
| 2859 | // If at least one of the types is a class type, the types are not |
| 2860 | // related, and we aren't allowed any user conversions, the |
| 2861 | // reference binding fails. This case is important for breaking |
| 2862 | // recursion, since TryImplicitConversion below will attempt to |
| 2863 | // create a temporary through the use of a copy constructor. |
| 2864 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 2865 | (T1->isRecordType() || T2->isRecordType())) |
| 2866 | return ICS; |
| 2867 | |
| 2868 | // C++ [over.ics.ref]p2: |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2869 | // When a parameter of reference type is not bound directly to |
| 2870 | // an argument expression, the conversion sequence is the one |
| 2871 | // required to convert the argument expression to the |
| 2872 | // underlying type of the reference according to |
| 2873 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 2874 | // to copy-initializing a temporary of the underlying type with |
| 2875 | // the argument expression. Any difference in top-level |
| 2876 | // cv-qualification is subsumed by the initialization itself |
| 2877 | // and does not constitute a conversion. |
| 2878 | ICS = S.TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 2879 | /*AllowExplicit=*/false, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2880 | /*InOverloadResolution=*/false); |
| 2881 | |
| 2882 | // Of course, that's still a reference binding. |
| 2883 | if (ICS.isStandard()) { |
| 2884 | ICS.Standard.ReferenceBinding = true; |
| 2885 | ICS.Standard.RRefBinding = isRValRef; |
| 2886 | } else if (ICS.isUserDefined()) { |
| 2887 | ICS.UserDefined.After.ReferenceBinding = true; |
| 2888 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 2889 | } |
| 2890 | return ICS; |
| 2891 | } |
| 2892 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2893 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2894 | /// ToType from the expression From. Return the implicit conversion |
| 2895 | /// sequence required to pass this argument, which may be a bad |
| 2896 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2897 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 2898 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2899 | static ImplicitConversionSequence |
| 2900 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | dcd27ff | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 2901 | bool SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2902 | bool InOverloadResolution) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2903 | if (ToType->isReferenceType()) |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2904 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2905 | /*FIXME:*/From->getLocStart(), |
| 2906 | SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2907 | /*AllowExplicit=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2909 | return S.TryImplicitConversion(From, ToType, |
| 2910 | SuppressUserConversions, |
| 2911 | /*AllowExplicit=*/false, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2912 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2915 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2916 | /// parameter of the given member function (@c Method) from the |
| 2917 | /// expression @p From. |
| 2918 | ImplicitConversionSequence |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2919 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2920 | CXXMethodDecl *Method, |
| 2921 | CXXRecordDecl *ActingContext) { |
| 2922 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2923 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2924 | // const volatile object. |
| 2925 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2926 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2927 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2928 | |
| 2929 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2930 | // to exit early. |
| 2931 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2932 | |
| 2933 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2934 | QualType FromType = OrigFromType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2935 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2936 | FromType = PT->getPointeeType(); |
| 2937 | |
| 2938 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2939 | |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2940 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2941 | // where X is the class of which the function is a member |
| 2942 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2943 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2945 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2946 | // reference binding here, that allows class rvalues to bind to |
| 2947 | // non-constant references. |
| 2948 | |
| 2949 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2950 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2951 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2952 | if (ImplicitParamType.getCVRQualifiers() |
| 2953 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2954 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2955 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2956 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2957 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2958 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2959 | |
| 2960 | // Check that we have either the same type or a derived type. It |
| 2961 | // affects the conversion rank. |
| 2962 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2963 | ImplicitConversionKind SecondKind; |
| 2964 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2965 | SecondKind = ICK_Identity; |
| 2966 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2967 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2968 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2969 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2970 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2971 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2972 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2973 | |
| 2974 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2975 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2976 | ICS.Standard.setAsIdentityConversion(); |
| 2977 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2978 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2979 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2980 | ICS.Standard.ReferenceBinding = true; |
| 2981 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2982 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2983 | return ICS; |
| 2984 | } |
| 2985 | |
| 2986 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2987 | /// the implicit object parameter for the given Method with the given |
| 2988 | /// expression. |
| 2989 | bool |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2990 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 2991 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2992 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2993 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2994 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2996 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2998 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2999 | FromRecordType = PT->getPointeeType(); |
| 3000 | DestType = Method->getThisType(Context); |
| 3001 | } else { |
| 3002 | FromRecordType = From->getType(); |
| 3003 | DestType = ImplicitParamRecordType; |
| 3004 | } |
| 3005 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3006 | // Note that we always use the true parent context when performing |
| 3007 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | ImplicitConversionSequence ICS |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3009 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 3010 | Method->getParent()); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3011 | if (ICS.isBad()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3012 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3013 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3014 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3016 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3017 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3018 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3019 | if (!Context.hasSameType(From->getType(), DestType)) |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3020 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
| 3021 | /*isLvalue=*/!From->getType()->isPointerType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3022 | return false; |
| 3023 | } |
| 3024 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3025 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3026 | /// expression From to bool (C++0x [conv]p3). |
| 3027 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Douglas Gregor | 0bbe94d | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3028 | // FIXME: This is pretty broken. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3030 | // FIXME: Are these flags correct? |
| 3031 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3033 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3037 | /// of the expression From to bool (C++0x [conv]p3). |
| 3038 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 3039 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3040 | if (!ICS.isBad()) |
| 3041 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3042 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3043 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3044 | return Diag(From->getSourceRange().getBegin(), |
| 3045 | diag::err_typecheck_bool_condition) |
| 3046 | << From->getType() << From->getSourceRange(); |
| 3047 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3048 | } |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3049 | |
| 3050 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3051 | /// expression From to 'id'. |
| 3052 | ImplicitConversionSequence Sema::TryContextuallyConvertToObjCId(Expr *From) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3053 | QualType Ty = Context.getObjCIdType(); |
| 3054 | return TryImplicitConversion(From, Ty, |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3055 | // FIXME: Are these flags correct? |
| 3056 | /*SuppressUserConversions=*/false, |
| 3057 | /*AllowExplicit=*/true, |
| 3058 | /*InOverloadResolution=*/false); |
| 3059 | } |
| 3060 | |
| 3061 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3062 | /// of the expression From to 'id'. |
| 3063 | bool Sema::PerformContextuallyConvertToObjCId(Expr *&From) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3064 | QualType Ty = Context.getObjCIdType(); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3065 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(From); |
| 3066 | if (!ICS.isBad()) |
| 3067 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| 3068 | return true; |
| 3069 | } |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3071 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3072 | /// candidate functions, using the given function call arguments. If |
| 3073 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3074 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3075 | /// |
| 3076 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3077 | /// based on an incomplete set of function arguments. This feature is used by |
| 3078 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3079 | void |
| 3080 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3081 | DeclAccessPair FoundDecl, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3082 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3083 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3084 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3085 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3087 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3088 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3090 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3092 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3093 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3094 | // If we get here, it's because we're calling a member function |
| 3095 | // that is named without a member access expression (e.g., |
| 3096 | // "this->f") that was either written explicitly or created |
| 3097 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3098 | // function, e.g., X::f(). We use an empty type for the implied |
| 3099 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3100 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3101 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3102 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3103 | SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3104 | return; |
| 3105 | } |
| 3106 | // We treat a constructor like a non-member function, since its object |
| 3107 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3110 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3111 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3112 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3113 | // Overload resolution is always an unevaluated context. |
| 3114 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3115 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3116 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3117 | // C++ [class.copy]p3: |
| 3118 | // A member function template is never instantiated to perform the copy |
| 3119 | // of a class object to an object of its class type. |
| 3120 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 3121 | if (NumArgs == 1 && |
| 3122 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3123 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3124 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3125 | return; |
| 3126 | } |
| 3127 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3128 | // Add this candidate |
| 3129 | CandidateSet.push_back(OverloadCandidate()); |
| 3130 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3131 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3132 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3133 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3134 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3135 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3136 | |
| 3137 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3138 | |
| 3139 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3140 | // parameters is viable only if it has an ellipsis in its parameter |
| 3141 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3142 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 3143 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3144 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3145 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3146 | return; |
| 3147 | } |
| 3148 | |
| 3149 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3150 | // is viable only if the (m+1)st parameter has a default argument |
| 3151 | // (8.3.6). For the purposes of overload resolution, the |
| 3152 | // parameter list is truncated on the right, so that there are |
| 3153 | // exactly m parameters. |
| 3154 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3155 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3156 | // Not enough arguments. |
| 3157 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3158 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3159 | return; |
| 3160 | } |
| 3161 | |
| 3162 | // Determine the implicit conversion sequences for each of the |
| 3163 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3164 | Candidate.Conversions.resize(NumArgs); |
| 3165 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3166 | if (ArgIdx < NumArgsInProto) { |
| 3167 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3168 | // exist for each argument an implicit conversion sequence |
| 3169 | // (13.3.3.1) that converts that argument to the corresponding |
| 3170 | // parameter of F. |
| 3171 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3173 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3174 | SuppressUserConversions, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3175 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3176 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3177 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3178 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3179 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3180 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3181 | } else { |
| 3182 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3183 | // argument for which there is no corresponding parameter is |
| 3184 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3185 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3190 | /// \brief Add all of the function declarations in the given function set to |
| 3191 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3192 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3193 | Expr **Args, unsigned NumArgs, |
| 3194 | OverloadCandidateSet& CandidateSet, |
| 3195 | bool SuppressUserConversions) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3196 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3197 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3198 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3199 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3200 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3201 | cast<CXXMethodDecl>(FD)->getParent(), |
| 3202 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3203 | CandidateSet, SuppressUserConversions); |
| 3204 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3205 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3206 | SuppressUserConversions); |
| 3207 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3208 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3209 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3210 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3211 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3212 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3213 | /*FIXME: explicit args */ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3214 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3215 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3216 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3217 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3218 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3219 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3220 | Args, NumArgs, CandidateSet, |
| 3221 | SuppressUserConversions); |
| 3222 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3223 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3226 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3227 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3228 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3229 | QualType ObjectType, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3230 | Expr **Args, unsigned NumArgs, |
| 3231 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3232 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3233 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3234 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3235 | |
| 3236 | if (isa<UsingShadowDecl>(Decl)) |
| 3237 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 3238 | |
| 3239 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3240 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3241 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3242 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3243 | /*ExplicitArgs*/ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3244 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3245 | CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3246 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3247 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3248 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3249 | ObjectType, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3250 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3251 | } |
| 3252 | } |
| 3253 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3254 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3255 | /// of candidate functions, using the given function call arguments |
| 3256 | /// and the object argument (@c Object). For example, in a call |
| 3257 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 3258 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 3259 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3260 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3262 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3263 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 3264 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3265 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3266 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3267 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3268 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3269 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3270 | assert(!isa<CXXConstructorDecl>(Method) && |
| 3271 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3273 | if (!CandidateSet.isNewCandidate(Method)) |
| 3274 | return; |
| 3275 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3276 | // Overload resolution is always an unevaluated context. |
| 3277 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3278 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3279 | // Add this candidate |
| 3280 | CandidateSet.push_back(OverloadCandidate()); |
| 3281 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3282 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3283 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3284 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3285 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3286 | |
| 3287 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3288 | |
| 3289 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3290 | // parameters is viable only if it has an ellipsis in its parameter |
| 3291 | // list (8.3.5). |
| 3292 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3293 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3294 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3295 | return; |
| 3296 | } |
| 3297 | |
| 3298 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3299 | // is viable only if the (m+1)st parameter has a default argument |
| 3300 | // (8.3.6). For the purposes of overload resolution, the |
| 3301 | // parameter list is truncated on the right, so that there are |
| 3302 | // exactly m parameters. |
| 3303 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 3304 | if (NumArgs < MinRequiredArgs) { |
| 3305 | // Not enough arguments. |
| 3306 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3307 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3308 | return; |
| 3309 | } |
| 3310 | |
| 3311 | Candidate.Viable = true; |
| 3312 | Candidate.Conversions.resize(NumArgs + 1); |
| 3313 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3314 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3315 | // The implicit object argument is ignored. |
| 3316 | Candidate.IgnoreObjectArgument = true; |
| 3317 | else { |
| 3318 | // Determine the implicit conversion sequence for the object |
| 3319 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3320 | Candidate.Conversions[0] |
| 3321 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3322 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3323 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3324 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3325 | return; |
| 3326 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | // Determine the implicit conversion sequences for each of the |
| 3330 | // arguments. |
| 3331 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3332 | if (ArgIdx < NumArgsInProto) { |
| 3333 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3334 | // exist for each argument an implicit conversion sequence |
| 3335 | // (13.3.3.1) that converts that argument to the corresponding |
| 3336 | // parameter of F. |
| 3337 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3339 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3340 | SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3341 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3342 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3343 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3344 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3345 | break; |
| 3346 | } |
| 3347 | } else { |
| 3348 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3349 | // argument for which there is no corresponding parameter is |
| 3350 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3351 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 3355 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3356 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 3357 | /// set, using template argument deduction to produce an appropriate member |
| 3358 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3360 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3361 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3362 | CXXRecordDecl *ActingContext, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3363 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3364 | QualType ObjectType, |
| 3365 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3366 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3367 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3368 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3369 | return; |
| 3370 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3371 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3373 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3375 | // candidate functions in the usual way.113) A given name can refer to one |
| 3376 | // or more function templates and also to a set of overloaded non-template |
| 3377 | // functions. In such a case, the candidate functions generated from each |
| 3378 | // function template are combined with the set of non-template candidate |
| 3379 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3380 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3381 | FunctionDecl *Specialization = 0; |
| 3382 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3383 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3384 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3385 | CandidateSet.push_back(OverloadCandidate()); |
| 3386 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3387 | Candidate.FoundDecl = FoundDecl; |
| 3388 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 3389 | Candidate.Viable = false; |
| 3390 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3391 | Candidate.IsSurrogate = false; |
| 3392 | Candidate.IgnoreObjectArgument = false; |
| 3393 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3394 | Info); |
| 3395 | return; |
| 3396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3398 | // Add the function template specialization produced by template argument |
| 3399 | // deduction as a candidate. |
| 3400 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3402 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3403 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3404 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3405 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3408 | /// \brief Add a C++ function template specialization as a candidate |
| 3409 | /// in the candidate set, using template argument deduction to produce |
| 3410 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3412 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3413 | DeclAccessPair FoundDecl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3414 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3415 | Expr **Args, unsigned NumArgs, |
| 3416 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3417 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3418 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3419 | return; |
| 3420 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3421 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3423 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3425 | // candidate functions in the usual way.113) A given name can refer to one |
| 3426 | // or more function templates and also to a set of overloaded non-template |
| 3427 | // functions. In such a case, the candidate functions generated from each |
| 3428 | // function template are combined with the set of non-template candidate |
| 3429 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3430 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3431 | FunctionDecl *Specialization = 0; |
| 3432 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3433 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3434 | Args, NumArgs, Specialization, Info)) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3435 | CandidateSet.push_back(OverloadCandidate()); |
| 3436 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3437 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3438 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3439 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3440 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3441 | Candidate.IsSurrogate = false; |
| 3442 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3443 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3444 | Info); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3445 | return; |
| 3446 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3448 | // Add the function template specialization produced by template argument |
| 3449 | // deduction as a candidate. |
| 3450 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3451 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3452 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3453 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3455 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3457 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3459 | /// (which may or may not be the same type as the type that the |
| 3460 | /// conversion function produces). |
| 3461 | void |
| 3462 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3463 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3464 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3465 | Expr *From, QualType ToType, |
| 3466 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3467 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3468 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3469 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3470 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3471 | return; |
| 3472 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3473 | // Overload resolution is always an unevaluated context. |
| 3474 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3475 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3476 | // Add this candidate |
| 3477 | CandidateSet.push_back(OverloadCandidate()); |
| 3478 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3479 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3480 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3481 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3482 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3483 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3484 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3485 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3486 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3487 | // Determine the implicit conversion sequence for the implicit |
| 3488 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3489 | Candidate.Viable = true; |
| 3490 | Candidate.Conversions.resize(1); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3491 | Candidate.Conversions[0] |
| 3492 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 3493 | ActingContext); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3494 | // Conversion functions to a different type in the base class is visible in |
| 3495 | // the derived class. So, a derived to base conversion should not participate |
| 3496 | // in overload resolution. |
| 3497 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 3498 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3499 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3500 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3501 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3502 | return; |
| 3503 | } |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3504 | |
| 3505 | // We won't go through a user-define type conversion function to convert a |
| 3506 | // derived to base as such conversions are given Conversion Rank. They only |
| 3507 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3508 | QualType FromCanon |
| 3509 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3510 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3511 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3512 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3513 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3514 | return; |
| 3515 | } |
| 3516 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3517 | // To determine what the conversion from the result of calling the |
| 3518 | // conversion function to the type we're eventually trying to |
| 3519 | // convert to (ToType), we need to synthesize a call to the |
| 3520 | // conversion function and attempt copy initialization from it. This |
| 3521 | // makes sure that we get the right semantics with respect to |
| 3522 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3523 | // call on the stack and we don't need its arguments to be |
| 3524 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3525 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3526 | From->getLocStart()); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3527 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3528 | CastExpr::CK_FunctionToPointerDecay, |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3529 | &ConversionRef, CXXBaseSpecifierArray(), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3530 | |
| 3531 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3532 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3533 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3535 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3536 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | ImplicitConversionSequence ICS = |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3538 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3539 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3540 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3542 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3543 | case ImplicitConversionSequence::StandardConversion: |
| 3544 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3545 | |
| 3546 | // C++ [over.ics.user]p3: |
| 3547 | // If the user-defined conversion is specified by a specialization of a |
| 3548 | // conversion function template, the second standard conversion sequence |
| 3549 | // shall have exact match rank. |
| 3550 | if (Conversion->getPrimaryTemplate() && |
| 3551 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3552 | Candidate.Viable = false; |
| 3553 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3554 | } |
| 3555 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | |
| 3558 | case ImplicitConversionSequence::BadConversion: |
| 3559 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3560 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3561 | break; |
| 3562 | |
| 3563 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3564 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3565 | "Can only end up with a standard conversion sequence or failure"); |
| 3566 | } |
| 3567 | } |
| 3568 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3569 | /// \brief Adds a conversion function template specialization |
| 3570 | /// candidate to the overload set, using template argument deduction |
| 3571 | /// to deduce the template arguments of the conversion function |
| 3572 | /// template from the type that we are converting to (C++ |
| 3573 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3575 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3576 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3577 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3578 | Expr *From, QualType ToType, |
| 3579 | OverloadCandidateSet &CandidateSet) { |
| 3580 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3581 | "Only conversion function templates permitted here"); |
| 3582 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3583 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3584 | return; |
| 3585 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3586 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3587 | CXXConversionDecl *Specialization = 0; |
| 3588 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3590 | Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3591 | CandidateSet.push_back(OverloadCandidate()); |
| 3592 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3593 | Candidate.FoundDecl = FoundDecl; |
| 3594 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3595 | Candidate.Viable = false; |
| 3596 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3597 | Candidate.IsSurrogate = false; |
| 3598 | Candidate.IgnoreObjectArgument = false; |
| 3599 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3600 | Info); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3601 | return; |
| 3602 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3604 | // Add the conversion function template specialization produced by |
| 3605 | // template argument deduction as a candidate. |
| 3606 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3607 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3608 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3611 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3612 | /// converts the given @c Object to a function pointer via the |
| 3613 | /// conversion function @c Conversion, and then attempts to call it |
| 3614 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3615 | /// the type of function that we'll eventually be calling. |
| 3616 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3617 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3618 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3619 | const FunctionProtoType *Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3620 | QualType ObjectType, |
| 3621 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3622 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3623 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3624 | return; |
| 3625 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3626 | // Overload resolution is always an unevaluated context. |
| 3627 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3628 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3629 | CandidateSet.push_back(OverloadCandidate()); |
| 3630 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3631 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3632 | Candidate.Function = 0; |
| 3633 | Candidate.Surrogate = Conversion; |
| 3634 | Candidate.Viable = true; |
| 3635 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3636 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3637 | Candidate.Conversions.resize(NumArgs + 1); |
| 3638 | |
| 3639 | // Determine the implicit conversion sequence for the implicit |
| 3640 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | ImplicitConversionSequence ObjectInit |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3642 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3643 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3644 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3645 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3646 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3647 | return; |
| 3648 | } |
| 3649 | |
| 3650 | // The first conversion is actually a user-defined conversion whose |
| 3651 | // first conversion is ObjectInit's standard conversion (which is |
| 3652 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3653 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3654 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3655 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3656 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3658 | = Candidate.Conversions[0].UserDefined.Before; |
| 3659 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3660 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3661 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3662 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3663 | |
| 3664 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3665 | // parameters is viable only if it has an ellipsis in its parameter |
| 3666 | // list (8.3.5). |
| 3667 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3668 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3669 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3670 | return; |
| 3671 | } |
| 3672 | |
| 3673 | // Function types don't have any default arguments, so just check if |
| 3674 | // we have enough arguments. |
| 3675 | if (NumArgs < NumArgsInProto) { |
| 3676 | // Not enough arguments. |
| 3677 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3678 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3679 | return; |
| 3680 | } |
| 3681 | |
| 3682 | // Determine the implicit conversion sequences for each of the |
| 3683 | // arguments. |
| 3684 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3685 | if (ArgIdx < NumArgsInProto) { |
| 3686 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3687 | // exist for each argument an implicit conversion sequence |
| 3688 | // (13.3.3.1) that converts that argument to the corresponding |
| 3689 | // parameter of F. |
| 3690 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3691 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3692 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3693 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3694 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3695 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3696 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3697 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3698 | break; |
| 3699 | } |
| 3700 | } else { |
| 3701 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3702 | // argument for which there is no corresponding parameter is |
| 3703 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3704 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3705 | } |
| 3706 | } |
| 3707 | } |
| 3708 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3709 | /// \brief Add overload candidates for overloaded operators that are |
| 3710 | /// member functions. |
| 3711 | /// |
| 3712 | /// Add the overloaded operator candidates that are member functions |
| 3713 | /// for the operator Op that was used in an operator expression such |
| 3714 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3715 | /// CandidateSet will store the added overload candidates. (C++ |
| 3716 | /// [over.match.oper]). |
| 3717 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3718 | SourceLocation OpLoc, |
| 3719 | Expr **Args, unsigned NumArgs, |
| 3720 | OverloadCandidateSet& CandidateSet, |
| 3721 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3722 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3723 | |
| 3724 | // C++ [over.match.oper]p3: |
| 3725 | // For a unary operator @ with an operand of a type whose |
| 3726 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3727 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3728 | // a right operand of a type whose cv-unqualified version is T2, |
| 3729 | // three sets of candidate functions, designated member |
| 3730 | // candidates, non-member candidates and built-in candidates, are |
| 3731 | // constructed as follows: |
| 3732 | QualType T1 = Args[0]->getType(); |
| 3733 | QualType T2; |
| 3734 | if (NumArgs > 1) |
| 3735 | T2 = Args[1]->getType(); |
| 3736 | |
| 3737 | // -- If T1 is a class type, the set of member candidates is the |
| 3738 | // result of the qualified lookup of T1::operator@ |
| 3739 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3740 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3741 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3742 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3743 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3744 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3746 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3747 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3748 | Operators.suppressDiagnostics(); |
| 3749 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3751 | OperEnd = Operators.end(); |
| 3752 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3753 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3754 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3755 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3756 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3757 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3760 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3761 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3762 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3763 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3764 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3765 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3766 | /// (at the beginning of the argument list) that will be contextually |
| 3767 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3769 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3770 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3771 | bool IsAssignmentOperator, |
| 3772 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3773 | // Overload resolution is always an unevaluated context. |
| 3774 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3775 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3776 | // Add this candidate |
| 3777 | CandidateSet.push_back(OverloadCandidate()); |
| 3778 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3779 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3780 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3781 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3782 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3783 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3784 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3785 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3786 | |
| 3787 | // Determine the implicit conversion sequences for each of the |
| 3788 | // arguments. |
| 3789 | Candidate.Viable = true; |
| 3790 | Candidate.Conversions.resize(NumArgs); |
| 3791 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3792 | // C++ [over.match.oper]p4: |
| 3793 | // For the built-in assignment operators, conversions of the |
| 3794 | // left operand are restricted as follows: |
| 3795 | // -- no temporaries are introduced to hold the left operand, and |
| 3796 | // -- no user-defined conversions are applied to the left |
| 3797 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3799 | // |
| 3800 | // We block these conversions by turning off user-defined |
| 3801 | // conversions, since that is the only way that initialization of |
| 3802 | // a reference to a non-class type can occur from something that |
| 3803 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3804 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3806 | "Contextual conversion to bool requires bool type"); |
| 3807 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3808 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3810 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3811 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3812 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3813 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3814 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3815 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3816 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3817 | break; |
| 3818 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3823 | /// candidate operator functions for built-in operators (C++ |
| 3824 | /// [over.built]). The types are separated into pointer types and |
| 3825 | /// enumeration types. |
| 3826 | class BuiltinCandidateTypeSet { |
| 3827 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3828 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3829 | |
| 3830 | /// PointerTypes - The set of pointer types that will be used in the |
| 3831 | /// built-in candidates. |
| 3832 | TypeSet PointerTypes; |
| 3833 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3834 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3835 | /// used in the built-in candidates. |
| 3836 | TypeSet MemberPointerTypes; |
| 3837 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3838 | /// EnumerationTypes - The set of enumeration types that will be |
| 3839 | /// used in the built-in candidates. |
| 3840 | TypeSet EnumerationTypes; |
| 3841 | |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 3842 | /// \brief The set of vector types that will be used in the built-in |
| 3843 | /// candidates. |
| 3844 | TypeSet VectorTypes; |
| 3845 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3846 | /// Sema - The semantic analysis instance where we are building the |
| 3847 | /// candidate type set. |
| 3848 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3850 | /// Context - The AST context in which we will build the type sets. |
| 3851 | ASTContext &Context; |
| 3852 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3853 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3854 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3855 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3856 | |
| 3857 | public: |
| 3858 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3859 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3860 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3862 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3863 | |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3864 | void AddTypesConvertedFrom(QualType Ty, |
| 3865 | SourceLocation Loc, |
| 3866 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3867 | bool AllowExplicitConversions, |
| 3868 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3869 | |
| 3870 | /// pointer_begin - First pointer type found; |
| 3871 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3872 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3873 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3874 | iterator pointer_end() { return PointerTypes.end(); } |
| 3875 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3876 | /// member_pointer_begin - First member pointer type found; |
| 3877 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3878 | |
| 3879 | /// member_pointer_end - Past the last member pointer type found; |
| 3880 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3881 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3882 | /// enumeration_begin - First enumeration type found; |
| 3883 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3884 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3885 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3886 | iterator enumeration_end() { return EnumerationTypes.end(); } |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 3887 | |
| 3888 | iterator vector_begin() { return VectorTypes.begin(); } |
| 3889 | iterator vector_end() { return VectorTypes.end(); } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3890 | }; |
| 3891 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3892 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3893 | /// the set of pointer types along with any more-qualified variants of |
| 3894 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3895 | /// will add "int const *", "int const volatile *", "int const |
| 3896 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3897 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3898 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3899 | /// |
| 3900 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3901 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3902 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3903 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3905 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3906 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3907 | return false; |
| 3908 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3909 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3910 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3911 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3912 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3913 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3914 | // (the qualifier would sink to the element type), and for another, the |
| 3915 | // only overload situation where it matters is subscript or pointer +- int, |
| 3916 | // and those shouldn't have qualifier variants anyway. |
| 3917 | if (PointeeTy->isArrayType()) |
| 3918 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3919 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3920 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3921 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3922 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3923 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3924 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3925 | // Iterate through all strict supersets of BaseCVR. |
| 3926 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3927 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3928 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3929 | // in the types. |
| 3930 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3931 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3932 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3933 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | return true; |
| 3937 | } |
| 3938 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3939 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3940 | /// to the set of pointer types along with any more-qualified variants of |
| 3941 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3942 | /// will add "int const *", "int const volatile *", "int const |
| 3943 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3944 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3945 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3946 | /// |
| 3947 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3948 | bool |
| 3949 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3950 | QualType Ty) { |
| 3951 | // Insert this type. |
| 3952 | if (!MemberPointerTypes.insert(Ty)) |
| 3953 | return false; |
| 3954 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3955 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3956 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3957 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3958 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3959 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3960 | // (the qualifier would sink to the element type), and for another, the |
| 3961 | // only overload situation where it matters is subscript or pointer +- int, |
| 3962 | // and those shouldn't have qualifier variants anyway. |
| 3963 | if (PointeeTy->isArrayType()) |
| 3964 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3965 | const Type *ClassTy = PointerTy->getClass(); |
| 3966 | |
| 3967 | // Iterate through all strict supersets of the pointee type's CVR |
| 3968 | // qualifiers. |
| 3969 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3970 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3971 | if ((CVR | BaseCVR) != CVR) continue; |
| 3972 | |
| 3973 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3974 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | return true; |
| 3978 | } |
| 3979 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3980 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3981 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3982 | /// primarily interested in pointer types and enumeration types. We also |
| 3983 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3984 | /// AllowUserConversions is true if we should look at the conversion |
| 3985 | /// functions of a class type, and AllowExplicitConversions if we |
| 3986 | /// should also include the explicit conversion functions of a class |
| 3987 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3988 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3989 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3990 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3991 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3992 | bool AllowExplicitConversions, |
| 3993 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3994 | // Only deal with canonical types. |
| 3995 | Ty = Context.getCanonicalType(Ty); |
| 3996 | |
| 3997 | // Look through reference types; they aren't part of the type of an |
| 3998 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3999 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4000 | Ty = RefTy->getPointeeType(); |
| 4001 | |
| 4002 | // We don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4003 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4004 | |
Sebastian Redl | 65ae200 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 4005 | // If we're dealing with an array type, decay to the pointer. |
| 4006 | if (Ty->isArrayType()) |
| 4007 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4008 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4009 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4010 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 4011 | |
| 4012 | // Insert our type, and its more-qualified variants, into the set |
| 4013 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4014 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4015 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4016 | } else if (Ty->isMemberPointerType()) { |
| 4017 | // Member pointers are far easier, since the pointee can't be converted. |
| 4018 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4019 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4020 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4021 | EnumerationTypes.insert(Ty); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4022 | } else if (Ty->isVectorType()) { |
| 4023 | VectorTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4024 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4025 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4026 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4027 | // No conversion functions in incomplete types. |
| 4028 | return; |
| 4029 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4031 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4032 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ae01f78 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 4033 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4034 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4035 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4036 | NamedDecl *D = I.getDecl(); |
| 4037 | if (isa<UsingShadowDecl>(D)) |
| 4038 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4039 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4041 | // about which builtin types we can convert to. |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4042 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4043 | continue; |
| 4044 | |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4045 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4046 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4047 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4048 | VisibleQuals); |
| 4049 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | } |
| 4053 | } |
| 4054 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4055 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4056 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4057 | /// given type to the candidate set. |
| 4058 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4059 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4060 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4061 | unsigned NumArgs, |
| 4062 | OverloadCandidateSet &CandidateSet) { |
| 4063 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4065 | // T& operator=(T&, T) |
| 4066 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4067 | ParamTypes[1] = T; |
| 4068 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4069 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4071 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4072 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4073 | ParamTypes[0] |
| 4074 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4075 | ParamTypes[1] = T; |
| 4076 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4078 | } |
| 4079 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4081 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4082 | /// if any, found in visible type conversion functions found in ArgExpr's type. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4083 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4084 | Qualifiers VRQuals; |
| 4085 | const RecordType *TyRec; |
| 4086 | if (const MemberPointerType *RHSMPType = |
| 4087 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4088 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4089 | else |
| 4090 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4091 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4092 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4093 | VRQuals.addVolatile(); |
| 4094 | VRQuals.addRestrict(); |
| 4095 | return VRQuals; |
| 4096 | } |
| 4097 | |
| 4098 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4099 | if (!ClassDecl->hasDefinition()) |
| 4100 | return VRQuals; |
| 4101 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4102 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4103 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4104 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4105 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4106 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4107 | NamedDecl *D = I.getDecl(); |
| 4108 | if (isa<UsingShadowDecl>(D)) |
| 4109 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4110 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4111 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4112 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4113 | CanTy = ResTypeRef->getPointeeType(); |
| 4114 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4115 | // as see them. |
| 4116 | bool done = false; |
| 4117 | while (!done) { |
| 4118 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4119 | CanTy = ResTypePtr->getPointeeType(); |
| 4120 | else if (const MemberPointerType *ResTypeMPtr = |
| 4121 | CanTy->getAs<MemberPointerType>()) |
| 4122 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4123 | else |
| 4124 | done = true; |
| 4125 | if (CanTy.isVolatileQualified()) |
| 4126 | VRQuals.addVolatile(); |
| 4127 | if (CanTy.isRestrictQualified()) |
| 4128 | VRQuals.addRestrict(); |
| 4129 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4130 | return VRQuals; |
| 4131 | } |
| 4132 | } |
| 4133 | } |
| 4134 | return VRQuals; |
| 4135 | } |
| 4136 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4137 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 4138 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 4139 | /// on the operator @p Op and the arguments given. For example, if the |
| 4140 | /// operator is a binary '+', this routine might add "int |
| 4141 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4142 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4144 | SourceLocation OpLoc, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4145 | Expr **Args, unsigned NumArgs, |
| 4146 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4147 | // The set of "promoted arithmetic types", which are the arithmetic |
| 4148 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 4149 | // that the first few of these types are the promoted integral |
| 4150 | // types; these types need to be first. |
| 4151 | // FIXME: What about complex? |
| 4152 | const unsigned FirstIntegralType = 0; |
| 4153 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4155 | LastPromotedIntegralType = 13; |
| 4156 | const unsigned FirstPromotedArithmeticType = 7, |
| 4157 | LastPromotedArithmeticType = 16; |
| 4158 | const unsigned NumArithmeticTypes = 16; |
| 4159 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 4161 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4162 | Context.SignedCharTy, Context.ShortTy, |
| 4163 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 4164 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 4165 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 4166 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 4167 | }; |
Douglas Gregor | b8440a7 | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 4168 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 4169 | "Invalid first promoted integral type"); |
| 4170 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 4171 | == Context.UnsignedLongLongTy && |
| 4172 | "Invalid last promoted integral type"); |
| 4173 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 4174 | "Invalid first promoted arithmetic type"); |
| 4175 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 4176 | == Context.LongDoubleTy && |
| 4177 | "Invalid last promoted arithmetic type"); |
| 4178 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4179 | // Find all of the types that the arguments can convert to, but only |
| 4180 | // if the operator we're looking at has built-in operator candidates |
| 4181 | // that make use of these types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4182 | Qualifiers VisibleTypeConversionsQuals; |
| 4183 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4184 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4185 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 4186 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4187 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4188 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4189 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 4190 | OpLoc, |
| 4191 | true, |
| 4192 | (Op == OO_Exclaim || |
| 4193 | Op == OO_AmpAmp || |
| 4194 | Op == OO_PipePipe), |
| 4195 | VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4196 | |
| 4197 | bool isComparison = false; |
| 4198 | switch (Op) { |
| 4199 | case OO_None: |
| 4200 | case NUM_OVERLOADED_OPERATORS: |
| 4201 | assert(false && "Expected an overloaded operator"); |
| 4202 | break; |
| 4203 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4204 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4206 | goto UnaryStar; |
| 4207 | else |
| 4208 | goto BinaryStar; |
| 4209 | break; |
| 4210 | |
| 4211 | case OO_Plus: // '+' is either unary or binary |
| 4212 | if (NumArgs == 1) |
| 4213 | goto UnaryPlus; |
| 4214 | else |
| 4215 | goto BinaryPlus; |
| 4216 | break; |
| 4217 | |
| 4218 | case OO_Minus: // '-' is either unary or binary |
| 4219 | if (NumArgs == 1) |
| 4220 | goto UnaryMinus; |
| 4221 | else |
| 4222 | goto BinaryMinus; |
| 4223 | break; |
| 4224 | |
| 4225 | case OO_Amp: // '&' is either unary or binary |
| 4226 | if (NumArgs == 1) |
| 4227 | goto UnaryAmp; |
| 4228 | else |
| 4229 | goto BinaryAmp; |
| 4230 | |
| 4231 | case OO_PlusPlus: |
| 4232 | case OO_MinusMinus: |
| 4233 | // C++ [over.built]p3: |
| 4234 | // |
| 4235 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 4236 | // is either volatile or empty, there exist candidate operator |
| 4237 | // functions of the form |
| 4238 | // |
| 4239 | // VQ T& operator++(VQ T&); |
| 4240 | // T operator++(VQ T&, int); |
| 4241 | // |
| 4242 | // C++ [over.built]p4: |
| 4243 | // |
| 4244 | // For every pair (T, VQ), where T is an arithmetic type other |
| 4245 | // than bool, and VQ is either volatile or empty, there exist |
| 4246 | // candidate operator functions of the form |
| 4247 | // |
| 4248 | // VQ T& operator--(VQ T&); |
| 4249 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4250 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4251 | Arith < NumArithmeticTypes; ++Arith) { |
| 4252 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4253 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4254 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4255 | |
| 4256 | // Non-volatile version. |
| 4257 | if (NumArgs == 1) |
| 4258 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4259 | else |
| 4260 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4261 | // heuristic to reduce number of builtin candidates in the set. |
| 4262 | // Add volatile version only if there are conversions to a volatile type. |
| 4263 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4264 | // Volatile version |
| 4265 | ParamTypes[0] |
| 4266 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 4267 | if (NumArgs == 1) |
| 4268 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4269 | else |
| 4270 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 4271 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | // C++ [over.built]p5: |
| 4275 | // |
| 4276 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4277 | // cv-unqualified object type, and VQ is either volatile or |
| 4278 | // empty, there exist candidate operator functions of the form |
| 4279 | // |
| 4280 | // T*VQ& operator++(T*VQ&); |
| 4281 | // T*VQ& operator--(T*VQ&); |
| 4282 | // T* operator++(T*VQ&, int); |
| 4283 | // T* operator--(T*VQ&, int); |
| 4284 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4285 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4286 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4287 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4288 | continue; |
| 4289 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | QualType ParamTypes[2] = { |
| 4291 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4292 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4293 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4294 | // Without volatile |
| 4295 | if (NumArgs == 1) |
| 4296 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4297 | else |
| 4298 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4299 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4300 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4301 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4302 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4303 | ParamTypes[0] |
| 4304 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4305 | if (NumArgs == 1) |
| 4306 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4307 | else |
| 4308 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4309 | } |
| 4310 | } |
| 4311 | break; |
| 4312 | |
| 4313 | UnaryStar: |
| 4314 | // C++ [over.built]p6: |
| 4315 | // For every cv-qualified or cv-unqualified object type T, there |
| 4316 | // exist candidate operator functions of the form |
| 4317 | // |
| 4318 | // T& operator*(T*); |
| 4319 | // |
| 4320 | // C++ [over.built]p7: |
| 4321 | // For every function type T, there exist candidate operator |
| 4322 | // functions of the form |
| 4323 | // T& operator*(T*); |
| 4324 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4325 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4326 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4327 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4328 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4329 | &ParamTy, Args, 1, CandidateSet); |
| 4330 | } |
| 4331 | break; |
| 4332 | |
| 4333 | UnaryPlus: |
| 4334 | // C++ [over.built]p8: |
| 4335 | // For every type T, there exist candidate operator functions of |
| 4336 | // the form |
| 4337 | // |
| 4338 | // T* operator+(T*); |
| 4339 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4340 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4341 | QualType ParamTy = *Ptr; |
| 4342 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 4343 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4345 | // Fall through |
| 4346 | |
| 4347 | UnaryMinus: |
| 4348 | // C++ [over.built]p9: |
| 4349 | // For every promoted arithmetic type T, there exist candidate |
| 4350 | // operator functions of the form |
| 4351 | // |
| 4352 | // T operator+(T); |
| 4353 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4354 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4355 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 4356 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 4357 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 4358 | } |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4359 | |
| 4360 | // Extension: We also add these operators for vector types. |
| 4361 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4362 | VecEnd = CandidateTypes.vector_end(); |
| 4363 | Vec != VecEnd; ++Vec) { |
| 4364 | QualType VecTy = *Vec; |
| 4365 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4366 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4367 | break; |
| 4368 | |
| 4369 | case OO_Tilde: |
| 4370 | // C++ [over.built]p10: |
| 4371 | // For every promoted integral type T, there exist candidate |
| 4372 | // operator functions of the form |
| 4373 | // |
| 4374 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4376 | Int < LastPromotedIntegralType; ++Int) { |
| 4377 | QualType IntTy = ArithmeticTypes[Int]; |
| 4378 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 4379 | } |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4380 | |
| 4381 | // Extension: We also add this operator for vector types. |
| 4382 | for (BuiltinCandidateTypeSet::iterator Vec = CandidateTypes.vector_begin(), |
| 4383 | VecEnd = CandidateTypes.vector_end(); |
| 4384 | Vec != VecEnd; ++Vec) { |
| 4385 | QualType VecTy = *Vec; |
| 4386 | AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 4387 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4388 | break; |
| 4389 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4390 | case OO_New: |
| 4391 | case OO_Delete: |
| 4392 | case OO_Array_New: |
| 4393 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4394 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4395 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4396 | break; |
| 4397 | |
| 4398 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4399 | UnaryAmp: |
| 4400 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4401 | // C++ [over.match.oper]p3: |
| 4402 | // -- For the operator ',', the unary operator '&', or the |
| 4403 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4404 | break; |
| 4405 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4406 | case OO_EqualEqual: |
| 4407 | case OO_ExclaimEqual: |
| 4408 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | // For every pointer to member type T, there exist candidate operator |
| 4410 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4411 | // |
| 4412 | // bool operator==(T,T); |
| 4413 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4414 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4415 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4416 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4417 | MemPtr != MemPtrEnd; |
| 4418 | ++MemPtr) { |
| 4419 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4420 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4423 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4425 | case OO_Less: |
| 4426 | case OO_Greater: |
| 4427 | case OO_LessEqual: |
| 4428 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4429 | // C++ [over.built]p15: |
| 4430 | // |
| 4431 | // For every pointer or enumeration type T, there exist |
| 4432 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4434 | // bool operator<(T, T); |
| 4435 | // bool operator>(T, T); |
| 4436 | // bool operator<=(T, T); |
| 4437 | // bool operator>=(T, T); |
| 4438 | // bool operator==(T, T); |
| 4439 | // bool operator!=(T, T); |
| 4440 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4441 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4442 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4443 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4444 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4446 | = CandidateTypes.enumeration_begin(); |
| 4447 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4448 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4449 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4450 | } |
| 4451 | |
| 4452 | // Fall through. |
| 4453 | isComparison = true; |
| 4454 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4455 | BinaryPlus: |
| 4456 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4457 | if (!isComparison) { |
| 4458 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4459 | |
| 4460 | // C++ [over.built]p13: |
| 4461 | // |
| 4462 | // For every cv-qualified or cv-unqualified object type T |
| 4463 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4465 | // T* operator+(T*, ptrdiff_t); |
| 4466 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4467 | // T* operator-(T*, ptrdiff_t); |
| 4468 | // T* operator+(ptrdiff_t, T*); |
| 4469 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4470 | // |
| 4471 | // C++ [over.built]p14: |
| 4472 | // |
| 4473 | // For every T, where T is a pointer to object type, there |
| 4474 | // exist candidate operator functions of the form |
| 4475 | // |
| 4476 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4477 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4478 | = CandidateTypes.pointer_begin(); |
| 4479 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4480 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4481 | |
| 4482 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4483 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4484 | |
| 4485 | if (Op == OO_Plus) { |
| 4486 | // T* operator+(ptrdiff_t, T*); |
| 4487 | ParamTypes[0] = ParamTypes[1]; |
| 4488 | ParamTypes[1] = *Ptr; |
| 4489 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4490 | } else { |
| 4491 | // ptrdiff_t operator-(T, T); |
| 4492 | ParamTypes[1] = *Ptr; |
| 4493 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4494 | Args, 2, CandidateSet); |
| 4495 | } |
| 4496 | } |
| 4497 | } |
| 4498 | // Fall through |
| 4499 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4500 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4501 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4502 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4503 | // C++ [over.built]p12: |
| 4504 | // |
| 4505 | // For every pair of promoted arithmetic types L and R, there |
| 4506 | // exist candidate operator functions of the form |
| 4507 | // |
| 4508 | // LR operator*(L, R); |
| 4509 | // LR operator/(L, R); |
| 4510 | // LR operator+(L, R); |
| 4511 | // LR operator-(L, R); |
| 4512 | // bool operator<(L, R); |
| 4513 | // bool operator>(L, R); |
| 4514 | // bool operator<=(L, R); |
| 4515 | // bool operator>=(L, R); |
| 4516 | // bool operator==(L, R); |
| 4517 | // bool operator!=(L, R); |
| 4518 | // |
| 4519 | // where LR is the result of the usual arithmetic conversions |
| 4520 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4521 | // |
| 4522 | // C++ [over.built]p24: |
| 4523 | // |
| 4524 | // For every pair of promoted arithmetic types L and R, there exist |
| 4525 | // candidate operator functions of the form |
| 4526 | // |
| 4527 | // LR operator?(bool, L, R); |
| 4528 | // |
| 4529 | // where LR is the result of the usual arithmetic conversions |
| 4530 | // between types L and R. |
| 4531 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4533 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4534 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4535 | Right < LastPromotedArithmeticType; ++Right) { |
| 4536 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4537 | QualType Result |
| 4538 | = isComparison |
| 4539 | ? Context.BoolTy |
| 4540 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4541 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4542 | } |
| 4543 | } |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4544 | |
| 4545 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 4546 | // conditional operator for vector types. |
| 4547 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4548 | Vec1End = CandidateTypes.vector_end(); |
| 4549 | Vec1 != Vec1End; ++Vec1) |
| 4550 | for (BuiltinCandidateTypeSet::iterator |
| 4551 | Vec2 = CandidateTypes.vector_begin(), |
| 4552 | Vec2End = CandidateTypes.vector_end(); |
| 4553 | Vec2 != Vec2End; ++Vec2) { |
| 4554 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 4555 | QualType Result; |
| 4556 | if (isComparison) |
| 4557 | Result = Context.BoolTy; |
| 4558 | else { |
| 4559 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 4560 | Result = *Vec1; |
| 4561 | else |
| 4562 | Result = *Vec2; |
| 4563 | } |
| 4564 | |
| 4565 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4566 | } |
| 4567 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4568 | break; |
| 4569 | |
| 4570 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4571 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4572 | case OO_Caret: |
| 4573 | case OO_Pipe: |
| 4574 | case OO_LessLess: |
| 4575 | case OO_GreaterGreater: |
| 4576 | // C++ [over.built]p17: |
| 4577 | // |
| 4578 | // For every pair of promoted integral types L and R, there |
| 4579 | // exist candidate operator functions of the form |
| 4580 | // |
| 4581 | // LR operator%(L, R); |
| 4582 | // LR operator&(L, R); |
| 4583 | // LR operator^(L, R); |
| 4584 | // LR operator|(L, R); |
| 4585 | // L operator<<(L, R); |
| 4586 | // L operator>>(L, R); |
| 4587 | // |
| 4588 | // where LR is the result of the usual arithmetic conversions |
| 4589 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4590 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4591 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4592 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4593 | Right < LastPromotedIntegralType; ++Right) { |
| 4594 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4595 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4596 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4597 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4598 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4599 | } |
| 4600 | } |
| 4601 | break; |
| 4602 | |
| 4603 | case OO_Equal: |
| 4604 | // C++ [over.built]p20: |
| 4605 | // |
| 4606 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4607 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4608 | // empty, there exist candidate operator functions of the form |
| 4609 | // |
| 4610 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4611 | for (BuiltinCandidateTypeSet::iterator |
| 4612 | Enum = CandidateTypes.enumeration_begin(), |
| 4613 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4614 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4616 | CandidateSet); |
| 4617 | for (BuiltinCandidateTypeSet::iterator |
| 4618 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4619 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4620 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4621 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4622 | CandidateSet); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4623 | |
| 4624 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4625 | |
| 4626 | case OO_PlusEqual: |
| 4627 | case OO_MinusEqual: |
| 4628 | // C++ [over.built]p19: |
| 4629 | // |
| 4630 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4631 | // volatile or empty, there exist candidate operator functions |
| 4632 | // of the form |
| 4633 | // |
| 4634 | // T*VQ& operator=(T*VQ&, T*); |
| 4635 | // |
| 4636 | // C++ [over.built]p21: |
| 4637 | // |
| 4638 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4639 | // cv-unqualified object type and VQ is either volatile or |
| 4640 | // empty, there exist candidate operator functions of the form |
| 4641 | // |
| 4642 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4643 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4644 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4645 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4646 | QualType ParamTypes[2]; |
| 4647 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4648 | |
| 4649 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4650 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4651 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4652 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4653 | |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4654 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4655 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4656 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4657 | ParamTypes[0] |
| 4658 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4659 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4660 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4661 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4662 | } |
| 4663 | // Fall through. |
| 4664 | |
| 4665 | case OO_StarEqual: |
| 4666 | case OO_SlashEqual: |
| 4667 | // C++ [over.built]p18: |
| 4668 | // |
| 4669 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 4670 | // VQ is either volatile or empty, and R is a promoted |
| 4671 | // arithmetic type, there exist candidate operator functions of |
| 4672 | // the form |
| 4673 | // |
| 4674 | // VQ L& operator=(VQ L&, R); |
| 4675 | // VQ L& operator*=(VQ L&, R); |
| 4676 | // VQ L& operator/=(VQ L&, R); |
| 4677 | // VQ L& operator+=(VQ L&, R); |
| 4678 | // VQ L& operator-=(VQ L&, R); |
| 4679 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4681 | Right < LastPromotedArithmeticType; ++Right) { |
| 4682 | QualType ParamTypes[2]; |
| 4683 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4684 | |
| 4685 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4686 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4687 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4688 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4689 | |
| 4690 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4691 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4692 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 4693 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4694 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4695 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4696 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4697 | } |
| 4698 | } |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4699 | |
| 4700 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 4701 | for (BuiltinCandidateTypeSet::iterator Vec1 = CandidateTypes.vector_begin(), |
| 4702 | Vec1End = CandidateTypes.vector_end(); |
| 4703 | Vec1 != Vec1End; ++Vec1) |
| 4704 | for (BuiltinCandidateTypeSet::iterator |
| 4705 | Vec2 = CandidateTypes.vector_begin(), |
| 4706 | Vec2End = CandidateTypes.vector_end(); |
| 4707 | Vec2 != Vec2End; ++Vec2) { |
| 4708 | QualType ParamTypes[2]; |
| 4709 | ParamTypes[1] = *Vec2; |
| 4710 | // Add this built-in operator as a candidate (VQ is empty). |
| 4711 | ParamTypes[0] = Context.getLValueReferenceType(*Vec1); |
| 4712 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4713 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4714 | |
| 4715 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4716 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4717 | ParamTypes[0] = Context.getVolatileType(*Vec1); |
| 4718 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4719 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4720 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4721 | } |
| 4722 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4723 | break; |
| 4724 | |
| 4725 | case OO_PercentEqual: |
| 4726 | case OO_LessLessEqual: |
| 4727 | case OO_GreaterGreaterEqual: |
| 4728 | case OO_AmpEqual: |
| 4729 | case OO_CaretEqual: |
| 4730 | case OO_PipeEqual: |
| 4731 | // C++ [over.built]p22: |
| 4732 | // |
| 4733 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 4734 | // is either volatile or empty, and R is a promoted integral |
| 4735 | // type, there exist candidate operator functions of the form |
| 4736 | // |
| 4737 | // VQ L& operator%=(VQ L&, R); |
| 4738 | // VQ L& operator<<=(VQ L&, R); |
| 4739 | // VQ L& operator>>=(VQ L&, R); |
| 4740 | // VQ L& operator&=(VQ L&, R); |
| 4741 | // VQ L& operator^=(VQ L&, R); |
| 4742 | // VQ L& operator|=(VQ L&, R); |
| 4743 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4745 | Right < LastPromotedIntegralType; ++Right) { |
| 4746 | QualType ParamTypes[2]; |
| 4747 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4748 | |
| 4749 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4750 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4751 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a4a9334 | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4752 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4753 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4754 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4755 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4756 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4757 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4758 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4759 | } |
| 4760 | } |
| 4761 | break; |
| 4762 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4763 | case OO_Exclaim: { |
| 4764 | // C++ [over.operator]p23: |
| 4765 | // |
| 4766 | // There also exist candidate operator functions of the form |
| 4767 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4769 | // bool operator&&(bool, bool); [BELOW] |
| 4770 | // bool operator||(bool, bool); [BELOW] |
| 4771 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4772 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4773 | /*IsAssignmentOperator=*/false, |
| 4774 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4775 | break; |
| 4776 | } |
| 4777 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4778 | case OO_AmpAmp: |
| 4779 | case OO_PipePipe: { |
| 4780 | // C++ [over.operator]p23: |
| 4781 | // |
| 4782 | // There also exist candidate operator functions of the form |
| 4783 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4784 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4785 | // bool operator&&(bool, bool); |
| 4786 | // bool operator||(bool, bool); |
| 4787 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4788 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4789 | /*IsAssignmentOperator=*/false, |
| 4790 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4791 | break; |
| 4792 | } |
| 4793 | |
| 4794 | case OO_Subscript: |
| 4795 | // C++ [over.built]p13: |
| 4796 | // |
| 4797 | // For every cv-qualified or cv-unqualified object type T there |
| 4798 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4799 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4800 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4801 | // T& operator[](T*, ptrdiff_t); |
| 4802 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4803 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4804 | // T& operator[](ptrdiff_t, T*); |
| 4805 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4806 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4807 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4808 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4809 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4810 | |
| 4811 | // T& operator[](T*, ptrdiff_t) |
| 4812 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4813 | |
| 4814 | // T& operator[](ptrdiff_t, T*); |
| 4815 | ParamTypes[0] = ParamTypes[1]; |
| 4816 | ParamTypes[1] = *Ptr; |
| 4817 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4818 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4819 | break; |
| 4820 | |
| 4821 | case OO_ArrowStar: |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4822 | // C++ [over.built]p11: |
| 4823 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4824 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4825 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4826 | // there exist candidate operator functions of the form |
| 4827 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4828 | // where CV12 is the union of CV1 and CV2. |
| 4829 | { |
| 4830 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4831 | CandidateTypes.pointer_begin(); |
| 4832 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4833 | QualType C1Ty = (*Ptr); |
| 4834 | QualType C1; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4835 | QualifierCollector Q1; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4836 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4837 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4838 | if (!isa<RecordType>(C1)) |
| 4839 | continue; |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4840 | // heuristic to reduce number of builtin candidates in the set. |
| 4841 | // Add volatile/restrict version only if there are conversions to a |
| 4842 | // volatile/restrict type. |
| 4843 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4844 | continue; |
| 4845 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4846 | continue; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4847 | } |
| 4848 | for (BuiltinCandidateTypeSet::iterator |
| 4849 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4850 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4851 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4852 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4853 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 12df37c | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4854 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4855 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4856 | break; |
| 4857 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4858 | // build CV12 T& |
| 4859 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4860 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4861 | T.isVolatileQualified()) |
| 4862 | continue; |
| 4863 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4864 | T.isRestrictQualified()) |
| 4865 | continue; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4866 | T = Q1.apply(T); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4867 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4868 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4869 | } |
| 4870 | } |
| 4871 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4872 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4873 | |
| 4874 | case OO_Conditional: |
| 4875 | // Note that we don't consider the first argument, since it has been |
| 4876 | // contextually converted to bool long ago. The candidates below are |
| 4877 | // therefore added as binary. |
| 4878 | // |
| 4879 | // C++ [over.built]p24: |
| 4880 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4881 | // there exist candidate operator functions of the form |
| 4882 | // |
| 4883 | // T operator?(bool, T, T); |
| 4884 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4885 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4886 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4887 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4888 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4889 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4890 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4891 | CandidateTypes.member_pointer_begin(), |
| 4892 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4893 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4894 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4895 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4896 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4900 | /// \brief Add function candidates found via argument-dependent lookup |
| 4901 | /// to the set of overloading candidates. |
| 4902 | /// |
| 4903 | /// This routine performs argument-dependent name lookup based on the |
| 4904 | /// given function name (which may also be an operator name) and adds |
| 4905 | /// all of the overload candidates found by ADL to the overload |
| 4906 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4908 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4909 | bool Operator, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4910 | Expr **Args, unsigned NumArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4911 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4912 | OverloadCandidateSet& CandidateSet, |
| 4913 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4914 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4915 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 4916 | // FIXME: This approach for uniquing ADL results (and removing |
| 4917 | // redundant candidates from the set) relies on pointer-equality, |
| 4918 | // which means we need to key off the canonical decl. However, |
| 4919 | // always going back to the canonical decl might not get us the |
| 4920 | // right set of default arguments. What default arguments are |
| 4921 | // we supposed to consider on ADL candidates, anyway? |
| 4922 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4923 | // FIXME: Pass in the explicit template arguments? |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4924 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4925 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4926 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4927 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4928 | CandEnd = CandidateSet.end(); |
| 4929 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4930 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4931 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4932 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4933 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4934 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4935 | |
| 4936 | // For each of the ADL candidates we found, add it to the overload |
| 4937 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4938 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4939 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4940 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4941 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4942 | continue; |
| 4943 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4944 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 4945 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4946 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4947 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4948 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4949 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4950 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4953 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4954 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4955 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4956 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4957 | const OverloadCandidate& Cand2, |
| 4958 | SourceLocation Loc) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4959 | // Define viable functions to be better candidates than non-viable |
| 4960 | // functions. |
| 4961 | if (!Cand2.Viable) |
| 4962 | return Cand1.Viable; |
| 4963 | else if (!Cand1.Viable) |
| 4964 | return false; |
| 4965 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4966 | // C++ [over.match.best]p1: |
| 4967 | // |
| 4968 | // -- if F is a static member function, ICS1(F) is defined such |
| 4969 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4970 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4971 | // better nor worse than ICS1(F). |
| 4972 | unsigned StartArg = 0; |
| 4973 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4974 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4975 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4976 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | // A viable function F1 is defined to be a better function than another |
| 4978 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4979 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4980 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4981 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4982 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4983 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4984 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4985 | Cand2.Conversions[ArgIdx])) { |
| 4986 | case ImplicitConversionSequence::Better: |
| 4987 | // Cand1 has a better conversion sequence. |
| 4988 | HasBetterConversion = true; |
| 4989 | break; |
| 4990 | |
| 4991 | case ImplicitConversionSequence::Worse: |
| 4992 | // Cand1 can't be better than Cand2. |
| 4993 | return false; |
| 4994 | |
| 4995 | case ImplicitConversionSequence::Indistinguishable: |
| 4996 | // Do nothing. |
| 4997 | break; |
| 4998 | } |
| 4999 | } |
| 5000 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5002 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5003 | if (HasBetterConversion) |
| 5004 | return true; |
| 5005 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5006 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5007 | // specialization, or, if not that, |
Douglas Gregor | ce21919b | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 5008 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5009 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 5010 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | |
| 5012 | // -- F1 and F2 are function template specializations, and the function |
| 5013 | // template for F1 is more specialized than the template for F2 |
| 5014 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 5015 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 5016 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 5017 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5018 | if (FunctionTemplateDecl *BetterTemplate |
| 5019 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 5020 | Cand2.Function->getPrimaryTemplate(), |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5021 | Loc, |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 5022 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 5023 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5024 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5025 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5026 | // -- the context is an initialization by user-defined conversion |
| 5027 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 5028 | // from the return type of F1 to the destination type (i.e., |
| 5029 | // the type of the entity being initialized) is a better |
| 5030 | // conversion sequence than the standard conversion sequence |
| 5031 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5032 | if (Cand1.Function && Cand2.Function && |
| 5033 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 5034 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 5035 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 5036 | Cand2.FinalConversion)) { |
| 5037 | case ImplicitConversionSequence::Better: |
| 5038 | // Cand1 has a better conversion sequence. |
| 5039 | return true; |
| 5040 | |
| 5041 | case ImplicitConversionSequence::Worse: |
| 5042 | // Cand1 can't be better than Cand2. |
| 5043 | return false; |
| 5044 | |
| 5045 | case ImplicitConversionSequence::Indistinguishable: |
| 5046 | // Do nothing |
| 5047 | break; |
| 5048 | } |
| 5049 | } |
| 5050 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5051 | return false; |
| 5052 | } |
| 5053 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5054 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5055 | /// within an overload candidate set. |
| 5056 | /// |
| 5057 | /// \param CandidateSet the set of candidate functions. |
| 5058 | /// |
| 5059 | /// \param Loc the location of the function name (or operator symbol) for |
| 5060 | /// which overload resolution occurs. |
| 5061 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5062 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5063 | /// function, Best points to the candidate function found. |
| 5064 | /// |
| 5065 | /// \returns The result of overload resolution. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5066 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 5067 | SourceLocation Loc, |
| 5068 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5069 | // Find the best viable function. |
| 5070 | Best = CandidateSet.end(); |
| 5071 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 5072 | Cand != CandidateSet.end(); ++Cand) { |
| 5073 | if (Cand->Viable) { |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5074 | if (Best == CandidateSet.end() || |
| 5075 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5076 | Best = Cand; |
| 5077 | } |
| 5078 | } |
| 5079 | |
| 5080 | // If we didn't find any viable functions, abort. |
| 5081 | if (Best == CandidateSet.end()) |
| 5082 | return OR_No_Viable_Function; |
| 5083 | |
| 5084 | // Make sure that this function is better than every other viable |
| 5085 | // function. If not, we have an ambiguity. |
| 5086 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 5087 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5089 | Cand != Best && |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5090 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5091 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5092 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 5093 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5094 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5096 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5097 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5098 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5099 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5100 | return OR_Deleted; |
| 5101 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5102 | // C++ [basic.def.odr]p2: |
| 5103 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5104 | // when referred to from a potentially-evaluated expression. [Note: this |
| 5105 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5106 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 5107 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 5108 | if (Best->Function) |
| 5109 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5110 | return OR_Success; |
| 5111 | } |
| 5112 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5113 | namespace { |
| 5114 | |
| 5115 | enum OverloadCandidateKind { |
| 5116 | oc_function, |
| 5117 | oc_method, |
| 5118 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5119 | oc_function_template, |
| 5120 | oc_method_template, |
| 5121 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5122 | oc_implicit_default_constructor, |
| 5123 | oc_implicit_copy_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5124 | oc_implicit_copy_assignment |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5125 | }; |
| 5126 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5127 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 5128 | FunctionDecl *Fn, |
| 5129 | std::string &Description) { |
| 5130 | bool isTemplate = false; |
| 5131 | |
| 5132 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 5133 | isTemplate = true; |
| 5134 | Description = S.getTemplateArgumentBindingsText( |
| 5135 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 5136 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5137 | |
| 5138 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5139 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5140 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5141 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5142 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 5143 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 5147 | // This actually gets spelled 'candidate function' for now, but |
| 5148 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5149 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5150 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5151 | |
| 5152 | assert(Meth->isCopyAssignment() |
| 5153 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5154 | return oc_implicit_copy_assignment; |
| 5155 | } |
| 5156 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5157 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | } // end anonymous namespace |
| 5161 | |
| 5162 | // Notes the location of an overload candidate. |
| 5163 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5164 | std::string FnDesc; |
| 5165 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 5166 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 5167 | << (unsigned) K << FnDesc; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5170 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 5171 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 5172 | /// target types of the conversion. |
| 5173 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 5174 | SourceLocation CaretLoc, |
| 5175 | const PartialDiagnostic &PDiag) { |
| 5176 | Diag(CaretLoc, PDiag) |
| 5177 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 5178 | for (AmbiguousConversionSequence::const_iterator |
| 5179 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 5180 | NoteOverloadCandidate(*I); |
| 5181 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5184 | namespace { |
| 5185 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5186 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 5187 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 5188 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5189 | assert(Cand->Function && "for now, candidate must be a function"); |
| 5190 | FunctionDecl *Fn = Cand->Function; |
| 5191 | |
| 5192 | // There's a conversion slot for the object argument if this is a |
| 5193 | // non-constructor method. Note that 'I' corresponds the |
| 5194 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5195 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5196 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5197 | if (I == 0) |
| 5198 | isObjectArgument = true; |
| 5199 | else |
| 5200 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5203 | std::string FnDesc; |
| 5204 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 5205 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5206 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 5207 | QualType FromTy = Conv.Bad.getFromType(); |
| 5208 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5209 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5210 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5211 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5212 | Expr *E = FromExpr->IgnoreParens(); |
| 5213 | if (isa<UnaryOperator>(E)) |
| 5214 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5215 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5216 | |
| 5217 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 5218 | << (unsigned) FnKind << FnDesc |
| 5219 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5220 | << ToTy << Name << I+1; |
| 5221 | return; |
| 5222 | } |
| 5223 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5224 | // Do some hand-waving analysis to see if the non-viability is due |
| 5225 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5226 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 5227 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 5228 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 5229 | CToTy = RT->getPointeeType(); |
| 5230 | else { |
| 5231 | // TODO: detect and diagnose the full richness of const mismatches. |
| 5232 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 5233 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 5234 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 5235 | } |
| 5236 | |
| 5237 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 5238 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 5239 | // It is dumb that we have to do this here. |
| 5240 | while (isa<ArrayType>(CFromTy)) |
| 5241 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5242 | while (isa<ArrayType>(CToTy)) |
| 5243 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5244 | |
| 5245 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 5246 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 5247 | |
| 5248 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 5249 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 5250 | << (unsigned) FnKind << FnDesc |
| 5251 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5252 | << FromTy |
| 5253 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 5254 | << (unsigned) isObjectArgument << I+1; |
| 5255 | return; |
| 5256 | } |
| 5257 | |
| 5258 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5259 | assert(CVR && "unexpected qualifiers mismatch"); |
| 5260 | |
| 5261 | if (isObjectArgument) { |
| 5262 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 5263 | << (unsigned) FnKind << FnDesc |
| 5264 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5265 | << FromTy << (CVR - 1); |
| 5266 | } else { |
| 5267 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 5268 | << (unsigned) FnKind << FnDesc |
| 5269 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5270 | << FromTy << (CVR - 1) << I+1; |
| 5271 | } |
| 5272 | return; |
| 5273 | } |
| 5274 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5275 | // Diagnose references or pointers to incomplete types differently, |
| 5276 | // since it's far from impossible that the incompleteness triggered |
| 5277 | // the failure. |
| 5278 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 5279 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 5280 | TempFromTy = PTy->getPointeeType(); |
| 5281 | if (TempFromTy->isIncompleteType()) { |
| 5282 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 5283 | << (unsigned) FnKind << FnDesc |
| 5284 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5285 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 5286 | return; |
| 5287 | } |
| 5288 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5289 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5290 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 5291 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5292 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 5293 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 5297 | unsigned NumFormalArgs) { |
| 5298 | // TODO: treat calls to a missing default constructor as a special case |
| 5299 | |
| 5300 | FunctionDecl *Fn = Cand->Function; |
| 5301 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 5302 | |
| 5303 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 5304 | |
| 5305 | // at least / at most / exactly |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5306 | // FIXME: variadic templates "at most" should account for parameter packs |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5307 | unsigned mode, modeCount; |
| 5308 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5309 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 5310 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5311 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5312 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 5313 | mode = 0; // "at least" |
| 5314 | else |
| 5315 | mode = 2; // "exactly" |
| 5316 | modeCount = MinParams; |
| 5317 | } else { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5318 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 5319 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5320 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5321 | if (MinParams != FnTy->getNumArgs()) |
| 5322 | mode = 1; // "at most" |
| 5323 | else |
| 5324 | mode = 2; // "exactly" |
| 5325 | modeCount = FnTy->getNumArgs(); |
| 5326 | } |
| 5327 | |
| 5328 | std::string Description; |
| 5329 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 5330 | |
| 5331 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5332 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 5333 | << modeCount << NumFormalArgs; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5336 | /// Diagnose a failed template-argument deduction. |
| 5337 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 5338 | Expr **Args, unsigned NumArgs) { |
| 5339 | FunctionDecl *Fn = Cand->Function; // pattern |
| 5340 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5341 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5342 | NamedDecl *ParamD; |
| 5343 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 5344 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 5345 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5346 | switch (Cand->DeductionFailure.Result) { |
| 5347 | case Sema::TDK_Success: |
| 5348 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 5349 | |
| 5350 | case Sema::TDK_Incomplete: { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5351 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 5352 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 5353 | << ParamD->getDeclName(); |
| 5354 | return; |
| 5355 | } |
| 5356 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5357 | case Sema::TDK_Inconsistent: |
| 5358 | case Sema::TDK_InconsistentQuals: { |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5359 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5360 | int which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5361 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5362 | which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5363 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5364 | which = 1; |
| 5365 | else { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5366 | which = 2; |
| 5367 | } |
| 5368 | |
| 5369 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
| 5370 | << which << ParamD->getDeclName() |
| 5371 | << *Cand->DeductionFailure.getFirstArg() |
| 5372 | << *Cand->DeductionFailure.getSecondArg(); |
| 5373 | return; |
| 5374 | } |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5375 | |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5376 | case Sema::TDK_InvalidExplicitArguments: |
| 5377 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| 5378 | if (ParamD->getDeclName()) |
| 5379 | S.Diag(Fn->getLocation(), |
| 5380 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 5381 | << ParamD->getDeclName(); |
| 5382 | else { |
| 5383 | int index = 0; |
| 5384 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 5385 | index = TTP->getIndex(); |
| 5386 | else if (NonTypeTemplateParmDecl *NTTP |
| 5387 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 5388 | index = NTTP->getIndex(); |
| 5389 | else |
| 5390 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| 5391 | S.Diag(Fn->getLocation(), |
| 5392 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 5393 | << (index + 1); |
| 5394 | } |
| 5395 | return; |
| 5396 | |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5397 | case Sema::TDK_TooManyArguments: |
| 5398 | case Sema::TDK_TooFewArguments: |
| 5399 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 5400 | return; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 5401 | |
| 5402 | case Sema::TDK_InstantiationDepth: |
| 5403 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
| 5404 | return; |
| 5405 | |
| 5406 | case Sema::TDK_SubstitutionFailure: { |
| 5407 | std::string ArgString; |
| 5408 | if (TemplateArgumentList *Args |
| 5409 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 5410 | ArgString = S.getTemplateArgumentBindingsText( |
| 5411 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 5412 | *Args); |
| 5413 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 5414 | << ArgString; |
| 5415 | return; |
| 5416 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5417 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5418 | // TODO: diagnose these individually, then kill off |
| 5419 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5420 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5421 | case Sema::TDK_FailedOverloadResolution: |
| 5422 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 5423 | return; |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 5428 | /// already generated a primary error at the call site. |
| 5429 | /// |
| 5430 | /// It really does need to be a single diagnostic with its caret |
| 5431 | /// pointed at the candidate declaration. Yes, this creates some |
| 5432 | /// major challenges of technical writing. Yes, this makes pointing |
| 5433 | /// out problems with specific arguments quite awkward. It's still |
| 5434 | /// better than generating twenty screens of text for every failed |
| 5435 | /// overload. |
| 5436 | /// |
| 5437 | /// It would be great to be able to express per-candidate problems |
| 5438 | /// more richly for those diagnostic clients that cared, but we'd |
| 5439 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5440 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 5441 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5442 | FunctionDecl *Fn = Cand->Function; |
| 5443 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5444 | // Note deleted candidates, but only if they're viable. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5445 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5446 | std::string FnDesc; |
| 5447 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5448 | |
| 5449 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5450 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5451 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5454 | // We don't really have anything else to say about viable candidates. |
| 5455 | if (Cand->Viable) { |
| 5456 | S.NoteOverloadCandidate(Fn); |
| 5457 | return; |
| 5458 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5459 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5460 | switch (Cand->FailureKind) { |
| 5461 | case ovl_fail_too_many_arguments: |
| 5462 | case ovl_fail_too_few_arguments: |
| 5463 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5464 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5465 | case ovl_fail_bad_deduction: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5466 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 5467 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5468 | case ovl_fail_trivial_conversion: |
| 5469 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5470 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5471 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5472 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5473 | case ovl_fail_bad_conversion: { |
| 5474 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 5475 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5476 | if (Cand->Conversions[I].isBad()) |
| 5477 | return DiagnoseBadConversion(S, Cand, I); |
| 5478 | |
| 5479 | // FIXME: this currently happens when we're called from SemaInit |
| 5480 | // when user-conversion overload fails. Figure out how to handle |
| 5481 | // those conditions and diagnose them well. |
| 5482 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5483 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5484 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 5488 | // Desugar the type of the surrogate down to a function type, |
| 5489 | // retaining as many typedefs as possible while still showing |
| 5490 | // the function type (and, therefore, its parameter types). |
| 5491 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 5492 | bool isLValueReference = false; |
| 5493 | bool isRValueReference = false; |
| 5494 | bool isPointer = false; |
| 5495 | if (const LValueReferenceType *FnTypeRef = |
| 5496 | FnType->getAs<LValueReferenceType>()) { |
| 5497 | FnType = FnTypeRef->getPointeeType(); |
| 5498 | isLValueReference = true; |
| 5499 | } else if (const RValueReferenceType *FnTypeRef = |
| 5500 | FnType->getAs<RValueReferenceType>()) { |
| 5501 | FnType = FnTypeRef->getPointeeType(); |
| 5502 | isRValueReference = true; |
| 5503 | } |
| 5504 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5505 | FnType = FnTypePtr->getPointeeType(); |
| 5506 | isPointer = true; |
| 5507 | } |
| 5508 | // Desugar down to a function type. |
| 5509 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5510 | // Reconstruct the pointer/reference as appropriate. |
| 5511 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5512 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5513 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5514 | |
| 5515 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5516 | << FnType; |
| 5517 | } |
| 5518 | |
| 5519 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5520 | const char *Opc, |
| 5521 | SourceLocation OpLoc, |
| 5522 | OverloadCandidate *Cand) { |
| 5523 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5524 | std::string TypeStr("operator"); |
| 5525 | TypeStr += Opc; |
| 5526 | TypeStr += "("; |
| 5527 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5528 | if (Cand->Conversions.size() == 1) { |
| 5529 | TypeStr += ")"; |
| 5530 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5531 | } else { |
| 5532 | TypeStr += ", "; |
| 5533 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5534 | TypeStr += ")"; |
| 5535 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5540 | OverloadCandidate *Cand) { |
| 5541 | unsigned NoOperands = Cand->Conversions.size(); |
| 5542 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5543 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5544 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5545 | if (!ICS.isAmbiguous()) continue; |
| 5546 | |
| 5547 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5548 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5552 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5553 | if (Cand->Function) |
| 5554 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5555 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5556 | return Cand->Surrogate->getLocation(); |
| 5557 | return SourceLocation(); |
| 5558 | } |
| 5559 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5560 | struct CompareOverloadCandidatesForDisplay { |
| 5561 | Sema &S; |
| 5562 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5563 | |
| 5564 | bool operator()(const OverloadCandidate *L, |
| 5565 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5566 | // Fast-path this check. |
| 5567 | if (L == R) return false; |
| 5568 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5569 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5570 | if (L->Viable) { |
| 5571 | if (!R->Viable) return true; |
| 5572 | |
| 5573 | // TODO: introduce a tri-valued comparison for overload |
| 5574 | // candidates. Would be more worthwhile if we had a sort |
| 5575 | // that could exploit it. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5576 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 5577 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5578 | } else if (R->Viable) |
| 5579 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5580 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5581 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5582 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5583 | // Criteria by which we can sort non-viable candidates: |
| 5584 | if (!L->Viable) { |
| 5585 | // 1. Arity mismatches come after other candidates. |
| 5586 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5587 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5588 | return false; |
| 5589 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5590 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5591 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5592 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5593 | // 2. Bad conversions come first and are ordered by the number |
| 5594 | // of bad conversions and quality of good conversions. |
| 5595 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5596 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5597 | return true; |
| 5598 | |
| 5599 | // If there's any ordering between the defined conversions... |
| 5600 | // FIXME: this might not be transitive. |
| 5601 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5602 | |
| 5603 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5604 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5605 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5606 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 5607 | R->Conversions[I])) { |
| 5608 | case ImplicitConversionSequence::Better: |
| 5609 | leftBetter++; |
| 5610 | break; |
| 5611 | |
| 5612 | case ImplicitConversionSequence::Worse: |
| 5613 | leftBetter--; |
| 5614 | break; |
| 5615 | |
| 5616 | case ImplicitConversionSequence::Indistinguishable: |
| 5617 | break; |
| 5618 | } |
| 5619 | } |
| 5620 | if (leftBetter > 0) return true; |
| 5621 | if (leftBetter < 0) return false; |
| 5622 | |
| 5623 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 5624 | return false; |
| 5625 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5626 | // TODO: others? |
| 5627 | } |
| 5628 | |
| 5629 | // Sort everything else by location. |
| 5630 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 5631 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 5632 | |
| 5633 | // Put candidates without locations (e.g. builtins) at the end. |
| 5634 | if (LLoc.isInvalid()) return false; |
| 5635 | if (RLoc.isInvalid()) return true; |
| 5636 | |
| 5637 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5638 | } |
| 5639 | }; |
| 5640 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5641 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 5642 | /// computes up to the first |
| 5643 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 5644 | Expr **Args, unsigned NumArgs) { |
| 5645 | assert(!Cand->Viable); |
| 5646 | |
| 5647 | // Don't do anything on failures other than bad conversion. |
| 5648 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 5649 | |
| 5650 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5651 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5652 | unsigned ConvCount = Cand->Conversions.size(); |
| 5653 | while (true) { |
| 5654 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 5655 | ConvIdx++; |
| 5656 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 5657 | break; |
| 5658 | } |
| 5659 | |
| 5660 | if (ConvIdx == ConvCount) |
| 5661 | return; |
| 5662 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5663 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 5664 | "remaining conversion is initialized?"); |
| 5665 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 5666 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5667 | // operation somehow. |
| 5668 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5669 | |
| 5670 | const FunctionProtoType* Proto; |
| 5671 | unsigned ArgIdx = ConvIdx; |
| 5672 | |
| 5673 | if (Cand->IsSurrogate) { |
| 5674 | QualType ConvType |
| 5675 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 5676 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5677 | ConvType = ConvPtrType->getPointeeType(); |
| 5678 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 5679 | ArgIdx--; |
| 5680 | } else if (Cand->Function) { |
| 5681 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 5682 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 5683 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 5684 | ArgIdx--; |
| 5685 | } else { |
| 5686 | // Builtin binary operator with a bad first conversion. |
| 5687 | assert(ConvCount <= 3); |
| 5688 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 5689 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5690 | = TryCopyInitialization(S, Args[ConvIdx], |
| 5691 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 5692 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5693 | /*InOverloadResolution*/ true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5694 | return; |
| 5695 | } |
| 5696 | |
| 5697 | // Fill in the rest of the conversions. |
| 5698 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5699 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 5700 | if (ArgIdx < NumArgsInProto) |
| 5701 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5702 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 5703 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5704 | /*InOverloadResolution=*/true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5705 | else |
| 5706 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 5707 | } |
| 5708 | } |
| 5709 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5710 | } // end anonymous namespace |
| 5711 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5712 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 5713 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5714 | /// set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5716 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5717 | OverloadCandidateDisplayKind OCD, |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5718 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5719 | const char *Opc, |
Fariborz Jahanian | 29f9d39 | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 5720 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5721 | // Sort the candidates by viability and position. Sorting directly would |
| 5722 | // be prohibitive, so we make a set of pointers and sort those. |
| 5723 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 5724 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 5725 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5726 | LastCand = CandidateSet.end(); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5727 | Cand != LastCand; ++Cand) { |
| 5728 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5729 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5730 | else if (OCD == OCD_AllCandidates) { |
| 5731 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5732 | if (Cand->Function || Cand->IsSurrogate) |
| 5733 | Cands.push_back(Cand); |
| 5734 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 5735 | // want to list every possible builtin candidate. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5736 | } |
| 5737 | } |
| 5738 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5739 | std::sort(Cands.begin(), Cands.end(), |
| 5740 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5741 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5742 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5743 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5744 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5745 | const Diagnostic::OverloadsShown ShowOverloads = Diags.getShowOverloads(); |
| 5746 | unsigned CandsShown = 0; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5747 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 5748 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 5749 | |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5750 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 5751 | // the user with. FIXME: This limit should depend on details of the |
| 5752 | // candidate list. |
| 5753 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 5754 | break; |
| 5755 | } |
| 5756 | ++CandsShown; |
| 5757 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5758 | if (Cand->Function) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5759 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5760 | else if (Cand->IsSurrogate) |
| 5761 | NoteSurrogateCandidate(*this, Cand); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5762 | else { |
| 5763 | assert(Cand->Viable && |
| 5764 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5765 | // Generally we only see ambiguities including viable builtin |
| 5766 | // operators if overload resolution got screwed up by an |
| 5767 | // ambiguous user-defined conversion. |
| 5768 | // |
| 5769 | // FIXME: It's quite possible for different conversions to see |
| 5770 | // different ambiguities, though. |
| 5771 | if (!ReportedAmbiguousConversions) { |
| 5772 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 5773 | ReportedAmbiguousConversions = true; |
| 5774 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5775 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5776 | // If this is a viable builtin, print it. |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5777 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5778 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5779 | } |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 5780 | |
| 5781 | if (I != E) |
Jeffrey Yasskin | 5d474d0 | 2010-06-11 06:58:43 +0000 | [diff] [blame] | 5782 | Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5785 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5786 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5787 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5788 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5789 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5792 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 5793 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 5794 | /// expression with overloaded function type and @p ToType is the type |
| 5795 | /// we're trying to resolve to. For example: |
| 5796 | /// |
| 5797 | /// @code |
| 5798 | /// int f(double); |
| 5799 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5800 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5801 | /// int (*pfd)(double) = f; // selects f(double) |
| 5802 | /// @endcode |
| 5803 | /// |
| 5804 | /// This routine returns the resulting FunctionDecl if it could be |
| 5805 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 5806 | /// routine will emit diagnostics if there is an error. |
| 5807 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5808 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5809 | bool Complain, |
| 5810 | DeclAccessPair &FoundResult) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5811 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5812 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5813 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5814 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5815 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 5816 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5817 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5818 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5819 | FunctionType = MemTypePtr->getPointeeType(); |
| 5820 | IsMember = true; |
| 5821 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5823 | // C++ [over.over]p1: |
| 5824 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5825 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5826 | // C++ [over.over]p1: |
| 5827 | // [...] The overloaded function name can be preceded by the & |
| 5828 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5829 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5830 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 5831 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 5832 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 5833 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5834 | } |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5835 | |
| 5836 | // We expect a pointer or reference to function, or a function pointer. |
| 5837 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 5838 | if (!FunctionType->isFunctionType()) { |
| 5839 | if (Complain) |
| 5840 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 5841 | << OvlExpr->getName() << ToType; |
| 5842 | |
| 5843 | return 0; |
| 5844 | } |
| 5845 | |
| 5846 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5847 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5848 | // Look through all of the overloaded functions, searching for one |
| 5849 | // whose type matches exactly. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5850 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5851 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 5852 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5853 | bool FoundNonTemplateFunction = false; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5854 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5855 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5856 | // Look through any using declarations to find the underlying function. |
| 5857 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5858 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5859 | // C++ [over.over]p3: |
| 5860 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 5861 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5862 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5863 | // type "pointer-to-member-function." |
| 5864 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5865 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5866 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5867 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5868 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5869 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5870 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5871 | // static when converting to member pointer. |
| 5872 | if (Method->isStatic() == IsMember) |
| 5873 | continue; |
| 5874 | } else if (IsMember) |
| 5875 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5876 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5877 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5878 | // If the name is a function template, template argument deduction is |
| 5879 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5880 | // resulting template argument list is used to generate a single |
| 5881 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5882 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5883 | // FIXME: We don't really want to build the specialization here, do we? |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5884 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5885 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5886 | if (TemplateDeductionResult Result |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5887 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5888 | FunctionType, Specialization, Info)) { |
| 5889 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5890 | (void)Result; |
| 5891 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5892 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5893 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5894 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5895 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5896 | Matches.push_back(std::make_pair(I.getPair(), |
| 5897 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5898 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5899 | |
| 5900 | continue; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5901 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5902 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5903 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5904 | // Skip non-static functions when converting to pointer, and static |
| 5905 | // when converting to member pointer. |
| 5906 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5907 | continue; |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5908 | |
| 5909 | // If we have explicit template arguments, skip non-templates. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5910 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5911 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5912 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5913 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5914 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5915 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 5916 | QualType ResultTy; |
| 5917 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5918 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5919 | ResultTy)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5920 | Matches.push_back(std::make_pair(I.getPair(), |
| 5921 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5922 | FoundNonTemplateFunction = true; |
| 5923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5924 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5925 | } |
| 5926 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5927 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5928 | if (Matches.empty()) { |
| 5929 | if (Complain) { |
| 5930 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 5931 | << OvlExpr->getName() << FunctionType; |
| 5932 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5933 | E = OvlExpr->decls_end(); |
| 5934 | I != E; ++I) |
| 5935 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 5936 | NoteOverloadCandidate(F); |
| 5937 | } |
| 5938 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5939 | return 0; |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5940 | } else if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5941 | FunctionDecl *Result = Matches[0].second; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5942 | FoundResult = Matches[0].first; |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5943 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5944 | if (Complain) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5945 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5946 | return Result; |
| 5947 | } |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5948 | |
| 5949 | // C++ [over.over]p4: |
| 5950 | // If more than one function is selected, [...] |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5951 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5952 | // [...] and any given function template specialization F1 is |
| 5953 | // eliminated if the set contains a second function template |
| 5954 | // specialization whose function template is more specialized |
| 5955 | // than the function template of F1 according to the partial |
| 5956 | // ordering rules of 14.5.5.2. |
| 5957 | |
| 5958 | // The algorithm specified above is quadratic. We instead use a |
| 5959 | // two-pass algorithm (similar to the one used to identify the |
| 5960 | // best viable function in an overload set) that identifies the |
| 5961 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5962 | |
| 5963 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 5964 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5965 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5966 | |
| 5967 | UnresolvedSetIterator Result = |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5968 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5969 | TPOC_Other, From->getLocStart(), |
| 5970 | PDiag(), |
| 5971 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5972 | << Matches[0].second->getDeclName(), |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5973 | PDiag(diag::note_ovl_candidate) |
| 5974 | << (unsigned) oc_function_template); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5975 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5976 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5977 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5978 | if (Complain) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5979 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5980 | DiagnoseUseOfDecl(FoundResult, OvlExpr->getNameLoc()); |
| 5981 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5982 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5983 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5984 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5985 | // [...] any function template specializations in the set are |
| 5986 | // eliminated if the set also contains a non-template function, [...] |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5987 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5988 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5989 | ++I; |
| 5990 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5991 | Matches[I] = Matches[--N]; |
| 5992 | Matches.set_size(N); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5993 | } |
| 5994 | } |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5995 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5997 | // selected function. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5998 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5999 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6000 | FoundResult = Matches[0].first; |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6001 | if (Complain) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6002 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6003 | DiagnoseUseOfDecl(Matches[0].first, OvlExpr->getNameLoc()); |
| 6004 | } |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6005 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 6006 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6007 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 6008 | // FIXME: We should probably return the same thing that BestViableFunction |
| 6009 | // returns (even if we issue the diagnostics here). |
| 6010 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6011 | << Matches[0].second->getDeclName(); |
| 6012 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 6013 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6014 | return 0; |
| 6015 | } |
| 6016 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6017 | /// \brief Given an expression that refers to an overloaded function, try to |
| 6018 | /// resolve that overloaded function expression down to a single function. |
| 6019 | /// |
| 6020 | /// This routine can only resolve template-ids that refer to a single function |
| 6021 | /// template, where that template-id refers to a single template whose template |
| 6022 | /// arguments are either provided by the template-id or have defaults, |
| 6023 | /// as described in C++0x [temp.arg.explicit]p3. |
| 6024 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 6025 | // C++ [over.over]p1: |
| 6026 | // [...] [Note: any redundant set of parentheses surrounding the |
| 6027 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6028 | // C++ [over.over]p1: |
| 6029 | // [...] The overloaded function name can be preceded by the & |
| 6030 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6031 | |
| 6032 | if (From->getType() != Context.OverloadTy) |
| 6033 | return 0; |
| 6034 | |
| 6035 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6036 | |
| 6037 | // If we didn't actually find any template-ids, we're done. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6038 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6039 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6040 | |
| 6041 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 6042 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6043 | |
| 6044 | // Look through all of the overloaded functions, searching for one |
| 6045 | // whose type matches exactly. |
| 6046 | FunctionDecl *Matched = 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6047 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6048 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6049 | // C++0x [temp.arg.explicit]p3: |
| 6050 | // [...] In contexts where deduction is done and fails, or in contexts |
| 6051 | // where deduction is not done, if a template argument list is |
| 6052 | // specified and it, along with any default template arguments, |
| 6053 | // identifies a single function template specialization, then the |
| 6054 | // template-id is an lvalue for the function template specialization. |
| 6055 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 6056 | |
| 6057 | // C++ [over.over]p2: |
| 6058 | // If the name is a function template, template argument deduction is |
| 6059 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 6060 | // resulting template argument list is used to generate a single |
| 6061 | // function template specialization, which is added to the set of |
| 6062 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6063 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6064 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 6065 | if (TemplateDeductionResult Result |
| 6066 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 6067 | Specialization, Info)) { |
| 6068 | // FIXME: make a note of the failed deduction for diagnostics. |
| 6069 | (void)Result; |
| 6070 | continue; |
| 6071 | } |
| 6072 | |
| 6073 | // Multiple matches; we can't resolve to a single declaration. |
| 6074 | if (Matched) |
| 6075 | return 0; |
| 6076 | |
| 6077 | Matched = Specialization; |
| 6078 | } |
| 6079 | |
| 6080 | return Matched; |
| 6081 | } |
| 6082 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6083 | /// \brief Add a single candidate to the overload set. |
| 6084 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6085 | DeclAccessPair FoundDecl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6086 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6087 | Expr **Args, unsigned NumArgs, |
| 6088 | OverloadCandidateSet &CandidateSet, |
| 6089 | bool PartialOverloading) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6090 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6091 | if (isa<UsingShadowDecl>(Callee)) |
| 6092 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 6093 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6094 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6095 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6096 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6097 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6098 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | if (FunctionTemplateDecl *FuncTemplate |
| 6102 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6103 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 6104 | ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6105 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6106 | return; |
| 6107 | } |
| 6108 | |
| 6109 | assert(false && "unhandled case in overloaded call candidate"); |
| 6110 | |
| 6111 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6112 | } |
| 6113 | |
| 6114 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 6115 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6116 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6117 | Expr **Args, unsigned NumArgs, |
| 6118 | OverloadCandidateSet &CandidateSet, |
| 6119 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6120 | |
| 6121 | #ifndef NDEBUG |
| 6122 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 6123 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6124 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6125 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 6126 | // and let Y be the lookup set produced by argument dependent |
| 6127 | // lookup (defined as follows). If X contains |
| 6128 | // |
| 6129 | // -- a declaration of a class member, or |
| 6130 | // |
| 6131 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6132 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6133 | // |
| 6134 | // -- a declaration that is neither a function or a function |
| 6135 | // template |
| 6136 | // |
| 6137 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6138 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6139 | if (ULE->requiresADL()) { |
| 6140 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6141 | E = ULE->decls_end(); I != E; ++I) { |
| 6142 | assert(!(*I)->getDeclContext()->isRecord()); |
| 6143 | assert(isa<UsingShadowDecl>(*I) || |
| 6144 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 6145 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6146 | } |
| 6147 | } |
| 6148 | #endif |
| 6149 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6150 | // It would be nice to avoid this copy. |
| 6151 | TemplateArgumentListInfo TABuffer; |
| 6152 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6153 | if (ULE->hasExplicitTemplateArgs()) { |
| 6154 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6155 | ExplicitTemplateArgs = &TABuffer; |
| 6156 | } |
| 6157 | |
| 6158 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 6159 | E = ULE->decls_end(); I != E; ++I) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6160 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6161 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6162 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6163 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6164 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6165 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 6166 | Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6167 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6168 | CandidateSet, |
| 6169 | PartialOverloading); |
| 6170 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6171 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6172 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 6173 | Expr **Args, unsigned NumArgs) { |
| 6174 | Fn->Destroy(SemaRef.Context); |
| 6175 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 6176 | Args[Arg]->Destroy(SemaRef.Context); |
| 6177 | return SemaRef.ExprError(); |
| 6178 | } |
| 6179 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6180 | /// Attempts to recover from a call where no functions were found. |
| 6181 | /// |
| 6182 | /// Returns true if new candidates were found. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6183 | static Sema::OwningExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6184 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6185 | UnresolvedLookupExpr *ULE, |
| 6186 | SourceLocation LParenLoc, |
| 6187 | Expr **Args, unsigned NumArgs, |
| 6188 | SourceLocation *CommaLocs, |
| 6189 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6190 | |
| 6191 | CXXScopeSpec SS; |
| 6192 | if (ULE->getQualifier()) { |
| 6193 | SS.setScopeRep(ULE->getQualifier()); |
| 6194 | SS.setRange(ULE->getQualifierRange()); |
| 6195 | } |
| 6196 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6197 | TemplateArgumentListInfo TABuffer; |
| 6198 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 6199 | if (ULE->hasExplicitTemplateArgs()) { |
| 6200 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 6201 | ExplicitTemplateArgs = &TABuffer; |
| 6202 | } |
| 6203 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6204 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 6205 | Sema::LookupOrdinaryName); |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 6206 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6207 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6208 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6209 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 6210 | |
| 6211 | // Build an implicit member call if appropriate. Just drop the |
| 6212 | // casts and such from the call, we don't really care. |
| 6213 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 6214 | if ((*R.begin())->isCXXClassMember()) |
| 6215 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 6216 | else if (ExplicitTemplateArgs) |
| 6217 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 6218 | else |
| 6219 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 6220 | |
| 6221 | if (NewFn.isInvalid()) |
| 6222 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 6223 | |
| 6224 | Fn->Destroy(SemaRef.Context); |
| 6225 | |
| 6226 | // This shouldn't cause an infinite loop because we're giving it |
| 6227 | // an expression with non-empty lookup results, which should never |
| 6228 | // end up here. |
| 6229 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 6230 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 6231 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6232 | } |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 6233 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6234 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6235 | /// (which eventually refers to the declaration Func) and the call |
| 6236 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 6237 | /// to a specific function. If overload resolution succeeds, returns |
| 6238 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 6239 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6240 | /// arguments and Fn, and returns NULL. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6241 | Sema::OwningExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6242 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6243 | SourceLocation LParenLoc, |
| 6244 | Expr **Args, unsigned NumArgs, |
| 6245 | SourceLocation *CommaLocs, |
| 6246 | SourceLocation RParenLoc) { |
| 6247 | #ifndef NDEBUG |
| 6248 | if (ULE->requiresADL()) { |
| 6249 | // To do ADL, we must have found an unqualified name. |
| 6250 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 6251 | |
| 6252 | // We don't perform ADL for implicit declarations of builtins. |
| 6253 | // Verify that this was correctly set up. |
| 6254 | FunctionDecl *F; |
| 6255 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 6256 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 6257 | F->getBuiltinID() && F->isImplicit()) |
| 6258 | assert(0 && "performing ADL for builtin"); |
| 6259 | |
| 6260 | // We don't perform ADL in C. |
| 6261 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 6262 | } |
| 6263 | #endif |
| 6264 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6265 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 6266 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6267 | // Add the functions denoted by the callee to the set of candidate |
| 6268 | // functions, including those from argument-dependent lookup. |
| 6269 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6270 | |
| 6271 | // If we found nothing, try to recover. |
| 6272 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 6273 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6274 | if (CandidateSet.empty()) |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6275 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6276 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6277 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6278 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6279 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6280 | case OR_Success: { |
| 6281 | FunctionDecl *FDecl = Best->Function; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6282 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6283 | DiagnoseUseOfDecl(Best->FoundDecl, ULE->getNameLoc()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6284 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6285 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 6286 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6287 | |
| 6288 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 6289 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6290 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6291 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6292 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6293 | break; |
| 6294 | |
| 6295 | case OR_Ambiguous: |
| 6296 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6297 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6298 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6299 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6300 | |
| 6301 | case OR_Deleted: |
| 6302 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 6303 | << Best->Function->isDeleted() |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6304 | << ULE->getName() |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6305 | << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6306 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6307 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
| 6310 | // Overload resolution failed. Destroy all of the subexpressions and |
| 6311 | // return NULL. |
| 6312 | Fn->Destroy(Context); |
| 6313 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 6314 | Args[Arg]->Destroy(Context); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6315 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6318 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6319 | return Functions.size() > 1 || |
| 6320 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 6321 | } |
| 6322 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6323 | /// \brief Create a unary operation that may resolve to an overloaded |
| 6324 | /// operator. |
| 6325 | /// |
| 6326 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 6327 | /// |
| 6328 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 6329 | /// operator. |
| 6330 | /// |
| 6331 | /// \param Functions The set of non-member functions that will be |
| 6332 | /// considered by overload resolution. The caller needs to build this |
| 6333 | /// set based on the context using, e.g., |
| 6334 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6335 | /// set should not contain any member functions; those will be added |
| 6336 | /// by CreateOverloadedUnaryOp(). |
| 6337 | /// |
| 6338 | /// \param input The input argument. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6339 | Sema::OwningExprResult |
| 6340 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 6341 | const UnresolvedSetImpl &Fns, |
| 6342 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6343 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 6344 | Expr *Input = (Expr *)input.get(); |
| 6345 | |
| 6346 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 6347 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 6348 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6349 | |
| 6350 | Expr *Args[2] = { Input, 0 }; |
| 6351 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6353 | // For post-increment and post-decrement, add the implicit '0' as |
| 6354 | // the second argument, so that we know this is a post-increment or |
| 6355 | // post-decrement. |
| 6356 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 6357 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6358 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6359 | SourceLocation()); |
| 6360 | NumArgs = 2; |
| 6361 | } |
| 6362 | |
| 6363 | if (Input->isTypeDependent()) { |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame^] | 6364 | if (Fns.empty()) |
| 6365 | return Owned(new (Context) UnaryOperator(input.takeAs<Expr>(), |
| 6366 | Opc, |
| 6367 | Context.DependentTy, |
| 6368 | OpLoc)); |
| 6369 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6370 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6371 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6372 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6373 | 0, SourceRange(), OpName, OpLoc, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6374 | /*ADL*/ true, IsOverloaded(Fns), |
| 6375 | Fns.begin(), Fns.end()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6376 | input.release(); |
| 6377 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 6378 | &Args[0], NumArgs, |
| 6379 | Context.DependentTy, |
| 6380 | OpLoc)); |
| 6381 | } |
| 6382 | |
| 6383 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6384 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6385 | |
| 6386 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6387 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6388 | |
| 6389 | // Add operator candidates that are member functions. |
| 6390 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 6391 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6392 | // Add candidates from ADL. |
| 6393 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | 6ec89d4 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 6394 | Args, NumArgs, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6395 | /*ExplicitTemplateArgs*/ 0, |
| 6396 | CandidateSet); |
| 6397 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6398 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6399 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6400 | |
| 6401 | // Perform overload resolution. |
| 6402 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6403 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6404 | case OR_Success: { |
| 6405 | // We found a built-in operator or an overloaded operator. |
| 6406 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6408 | if (FnDecl) { |
| 6409 | // We matched an overloaded operator. Build a call to that |
| 6410 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6411 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6412 | // Convert the arguments. |
| 6413 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6414 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6415 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6416 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 6417 | Best->FoundDecl, Method)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6418 | return ExprError(); |
| 6419 | } else { |
| 6420 | // Convert the arguments. |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6421 | OwningExprResult InputInit |
| 6422 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6423 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6424 | SourceLocation(), |
| 6425 | move(input)); |
| 6426 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6427 | return ExprError(); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6429 | input = move(InputInit); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6430 | Input = (Expr *)input.get(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6433 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6434 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6435 | // Determine the result type |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6436 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6437 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6438 | // Build the actual expression node. |
| 6439 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6440 | SourceLocation()); |
| 6441 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6442 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6443 | input.release(); |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6444 | Args[0] = Input; |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6445 | ExprOwningPtr<CallExpr> TheCall(this, |
| 6446 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6447 | Args, NumArgs, ResultTy, OpLoc)); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6448 | |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6449 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6450 | FnDecl)) |
| 6451 | return ExprError(); |
| 6452 | |
| 6453 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6454 | } else { |
| 6455 | // We matched a built-in operator. Convert the arguments, then |
| 6456 | // break out so that we will build the appropriate built-in |
| 6457 | // operator node. |
| 6458 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6459 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6460 | return ExprError(); |
| 6461 | |
| 6462 | break; |
| 6463 | } |
| 6464 | } |
| 6465 | |
| 6466 | case OR_No_Viable_Function: |
| 6467 | // No viable function; fall through to handling this as a |
| 6468 | // built-in operator, which will produce an error message for us. |
| 6469 | break; |
| 6470 | |
| 6471 | case OR_Ambiguous: |
| 6472 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6473 | << UnaryOperator::getOpcodeStr(Opc) |
| 6474 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6475 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6476 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6477 | return ExprError(); |
| 6478 | |
| 6479 | case OR_Deleted: |
| 6480 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6481 | << Best->Function->isDeleted() |
| 6482 | << UnaryOperator::getOpcodeStr(Opc) |
| 6483 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6484 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6485 | return ExprError(); |
| 6486 | } |
| 6487 | |
| 6488 | // Either we found no viable overloaded operator or we matched a |
| 6489 | // built-in operator. In either case, fall through to trying to |
| 6490 | // build a built-in operation. |
| 6491 | input.release(); |
| 6492 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 6493 | } |
| 6494 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6495 | /// \brief Create a binary operation that may resolve to an overloaded |
| 6496 | /// operator. |
| 6497 | /// |
| 6498 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 6499 | /// |
| 6500 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 6501 | /// operator. |
| 6502 | /// |
| 6503 | /// \param Functions The set of non-member functions that will be |
| 6504 | /// considered by overload resolution. The caller needs to build this |
| 6505 | /// set based on the context using, e.g., |
| 6506 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6507 | /// set should not contain any member functions; those will be added |
| 6508 | /// by CreateOverloadedBinOp(). |
| 6509 | /// |
| 6510 | /// \param LHS Left-hand argument. |
| 6511 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6512 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6513 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6514 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6515 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6516 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6517 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6518 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6519 | |
| 6520 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 6521 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 6522 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6523 | |
| 6524 | // If either side is type-dependent, create an appropriate dependent |
| 6525 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6526 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6527 | if (Fns.empty()) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6528 | // If there are no functions to store, just build a dependent |
| 6529 | // BinaryOperator or CompoundAssignment. |
| 6530 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 6531 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6532 | Context.DependentTy, OpLoc)); |
| 6533 | |
| 6534 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6535 | Context.DependentTy, |
| 6536 | Context.DependentTy, |
| 6537 | Context.DependentTy, |
| 6538 | OpLoc)); |
| 6539 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6540 | |
| 6541 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6542 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6543 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6544 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6545 | 0, SourceRange(), OpName, OpLoc, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6546 | /*ADL*/ true, IsOverloaded(Fns), |
| 6547 | Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6548 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6549 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6550 | Context.DependentTy, |
| 6551 | OpLoc)); |
| 6552 | } |
| 6553 | |
| 6554 | // If this is the .* operator, which is not overloadable, just |
| 6555 | // create a built-in binary operator. |
| 6556 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6557 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6558 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6559 | // If this is the assignment operator, we only perform overload resolution |
| 6560 | // if the left-hand side is a class or enumeration type. This is actually |
| 6561 | // a hack. The standard requires that we do overload resolution between the |
| 6562 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6563 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6564 | // Note that we go the traditional code path for compound assignment forms. |
| 6565 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6566 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6567 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6568 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6569 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6570 | |
| 6571 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6572 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6573 | |
| 6574 | // Add operator candidates that are member functions. |
| 6575 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6576 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6577 | // Add candidates from ADL. |
| 6578 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6579 | Args, 2, |
| 6580 | /*ExplicitTemplateArgs*/ 0, |
| 6581 | CandidateSet); |
| 6582 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6583 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6584 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6585 | |
| 6586 | // Perform overload resolution. |
| 6587 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6588 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6589 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6590 | // We found a built-in operator or an overloaded operator. |
| 6591 | FunctionDecl *FnDecl = Best->Function; |
| 6592 | |
| 6593 | if (FnDecl) { |
| 6594 | // We matched an overloaded operator. Build a call to that |
| 6595 | // operator. |
| 6596 | |
| 6597 | // Convert the arguments. |
| 6598 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6599 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6600 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6601 | |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6602 | OwningExprResult Arg1 |
| 6603 | = PerformCopyInitialization( |
| 6604 | InitializedEntity::InitializeParameter( |
| 6605 | FnDecl->getParamDecl(0)), |
| 6606 | SourceLocation(), |
| 6607 | Owned(Args[1])); |
| 6608 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6609 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6610 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6611 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6612 | Best->FoundDecl, Method)) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6613 | return ExprError(); |
| 6614 | |
| 6615 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6616 | } else { |
| 6617 | // Convert the arguments. |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6618 | OwningExprResult Arg0 |
| 6619 | = PerformCopyInitialization( |
| 6620 | InitializedEntity::InitializeParameter( |
| 6621 | FnDecl->getParamDecl(0)), |
| 6622 | SourceLocation(), |
| 6623 | Owned(Args[0])); |
| 6624 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6625 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6626 | |
| 6627 | OwningExprResult Arg1 |
| 6628 | = PerformCopyInitialization( |
| 6629 | InitializedEntity::InitializeParameter( |
| 6630 | FnDecl->getParamDecl(1)), |
| 6631 | SourceLocation(), |
| 6632 | Owned(Args[1])); |
| 6633 | if (Arg1.isInvalid()) |
| 6634 | return ExprError(); |
| 6635 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 6636 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6639 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6640 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6641 | // Determine the result type |
| 6642 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6643 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6644 | ResultTy = ResultTy.getNonReferenceType(); |
| 6645 | |
| 6646 | // Build the actual expression node. |
| 6647 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 6648 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6649 | UsualUnaryConversions(FnExpr); |
| 6650 | |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6651 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6652 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 6653 | Args, 2, ResultTy, |
| 6654 | OpLoc)); |
| 6655 | |
| 6656 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6657 | FnDecl)) |
| 6658 | return ExprError(); |
| 6659 | |
| 6660 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6661 | } else { |
| 6662 | // We matched a built-in operator. Convert the arguments, then |
| 6663 | // break out so that we will build the appropriate built-in |
| 6664 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6665 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6666 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6667 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6668 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6669 | return ExprError(); |
| 6670 | |
| 6671 | break; |
| 6672 | } |
| 6673 | } |
| 6674 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6675 | case OR_No_Viable_Function: { |
| 6676 | // C++ [over.match.oper]p9: |
| 6677 | // If the operator is the operator , [...] and there are no |
| 6678 | // viable functions, then the operator is assumed to be the |
| 6679 | // built-in operator and interpreted according to clause 5. |
| 6680 | if (Opc == BinaryOperator::Comma) |
| 6681 | break; |
| 6682 | |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6683 | // For class as left operand for assignment or compound assigment operator |
| 6684 | // do not fall through to handling in built-in, but report that no overloaded |
| 6685 | // assignment operator found |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6686 | OwningExprResult Result = ExprError(); |
| 6687 | if (Args[0]->getType()->isRecordType() && |
| 6688 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6689 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6690 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6691 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6692 | } else { |
| 6693 | // No viable function; try to create a built-in operation, which will |
| 6694 | // produce an error. Then, show the non-viable candidates. |
| 6695 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6696 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6697 | assert(Result.isInvalid() && |
| 6698 | "C++ binary operator overloading is missing candidates!"); |
| 6699 | if (Result.isInvalid()) |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6700 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6701 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6702 | return move(Result); |
| 6703 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6704 | |
| 6705 | case OR_Ambiguous: |
| 6706 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6707 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6708 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6709 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6710 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6711 | return ExprError(); |
| 6712 | |
| 6713 | case OR_Deleted: |
| 6714 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6715 | << Best->Function->isDeleted() |
| 6716 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6717 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6718 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6719 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6720 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6721 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6722 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6723 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6726 | Action::OwningExprResult |
| 6727 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 6728 | SourceLocation RLoc, |
| 6729 | ExprArg Base, ExprArg Idx) { |
| 6730 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 6731 | static_cast<Expr*>(Idx.get()) }; |
| 6732 | DeclarationName OpName = |
| 6733 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 6734 | |
| 6735 | // If either side is type-dependent, create an appropriate dependent |
| 6736 | // expression. |
| 6737 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 6738 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6739 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6740 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6741 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6742 | 0, SourceRange(), OpName, LLoc, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 6743 | /*ADL*/ true, /*Overloaded*/ false, |
| 6744 | UnresolvedSetIterator(), |
| 6745 | UnresolvedSetIterator()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6746 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6747 | |
| 6748 | Base.release(); |
| 6749 | Idx.release(); |
| 6750 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 6751 | Args, 2, |
| 6752 | Context.DependentTy, |
| 6753 | RLoc)); |
| 6754 | } |
| 6755 | |
| 6756 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6757 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6758 | |
| 6759 | // Subscript can only be overloaded as a member function. |
| 6760 | |
| 6761 | // Add operator candidates that are member functions. |
| 6762 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6763 | |
| 6764 | // Add builtin operator candidates. |
| 6765 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6766 | |
| 6767 | // Perform overload resolution. |
| 6768 | OverloadCandidateSet::iterator Best; |
| 6769 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 6770 | case OR_Success: { |
| 6771 | // We found a built-in operator or an overloaded operator. |
| 6772 | FunctionDecl *FnDecl = Best->Function; |
| 6773 | |
| 6774 | if (FnDecl) { |
| 6775 | // We matched an overloaded operator. Build a call to that |
| 6776 | // operator. |
| 6777 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6778 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6779 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6780 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6781 | // Convert the arguments. |
| 6782 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6783 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6784 | Best->FoundDecl, Method)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6785 | return ExprError(); |
| 6786 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 6787 | // Convert the arguments. |
| 6788 | OwningExprResult InputInit |
| 6789 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6790 | FnDecl->getParamDecl(0)), |
| 6791 | SourceLocation(), |
| 6792 | Owned(Args[1])); |
| 6793 | if (InputInit.isInvalid()) |
| 6794 | return ExprError(); |
| 6795 | |
| 6796 | Args[1] = InputInit.takeAs<Expr>(); |
| 6797 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6798 | // Determine the result type |
| 6799 | QualType ResultTy |
| 6800 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 6801 | ResultTy = ResultTy.getNonReferenceType(); |
| 6802 | |
| 6803 | // Build the actual expression node. |
| 6804 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6805 | LLoc); |
| 6806 | UsualUnaryConversions(FnExpr); |
| 6807 | |
| 6808 | Base.release(); |
| 6809 | Idx.release(); |
| 6810 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6811 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 6812 | FnExpr, Args, 2, |
| 6813 | ResultTy, RLoc)); |
| 6814 | |
| 6815 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 6816 | FnDecl)) |
| 6817 | return ExprError(); |
| 6818 | |
| 6819 | return MaybeBindToTemporary(TheCall.release()); |
| 6820 | } else { |
| 6821 | // We matched a built-in operator. Convert the arguments, then |
| 6822 | // break out so that we will build the appropriate built-in |
| 6823 | // operator node. |
| 6824 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6825 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6826 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6827 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6828 | return ExprError(); |
| 6829 | |
| 6830 | break; |
| 6831 | } |
| 6832 | } |
| 6833 | |
| 6834 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6835 | if (CandidateSet.empty()) |
| 6836 | Diag(LLoc, diag::err_ovl_no_oper) |
| 6837 | << Args[0]->getType() << /*subscript*/ 0 |
| 6838 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 6839 | else |
| 6840 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 6841 | << Args[0]->getType() |
| 6842 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6843 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6844 | "[]", LLoc); |
| 6845 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
| 6848 | case OR_Ambiguous: |
| 6849 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 6850 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6851 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6852 | "[]", LLoc); |
| 6853 | return ExprError(); |
| 6854 | |
| 6855 | case OR_Deleted: |
| 6856 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 6857 | << Best->Function->isDeleted() << "[]" |
| 6858 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6859 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6860 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6861 | return ExprError(); |
| 6862 | } |
| 6863 | |
| 6864 | // We matched a built-in operator; build it. |
| 6865 | Base.release(); |
| 6866 | Idx.release(); |
| 6867 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 6868 | Owned(Args[1]), RLoc); |
| 6869 | } |
| 6870 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6871 | /// BuildCallToMemberFunction - Build a call to a member |
| 6872 | /// function. MemExpr is the expression that refers to the member |
| 6873 | /// function (and includes the object parameter), Args/NumArgs are the |
| 6874 | /// arguments to the function call (not including the object |
| 6875 | /// parameter). The caller needs to validate that the member |
| 6876 | /// expression refers to a member function or an overloaded member |
| 6877 | /// function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6878 | Sema::OwningExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6879 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 6880 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6881 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 6882 | SourceLocation RParenLoc) { |
| 6883 | // Dig out the member expression. This holds both the object |
| 6884 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6885 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6886 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6887 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6888 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 6889 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6890 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6891 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6892 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6893 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6894 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6895 | Qualifier = MemExpr->getQualifier(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6896 | } else { |
| 6897 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6898 | Qualifier = UnresExpr->getQualifier(); |
| 6899 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6900 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6901 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6902 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6903 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6904 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6905 | // FIXME: avoid copy. |
| 6906 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6907 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6908 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6909 | TemplateArgs = &TemplateArgsBuffer; |
| 6910 | } |
| 6911 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6912 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6913 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6914 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6915 | NamedDecl *Func = *I; |
| 6916 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6917 | if (isa<UsingShadowDecl>(Func)) |
| 6918 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6919 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6920 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6921 | // If explicit template arguments were provided, we can't call a |
| 6922 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6923 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6924 | continue; |
| 6925 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6926 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6927 | Args, NumArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6928 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6929 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6930 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6931 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6932 | ObjectType, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6933 | CandidateSet, |
| 6934 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6935 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6936 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6937 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6938 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6939 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6940 | OverloadCandidateSet::iterator Best; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6941 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6942 | case OR_Success: |
| 6943 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6944 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6945 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6946 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6947 | break; |
| 6948 | |
| 6949 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6950 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6951 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6952 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6953 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6954 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6955 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6956 | |
| 6957 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6958 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6959 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6960 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6961 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6962 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6963 | |
| 6964 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6965 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6966 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6967 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6968 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6969 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6970 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6973 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6974 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6975 | // If overload resolution picked a static member, build a |
| 6976 | // non-member call based on that function. |
| 6977 | if (Method->isStatic()) { |
| 6978 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6979 | Args, NumArgs, RParenLoc); |
| 6980 | } |
| 6981 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6982 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6986 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6987 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6988 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6989 | Method->getResultType().getNonReferenceType(), |
| 6990 | RParenLoc)); |
| 6991 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6992 | // Check for a valid return type. |
| 6993 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6994 | TheCall.get(), Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6995 | return ExprError(); |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6996 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6997 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6998 | // We only need to do this if there was actually an overload; otherwise |
| 6999 | // it was done at lookup. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7000 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7001 | if (!Method->isStatic() && |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7002 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 7003 | FoundDecl, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7004 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7005 | MemExpr->setBase(ObjectArg); |
| 7006 | |
| 7007 | // Convert the rest of the arguments |
Douglas Gregor | c8be952 | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 7008 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7009 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7010 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7011 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7012 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7013 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7014 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 7015 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7016 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 7017 | } |
| 7018 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7019 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 7020 | /// type (C++ [over.call.object]), which can end up invoking an |
| 7021 | /// overloaded function call operator (@c operator()) or performing a |
| 7022 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7023 | Sema::ExprResult |
| 7024 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 7025 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7026 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7027 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7028 | SourceLocation RParenLoc) { |
| 7029 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7030 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7031 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7032 | // C++ [over.call.object]p1: |
| 7033 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7034 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7035 | // candidate functions includes at least the function call |
| 7036 | // operators of T. The function call operators of T are obtained by |
| 7037 | // ordinary lookup of the name operator() in the context of |
| 7038 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7039 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 7040 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7041 | |
| 7042 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7043 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7044 | << Object->getSourceRange())) |
| 7045 | return true; |
| 7046 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7047 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 7048 | LookupQualifiedName(R, Record->getDecl()); |
| 7049 | R.suppressDiagnostics(); |
| 7050 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 7051 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7052 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7053 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 7054 | Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 7055 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 7056 | } |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7057 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7058 | // C++ [over.call.object]p2: |
| 7059 | // In addition, for each conversion function declared in T of the |
| 7060 | // form |
| 7061 | // |
| 7062 | // operator conversion-type-id () cv-qualifier; |
| 7063 | // |
| 7064 | // where cv-qualifier is the same cv-qualification as, or a |
| 7065 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 7066 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 7067 | // R", or the type "reference to pointer to function of |
| 7068 | // (P1,...,Pn) returning R", or the type "reference to function |
| 7069 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7070 | // is also considered as a candidate function. Similarly, |
| 7071 | // surrogate call functions are added to the set of candidate |
| 7072 | // functions for each conversion function declared in an |
| 7073 | // accessible base class provided the function is not hidden |
| 7074 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7075 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 7076 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7077 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7078 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7079 | NamedDecl *D = *I; |
| 7080 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 7081 | if (isa<UsingShadowDecl>(D)) |
| 7082 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 7083 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7084 | // Skip over templated conversion functions; they aren't |
| 7085 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7086 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7087 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7088 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7089 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7090 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7091 | // Strip the reference type (if any) and then the pointer type (if |
| 7092 | // any) to get down to what might be a function type. |
| 7093 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 7094 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7095 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7096 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 7097 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7098 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7099 | Object->getType(), Args, NumArgs, |
| 7100 | CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7101 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7102 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7103 | // Perform overload resolution. |
| 7104 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7105 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7106 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7107 | // Overload resolution succeeded; we'll build the appropriate call |
| 7108 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7109 | break; |
| 7110 | |
| 7111 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 7112 | if (CandidateSet.empty()) |
| 7113 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 7114 | << Object->getType() << /*call*/ 1 |
| 7115 | << Object->getSourceRange(); |
| 7116 | else |
| 7117 | Diag(Object->getSourceRange().getBegin(), |
| 7118 | diag::err_ovl_no_viable_object_call) |
| 7119 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7120 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7121 | break; |
| 7122 | |
| 7123 | case OR_Ambiguous: |
| 7124 | Diag(Object->getSourceRange().getBegin(), |
| 7125 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7126 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7127 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7128 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7129 | |
| 7130 | case OR_Deleted: |
| 7131 | Diag(Object->getSourceRange().getBegin(), |
| 7132 | diag::err_ovl_deleted_object_call) |
| 7133 | << Best->Function->isDeleted() |
| 7134 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7135 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7136 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7137 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7138 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7139 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7140 | // We had an error; delete all of the subexpressions and return |
| 7141 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7142 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7143 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7144 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7145 | return true; |
| 7146 | } |
| 7147 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7148 | if (Best->Function == 0) { |
| 7149 | // Since there is no function declaration, this is one of the |
| 7150 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7151 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7152 | = cast<CXXConversionDecl>( |
| 7153 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 7154 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7155 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7156 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7157 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7158 | // We selected one of the surrogate functions that converts the |
| 7159 | // object parameter to a function pointer. Perform the conversion |
| 7160 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7161 | |
| 7162 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7163 | // and then call it. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7164 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 7165 | Conv); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 7166 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 7167 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7168 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
Douglas Gregor | e5e775b | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7169 | CommaLocs, RParenLoc).result(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7170 | } |
| 7171 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7172 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7173 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 7174 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 7175 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 7176 | // that calls this method, using Object for the implicit object |
| 7177 | // parameter and passing along the remaining arguments. |
| 7178 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7179 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7180 | |
| 7181 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7182 | unsigned NumArgsToCheck = NumArgs; |
| 7183 | |
| 7184 | // Build the full argument list for the method call (the |
| 7185 | // implicit object parameter is placed at the beginning of the |
| 7186 | // list). |
| 7187 | Expr **MethodArgs; |
| 7188 | if (NumArgs < NumArgsInProto) { |
| 7189 | NumArgsToCheck = NumArgsInProto; |
| 7190 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 7191 | } else { |
| 7192 | MethodArgs = new Expr*[NumArgs + 1]; |
| 7193 | } |
| 7194 | MethodArgs[0] = Object; |
| 7195 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 7196 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7197 | |
| 7198 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7199 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7200 | UsualUnaryConversions(NewFn); |
| 7201 | |
| 7202 | // Once we've built TheCall, all of the expressions are properly |
| 7203 | // owned. |
| 7204 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7205 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7206 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 7207 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7208 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7209 | delete [] MethodArgs; |
| 7210 | |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 7211 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 7212 | Method)) |
| 7213 | return true; |
| 7214 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7215 | // We may have default arguments. If so, we need to allocate more |
| 7216 | // slots in the call for them. |
| 7217 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7218 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7219 | else if (NumArgs > NumArgsInProto) |
| 7220 | NumArgsToCheck = NumArgsInProto; |
| 7221 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7222 | bool IsError = false; |
| 7223 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7224 | // Initialize the implicit object parameter. |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7225 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7226 | Best->FoundDecl, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7227 | TheCall->setArg(0, Object); |
| 7228 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7229 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7230 | // Check the argument types. |
| 7231 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7232 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7233 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7234 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7235 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7236 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7237 | |
| 7238 | OwningExprResult InputInit |
| 7239 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7240 | Method->getParamDecl(i)), |
| 7241 | SourceLocation(), Owned(Arg)); |
| 7242 | |
| 7243 | IsError |= InputInit.isInvalid(); |
| 7244 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7245 | } else { |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 7246 | OwningExprResult DefArg |
| 7247 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 7248 | if (DefArg.isInvalid()) { |
| 7249 | IsError = true; |
| 7250 | break; |
| 7251 | } |
| 7252 | |
| 7253 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7254 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7255 | |
| 7256 | TheCall->setArg(i + 1, Arg); |
| 7257 | } |
| 7258 | |
| 7259 | // If this is a variadic call, handle args passed through "...". |
| 7260 | if (Proto->isVariadic()) { |
| 7261 | // Promote the arguments (C99 6.5.2.2p7). |
| 7262 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 7263 | Expr *Arg = Args[i]; |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 7264 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod, 0); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7265 | TheCall->setArg(i + 1, Arg); |
| 7266 | } |
| 7267 | } |
| 7268 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7269 | if (IsError) return true; |
| 7270 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7271 | if (CheckFunctionCall(Method, TheCall.get())) |
| 7272 | return true; |
| 7273 | |
Douglas Gregor | e5e775b | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7274 | return MaybeBindToTemporary(TheCall.release()).result(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7275 | } |
| 7276 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7277 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7278 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7279 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7280 | Sema::OwningExprResult |
| 7281 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 7282 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7283 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7284 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7285 | SourceLocation Loc = Base->getExprLoc(); |
| 7286 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7287 | // C++ [over.ref]p1: |
| 7288 | // |
| 7289 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 7290 | // for a class object x of type T if T::operator->() exists and if |
| 7291 | // the operator is selected as the best match function by the |
| 7292 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7293 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7294 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7295 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7296 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7297 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 7298 | PDiag(diag::err_typecheck_incomplete_tag) |
| 7299 | << Base->getSourceRange())) |
| 7300 | return ExprError(); |
| 7301 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7302 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7303 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 7304 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7305 | |
| 7306 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7307 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7308 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7309 | /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7310 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7311 | |
| 7312 | // Perform overload resolution. |
| 7313 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7314 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7315 | case OR_Success: |
| 7316 | // Overload resolution succeeded; we'll build the call below. |
| 7317 | break; |
| 7318 | |
| 7319 | case OR_No_Viable_Function: |
| 7320 | if (CandidateSet.empty()) |
| 7321 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7322 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7323 | else |
| 7324 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7325 | << "operator->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7326 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7327 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7328 | |
| 7329 | case OR_Ambiguous: |
| 7330 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7331 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7332 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7333 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7334 | |
| 7335 | case OR_Deleted: |
| 7336 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7337 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7338 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7339 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7340 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7343 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7344 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7345 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7346 | // Convert the object parameter. |
| 7347 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7348 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 7349 | Best->FoundDecl, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7350 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 7351 | |
| 7352 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7353 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7354 | |
| 7355 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7356 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 7357 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7358 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7359 | |
| 7360 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 7361 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7362 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 7363 | &Base, 1, ResultTy, OpLoc)); |
| 7364 | |
| 7365 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 7366 | Method)) |
| 7367 | return ExprError(); |
| 7368 | return move(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7369 | } |
| 7370 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7371 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 7372 | /// a C++ overloaded function (possibly with some parentheses and |
| 7373 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 7374 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7375 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7376 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7377 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7378 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7379 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 7380 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7381 | if (SubExpr == PE->getSubExpr()) |
| 7382 | return PE->Retain(); |
| 7383 | |
| 7384 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 7385 | } |
| 7386 | |
| 7387 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7388 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 7389 | Found, Fn); |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7390 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7391 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7392 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7393 | if (SubExpr == ICE->getSubExpr()) |
| 7394 | return ICE->Retain(); |
| 7395 | |
| 7396 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 7397 | ICE->getCastKind(), |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 7398 | SubExpr, CXXBaseSpecifierArray(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7399 | ICE->isLvalueCast()); |
| 7400 | } |
| 7401 | |
| 7402 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7403 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7404 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7405 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7406 | if (Method->isStatic()) { |
| 7407 | // Do nothing: static member functions aren't any different |
| 7408 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7409 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7410 | // Fix the sub expression, which really has to be an |
| 7411 | // UnresolvedLookupExpr holding an overloaded member function |
| 7412 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7413 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7414 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7415 | if (SubExpr == UnOp->getSubExpr()) |
| 7416 | return UnOp->Retain(); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7417 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7418 | assert(isa<DeclRefExpr>(SubExpr) |
| 7419 | && "fixed to something other than a decl ref"); |
| 7420 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 7421 | && "fixed to a member ref with no nested name qualifier"); |
| 7422 | |
| 7423 | // We have taken the address of a pointer to member |
| 7424 | // function. Perform the computation here so that we get the |
| 7425 | // appropriate pointer to member type. |
| 7426 | QualType ClassType |
| 7427 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 7428 | QualType MemPtrType |
| 7429 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 7430 | |
| 7431 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7432 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7433 | } |
| 7434 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7435 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7436 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7437 | if (SubExpr == UnOp->getSubExpr()) |
| 7438 | return UnOp->Retain(); |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7439 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7440 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7441 | Context.getPointerType(SubExpr->getType()), |
| 7442 | UnOp->getOperatorLoc()); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7443 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7444 | |
| 7445 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7446 | // FIXME: avoid copy. |
| 7447 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7448 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7449 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7450 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7453 | return DeclRefExpr::Create(Context, |
| 7454 | ULE->getQualifier(), |
| 7455 | ULE->getQualifierRange(), |
| 7456 | Fn, |
| 7457 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7458 | Fn->getType(), |
| 7459 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7462 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7463 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7464 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7465 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 7466 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7467 | TemplateArgs = &TemplateArgsBuffer; |
| 7468 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7469 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7470 | Expr *Base; |
| 7471 | |
| 7472 | // If we're filling in |
| 7473 | if (MemExpr->isImplicitAccess()) { |
| 7474 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 7475 | return DeclRefExpr::Create(Context, |
| 7476 | MemExpr->getQualifier(), |
| 7477 | MemExpr->getQualifierRange(), |
| 7478 | Fn, |
| 7479 | MemExpr->getMemberLoc(), |
| 7480 | Fn->getType(), |
| 7481 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 7482 | } else { |
| 7483 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 7484 | if (MemExpr->getQualifier()) |
| 7485 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 7486 | Base = new (Context) CXXThisExpr(Loc, |
| 7487 | MemExpr->getBaseType(), |
| 7488 | /*isImplicit=*/true); |
| 7489 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7490 | } else |
| 7491 | Base = MemExpr->getBase()->Retain(); |
| 7492 | |
| 7493 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7494 | MemExpr->isArrow(), |
| 7495 | MemExpr->getQualifier(), |
| 7496 | MemExpr->getQualifierRange(), |
| 7497 | Fn, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7498 | Found, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7499 | MemExpr->getMemberLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7500 | TemplateArgs, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7501 | Fn->getType()); |
| 7502 | } |
| 7503 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7504 | assert(false && "Invalid reference to overloaded function"); |
| 7505 | return E->Retain(); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7508 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7509 | DeclAccessPair Found, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7510 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7511 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7512 | } |
| 7513 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7514 | } // end namespace clang |