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 | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 55 | ICC_Conversion |
| 56 | }; |
| 57 | return Category[(int)Kind]; |
| 58 | } |
| 59 | |
| 60 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 61 | /// corresponding to the given implicit conversion kind. |
| 62 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 63 | static const ImplicitConversionRank |
| 64 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 70 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 73 | ICR_Promotion, |
| 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 81 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 82 | ICR_Conversion, |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 83 | ICR_Complex_Real_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 84 | }; |
| 85 | return Rank[(int)Kind]; |
| 86 | } |
| 87 | |
| 88 | /// GetImplicitConversionName - Return the name of this kind of |
| 89 | /// implicit conversion. |
| 90 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 91 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | "No conversion", |
| 93 | "Lvalue-to-rvalue", |
| 94 | "Array-to-pointer", |
| 95 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 96 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 97 | "Qualification", |
| 98 | "Integral promotion", |
| 99 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Integral conversion", |
| 102 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 103 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 104 | "Floating-integral conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 105 | "Complex-real conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | "Pointer conversion", |
| 107 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 108 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 109 | "Compatible-types conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 110 | "Derived-to-base conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 111 | }; |
| 112 | return Name[Kind]; |
| 113 | } |
| 114 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 115 | /// StandardConversionSequence - Set the standard conversion |
| 116 | /// sequence to the identity conversion. |
| 117 | void StandardConversionSequence::setAsIdentityConversion() { |
| 118 | First = ICK_Identity; |
| 119 | Second = ICK_Identity; |
| 120 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 121 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 122 | ReferenceBinding = false; |
| 123 | DirectBinding = false; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 124 | RRefBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 125 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 128 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 129 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 130 | /// implicit conversions. |
| 131 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 132 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 133 | if (GetConversionRank(First) > Rank) |
| 134 | Rank = GetConversionRank(First); |
| 135 | if (GetConversionRank(Second) > Rank) |
| 136 | Rank = GetConversionRank(Second); |
| 137 | if (GetConversionRank(Third) > Rank) |
| 138 | Rank = GetConversionRank(Third); |
| 139 | return Rank; |
| 140 | } |
| 141 | |
| 142 | /// isPointerConversionToBool - Determines whether this conversion is |
| 143 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 145 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 151 | if (getToType(1)->isBooleanType() && |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 152 | (getFromType()->isPointerType() || getFromType()->isBlockPointerType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 166 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 167 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 175 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 185 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | bool PrintedSomething = false; |
| 187 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 188 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 189 | PrintedSomething = true; |
| 190 | } |
| 191 | |
| 192 | if (Second != ICK_Identity) { |
| 193 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 194 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 195 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 196 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 197 | |
| 198 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 199 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 200 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 201 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 202 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 203 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 204 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | PrintedSomething = true; |
| 206 | } |
| 207 | |
| 208 | if (Third != ICK_Identity) { |
| 209 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 210 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 212 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 217 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 222 | /// error. Useful for debugging overloading issues. |
| 223 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 224 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 225 | if (Before.First || Before.Second || Before.Third) { |
| 226 | Before.DebugPrint(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 227 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 229 | OS << "'" << ConversionFunction->getNameAsString() << "'"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 230 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 231 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 232 | After.DebugPrint(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 237 | /// error. Useful for debugging overloading issues. |
| 238 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 239 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 240 | switch (ConversionKind) { |
| 241 | case StandardConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 242 | OS << "Standard conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 243 | Standard.DebugPrint(); |
| 244 | break; |
| 245 | case UserDefinedConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 246 | OS << "User-defined conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 247 | UserDefined.DebugPrint(); |
| 248 | break; |
| 249 | case EllipsisConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 250 | OS << "Ellipsis conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 251 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 252 | case AmbiguousConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 253 | OS << "Ambiguous conversion"; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 254 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 255 | case BadConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 256 | OS << "Bad conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 260 | OS << "\n"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 261 | } |
| 262 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 263 | void AmbiguousConversionSequence::construct() { |
| 264 | new (&conversions()) ConversionSet(); |
| 265 | } |
| 266 | |
| 267 | void AmbiguousConversionSequence::destruct() { |
| 268 | conversions().~ConversionSet(); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 273 | FromTypePtr = O.FromTypePtr; |
| 274 | ToTypePtr = O.ToTypePtr; |
| 275 | new (&conversions()) ConversionSet(O.conversions()); |
| 276 | } |
| 277 | |
| 278 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 279 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 280 | // overload of the declarations in Old. This routine returns false if |
| 281 | // New and Old cannot be overloaded, e.g., if New has the same |
| 282 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 283 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 284 | // it does return false, MatchedDecl will point to the decl that New |
| 285 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 286 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 287 | // |
| 288 | // Example: Given the following input: |
| 289 | // |
| 290 | // void f(int, float); // #1 |
| 291 | // void f(int, int); // #2 |
| 292 | // int f(int, int); // #3 |
| 293 | // |
| 294 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 296 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 297 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 298 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 299 | // (since they have different signatures), so this routine returns |
| 300 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 301 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 302 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 303 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 304 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 305 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 306 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 307 | // point to the FunctionDecl for #2. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 308 | Sema::OverloadKind |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 309 | Sema::CheckOverload(FunctionDecl *New, const LookupResult &Old, |
| 310 | NamedDecl *&Match) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 311 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 312 | I != E; ++I) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 313 | NamedDecl *OldD = (*I)->getUnderlyingDecl(); |
| 314 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 315 | if (!IsOverload(New, OldT->getTemplatedDecl())) { |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 316 | Match = *I; |
| 317 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 318 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 319 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 320 | if (!IsOverload(New, OldF)) { |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 321 | Match = *I; |
| 322 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 323 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 324 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 325 | // We can overload with these, which can show up when doing |
| 326 | // redeclaration checks for UsingDecls. |
| 327 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 328 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 329 | // Optimistically assume that an unresolved using decl will |
| 330 | // overload; if it doesn't, we'll have to diagnose during |
| 331 | // template instantiation. |
| 332 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 333 | // (C++ 13p1): |
| 334 | // Only function declarations can be overloaded; object and type |
| 335 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 336 | Match = *I; |
| 337 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 338 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 339 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 340 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 341 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old) { |
| 345 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 346 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 347 | |
| 348 | // C++ [temp.fct]p2: |
| 349 | // A function template can be overloaded with other function templates |
| 350 | // and with normal (non-template) functions. |
| 351 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 352 | return true; |
| 353 | |
| 354 | // Is the function New an overload of the function Old? |
| 355 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 356 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 357 | |
| 358 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 359 | // determine whether they are overloads. If we find any mismatch |
| 360 | // in the signature, they are overloads. |
| 361 | |
| 362 | // If either of these functions is a K&R-style function (no |
| 363 | // prototype), then we consider them to have matching signatures. |
| 364 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 365 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 366 | return false; |
| 367 | |
| 368 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 369 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 370 | |
| 371 | // The signature of a function includes the types of its |
| 372 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 373 | // of the ellipsis; see C++ DR 357). |
| 374 | if (OldQType != NewQType && |
| 375 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 376 | OldType->isVariadic() != NewType->isVariadic() || |
| 377 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 378 | NewType->arg_type_begin()))) |
| 379 | return true; |
| 380 | |
| 381 | // C++ [temp.over.link]p4: |
| 382 | // The signature of a function template consists of its function |
| 383 | // signature, its return type and its template parameter list. The names |
| 384 | // of the template parameters are significant only for establishing the |
| 385 | // relationship between the template parameters and the rest of the |
| 386 | // signature. |
| 387 | // |
| 388 | // We check the return type and template parameter lists for function |
| 389 | // templates first; the remaining checks follow. |
| 390 | if (NewTemplate && |
| 391 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 392 | OldTemplate->getTemplateParameters(), |
| 393 | false, TPL_TemplateMatch) || |
| 394 | OldType->getResultType() != NewType->getResultType())) |
| 395 | return true; |
| 396 | |
| 397 | // If the function is a class member, its signature includes the |
| 398 | // cv-qualifiers (if any) on the function itself. |
| 399 | // |
| 400 | // As part of this, also check whether one of the member functions |
| 401 | // is static, in which case they are not overloads (C++ |
| 402 | // 13.1p2). While not part of the definition of the signature, |
| 403 | // this check is important to determine whether these functions |
| 404 | // can be overloaded. |
| 405 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 406 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 407 | if (OldMethod && NewMethod && |
| 408 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 409 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 410 | return true; |
| 411 | |
| 412 | // The signatures match; this is not an overload. |
| 413 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 416 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 417 | /// from the given expression (Expr) to the given type (ToType). This |
| 418 | /// function returns an implicit conversion sequence that can be used |
| 419 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 420 | /// |
| 421 | /// void f(float f); |
| 422 | /// void g(int i) { f(i); } |
| 423 | /// |
| 424 | /// this routine would produce an implicit conversion sequence to |
| 425 | /// describe the initialization of f from i, which will be a standard |
| 426 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 427 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 428 | // |
| 429 | /// Note that this routine only determines how the conversion can be |
| 430 | /// performed; it does not actually perform the conversion. As such, |
| 431 | /// it will not produce any diagnostics if no conversion is available, |
| 432 | /// but will instead return an implicit conversion sequence of kind |
| 433 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 434 | /// |
| 435 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 436 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 437 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 438 | /// permitted. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 439 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 440 | /// no matter its actual lvalueness. |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 441 | /// If @p UserCast, the implicit conversion is being done for a user-specified |
| 442 | /// cast. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 443 | ImplicitConversionSequence |
Anders Carlsson | 5ec4abf | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 444 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 445 | bool SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 446 | bool AllowExplicit, bool ForceRValue, |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 447 | bool InOverloadResolution, |
| 448 | bool UserCast) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 449 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 450 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 451 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 452 | return ICS; |
| 453 | } |
| 454 | |
| 455 | if (!getLangOptions().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 456 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 457 | return ICS; |
| 458 | } |
| 459 | |
| 460 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 461 | OverloadingResult UserDefResult |
| 462 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
| 463 | !SuppressUserConversions, AllowExplicit, |
Douglas Gregor | 7b23e41 | 2010-04-16 17:25:05 +0000 | [diff] [blame] | 464 | UserCast); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 465 | |
| 466 | if (UserDefResult == OR_Success) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 467 | ICS.setUserDefined(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 468 | // C++ [over.ics.user]p4: |
| 469 | // A conversion of an expression of class type to the same class |
| 470 | // type is given Exact Match rank, and a conversion of an |
| 471 | // expression of class type to a base class of that type is |
| 472 | // given Conversion rank, in spite of the fact that a copy |
| 473 | // constructor (i.e., a user-defined conversion function) is |
| 474 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 476 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | QualType FromCanon |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 478 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 479 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 480 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 4141d5b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 481 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 482 | // Turn this into a "standard" conversion sequence, so that it |
| 483 | // gets ranked with standard conversion sequences. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 484 | ICS.setStandard(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 485 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 486 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 487 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 488 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 489 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 490 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 491 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 492 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 493 | |
| 494 | // C++ [over.best.ics]p4: |
| 495 | // However, when considering the argument of a user-defined |
| 496 | // conversion function that is a candidate by 13.3.1.3 when |
| 497 | // invoked for the copying of the temporary in the second step |
| 498 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 499 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 500 | // ellipsis conversion sequences are allowed. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 501 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 502 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 503 | } |
John McCall | e8c8cd2 | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 504 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 505 | ICS.setAmbiguous(); |
| 506 | ICS.Ambiguous.setFromType(From->getType()); |
| 507 | ICS.Ambiguous.setToType(ToType); |
| 508 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 509 | Cand != Conversions.end(); ++Cand) |
| 510 | if (Cand->Viable) |
| 511 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 512 | } else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 513 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 514 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 515 | |
| 516 | return ICS; |
| 517 | } |
| 518 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 519 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 520 | /// conversion that strips "noreturn" off the nested function type. |
| 521 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 522 | QualType ToType, QualType &ResultTy) { |
| 523 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 524 | return false; |
| 525 | |
| 526 | // Strip the noreturn off the type we're converting from; noreturn can |
| 527 | // safely be removed. |
| 528 | FromType = Context.getNoReturnType(FromType, false); |
| 529 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 530 | return false; |
| 531 | |
| 532 | ResultTy = FromType; |
| 533 | return true; |
| 534 | } |
| 535 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 536 | /// IsStandardConversion - Determines whether there is a standard |
| 537 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 538 | /// expression From to the type ToType. Standard conversion sequences |
| 539 | /// only consider non-class types; for conversions that involve class |
| 540 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 541 | /// contain the standard conversion sequence required to perform this |
| 542 | /// conversion and this routine will return true. Otherwise, this |
| 543 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | bool |
| 545 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 546 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 548 | QualType FromType = From->getType(); |
| 549 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 550 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 551 | SCS.setAsIdentityConversion(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 552 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 553 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 554 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 555 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 557 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 559 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 560 | if (getLangOptions().CPlusPlus) |
| 561 | return false; |
| 562 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 566 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 567 | // array-to-pointer conversion, or function-to-pointer conversion |
| 568 | // (C++ 4p1). |
| 569 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 570 | DeclAccessPair AccessPair; |
| 571 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 573 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 574 | // converted to an rvalue. |
| 575 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 577 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 578 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 579 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 580 | |
| 581 | // If T is a non-class type, the type of the rvalue is the |
| 582 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 583 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 584 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 585 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 586 | } else if (FromType->isArrayType()) { |
| 587 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 588 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 589 | |
| 590 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 591 | // bound of T" can be converted to an rvalue of type "pointer to |
| 592 | // T" (C++ 4.2p1). |
| 593 | FromType = Context.getArrayDecayedType(FromType); |
| 594 | |
| 595 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 596 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 597 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | |
| 599 | // For the purpose of ranking in overload resolution |
| 600 | // (13.3.3.1.1), this conversion is considered an |
| 601 | // array-to-pointer conversion followed by a qualification |
| 602 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 603 | SCS.Second = ICK_Identity; |
| 604 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 605 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 606 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 607 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 608 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 609 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 610 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 611 | |
| 612 | // An lvalue of function type T can be converted to an rvalue of |
| 613 | // type "pointer to T." The result is a pointer to the |
| 614 | // function. (C++ 4.3p1). |
| 615 | FromType = Context.getPointerType(FromType); |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 616 | } else if (From->getType() == Context.OverloadTy) { |
| 617 | if (FunctionDecl *Fn |
| 618 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 619 | AccessPair)) { |
| 620 | // Address of overloaded function (C++ [over.over]). |
| 621 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 622 | |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 623 | // We were able to resolve the address of the overloaded function, |
| 624 | // so we can convert to the type of that function. |
| 625 | FromType = Fn->getType(); |
| 626 | if (ToType->isLValueReferenceType()) |
| 627 | FromType = Context.getLValueReferenceType(FromType); |
| 628 | else if (ToType->isRValueReferenceType()) |
| 629 | FromType = Context.getRValueReferenceType(FromType); |
| 630 | else if (ToType->isMemberPointerType()) { |
| 631 | // Resolve address only succeeds if both sides are member pointers, |
| 632 | // but it doesn't have to be the same class. See DR 247. |
| 633 | // Note that this means that the type of &Derived::fn can be |
| 634 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 635 | // base class, even if it was brought into the derived class via a |
| 636 | // using declaration. The standard isn't clear on this issue at all. |
| 637 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 638 | FromType = Context.getMemberPointerType(FromType, |
| 639 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 640 | } else { |
| 641 | FromType = Context.getPointerType(FromType); |
| 642 | } |
| 643 | } else { |
| 644 | return false; |
| 645 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 646 | } else { |
| 647 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 648 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 649 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 650 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 651 | |
| 652 | // The second conversion can be an integral promotion, floating |
| 653 | // point promotion, integral conversion, floating point conversion, |
| 654 | // floating-integral conversion, pointer conversion, |
| 655 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 656 | // For overloading in C, this can also be a "compatible-type" |
| 657 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 658 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 659 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 660 | // The unqualified versions of the types are the same: there's no |
| 661 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 662 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 663 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 665 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 666 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 667 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 668 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 669 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 670 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 671 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 672 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 673 | SCS.Second = ICK_Complex_Promotion; |
| 674 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 675 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 676 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 677 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 678 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 679 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 680 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 681 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 682 | SCS.Second = ICK_Complex_Conversion; |
| 683 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 684 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 685 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 686 | // Complex-real conversions (C99 6.3.1.7) |
| 687 | SCS.Second = ICK_Complex_Real; |
| 688 | FromType = ToType.getUnqualifiedType(); |
| 689 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 690 | // Floating point conversions (C++ 4.8). |
| 691 | SCS.Second = ICK_Floating_Conversion; |
| 692 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 693 | } else if ((FromType->isFloatingType() && |
| 694 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 695 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 697 | ToType->isFloatingType())) { |
| 698 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 699 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 700 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 701 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 702 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 703 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 704 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 705 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 706 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 707 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 708 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 709 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 710 | } else if (ToType->isBooleanType() && |
| 711 | (FromType->isArithmeticType() || |
| 712 | FromType->isEnumeralType() || |
Fariborz Jahanian | 8811885 | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 713 | FromType->isAnyPointerType() || |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 714 | FromType->isBlockPointerType() || |
| 715 | FromType->isMemberPointerType() || |
| 716 | FromType->isNullPtrType())) { |
| 717 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 718 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 721 | Context.typesAreCompatible(ToType, FromType)) { |
| 722 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 723 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 724 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 725 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 726 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 727 | } else { |
| 728 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 729 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 730 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 731 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 733 | QualType CanonFrom; |
| 734 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 735 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 736 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 737 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 738 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 739 | CanonFrom = Context.getCanonicalType(FromType); |
| 740 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 741 | } else { |
| 742 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 743 | SCS.Third = ICK_Identity; |
| 744 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 746 | // [...] Any difference in top-level cv-qualification is |
| 747 | // subsumed by the initialization itself and does not constitute |
| 748 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 749 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 751 | if (CanonFrom.getLocalUnqualifiedType() |
| 752 | == CanonTo.getLocalUnqualifiedType() && |
| 753 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 754 | FromType = ToType; |
| 755 | CanonFrom = CanonTo; |
| 756 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 757 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 758 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 759 | |
| 760 | // If we have not converted the argument type to the parameter type, |
| 761 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 762 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 763 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 764 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 765 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 769 | /// expression From (whose potentially-adjusted type is FromType) to |
| 770 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 771 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 773 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 774 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 775 | if (!To) { |
| 776 | return false; |
| 777 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 778 | |
| 779 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 780 | // unsigned short int can be converted to an rvalue of type int if |
| 781 | // int can represent all the values of the source type; otherwise, |
| 782 | // the source rvalue can be converted to an rvalue of type unsigned |
| 783 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 784 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 785 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 786 | if (// We can promote any signed, promotable integer type to an int |
| 787 | (FromType->isSignedIntegerType() || |
| 788 | // We can promote any unsigned integer type whose size is |
| 789 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 791 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 792 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 795 | return To->getKind() == BuiltinType::UInt; |
| 796 | } |
| 797 | |
| 798 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 799 | // can be converted to an rvalue of the first of the following types |
| 800 | // that can represent all the values of its underlying type: int, |
| 801 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 802 | |
| 803 | // We pre-calculate the promotion type for enum types. |
| 804 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 805 | if (ToType->isIntegerType()) |
| 806 | return Context.hasSameUnqualifiedType(ToType, |
| 807 | FromEnumType->getDecl()->getPromotionType()); |
| 808 | |
| 809 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 810 | // Determine whether the type we're converting from is signed or |
| 811 | // unsigned. |
| 812 | bool FromIsSigned; |
| 813 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 814 | |
| 815 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 816 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 817 | |
| 818 | // The types we'll try to promote to, in the appropriate |
| 819 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | QualType PromoteTypes[6] = { |
| 821 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 822 | Context.LongTy, Context.UnsignedLongTy , |
| 823 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 824 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 825 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 826 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 827 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 829 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 830 | // We found the type that we can promote to. If this is the |
| 831 | // type we wanted, we have a promotion. Otherwise, no |
| 832 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 833 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 839 | // rvalue of type int if int can represent all the values of the |
| 840 | // bit-field; otherwise, it can be converted to unsigned int if |
| 841 | // unsigned int can represent all the values of the bit-field. If |
| 842 | // the bit-field is larger yet, no integral promotion applies to |
| 843 | // it. If the bit-field has an enumerated type, it is treated as any |
| 844 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 845 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 846 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 847 | using llvm::APSInt; |
| 848 | if (From) |
| 849 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 850 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 851 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 852 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 853 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 854 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 856 | // Are we promoting to an int from a bitfield that fits in an int? |
| 857 | if (BitWidth < ToSize || |
| 858 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 859 | return To->getKind() == BuiltinType::Int; |
| 860 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 862 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 863 | // that fits into an unsigned int? |
| 864 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 865 | return To->getKind() == BuiltinType::UInt; |
| 866 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 868 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 869 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 870 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 872 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 873 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 874 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 875 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 876 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 877 | |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 882 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 883 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 885 | /// An rvalue of type float can be converted to an rvalue of type |
| 886 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 887 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 888 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 889 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 890 | ToBuiltin->getKind() == BuiltinType::Double) |
| 891 | return true; |
| 892 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 893 | // C99 6.3.1.5p1: |
| 894 | // When a float is promoted to double or long double, or a |
| 895 | // double is promoted to long double [...]. |
| 896 | if (!getLangOptions().CPlusPlus && |
| 897 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 898 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 899 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 900 | return true; |
| 901 | } |
| 902 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 903 | return false; |
| 904 | } |
| 905 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 906 | /// \brief Determine if a conversion is a complex promotion. |
| 907 | /// |
| 908 | /// A complex promotion is defined as a complex -> complex conversion |
| 909 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 910 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 911 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 912 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 913 | if (!FromComplex) |
| 914 | return false; |
| 915 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 916 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 917 | if (!ToComplex) |
| 918 | return false; |
| 919 | |
| 920 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 921 | ToComplex->getElementType()) || |
| 922 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 923 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 926 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 927 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 928 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 929 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 930 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | static QualType |
| 932 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 933 | QualType ToPointee, QualType ToType, |
| 934 | ASTContext &Context) { |
| 935 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 936 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 937 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
| 939 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 940 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 941 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 942 | if (!ToType.isNull()) |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 943 | return ToType; |
| 944 | |
| 945 | // Build a pointer to ToPointee. It has the right qualifiers |
| 946 | // already. |
| 947 | return Context.getPointerType(ToPointee); |
| 948 | } |
| 949 | |
| 950 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 951 | return Context.getPointerType( |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 952 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 953 | Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 956 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 957 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 958 | /// not have the right set of qualifiers. |
| 959 | static QualType |
| 960 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 961 | QualType ToType, |
| 962 | ASTContext &Context) { |
| 963 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 964 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 965 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 966 | |
| 967 | // Exact qualifier match -> return the pointer type we're converting to. |
| 968 | if (CanonToType.getLocalQualifiers() == Quals) |
| 969 | return ToType; |
| 970 | |
| 971 | // Just build a canonical type that has the right qualifiers. |
| 972 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 973 | } |
| 974 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 976 | bool InOverloadResolution, |
| 977 | ASTContext &Context) { |
| 978 | // Handle value-dependent integral null pointer constants correctly. |
| 979 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 980 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 981 | Expr->getType()->isIntegralType()) |
| 982 | return !InOverloadResolution; |
| 983 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 984 | return Expr->isNullPointerConstant(Context, |
| 985 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 986 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 987 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 989 | /// IsPointerConversion - Determines whether the conversion of the |
| 990 | /// expression From, which has the (possibly adjusted) type FromType, |
| 991 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 992 | /// 4.10). If so, returns true and places the converted type (that |
| 993 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 994 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 995 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 996 | /// This routine also supports conversions to and from block pointers |
| 997 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 998 | /// pointers to interfaces. FIXME: Once we've determined the |
| 999 | /// appropriate overloading rules for Objective-C, we may want to |
| 1000 | /// split the Objective-C checks into a different routine; however, |
| 1001 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1002 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1003 | /// set if the conversion is an allowed Objective-C conversion that |
| 1004 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1005 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1006 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1007 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1009 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1010 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1011 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1012 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1014 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1015 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1016 | ConvertedType = ToType; |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1020 | // Blocks: Block pointers can be converted to void*. |
| 1021 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1022 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1023 | ConvertedType = ToType; |
| 1024 | return true; |
| 1025 | } |
| 1026 | // Blocks: A null pointer constant can be converted to a block |
| 1027 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1029 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1030 | ConvertedType = ToType; |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1034 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1035 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1037 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1038 | ConvertedType = ToType; |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1042 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1043 | if (!ToTypePtr) |
| 1044 | return false; |
| 1045 | |
| 1046 | // 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] | 1047 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1048 | ConvertedType = ToType; |
| 1049 | return true; |
| 1050 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1051 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1052 | // Beyond this point, both types need to be pointers |
| 1053 | // , including objective-c pointers. |
| 1054 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1055 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1056 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1057 | ToType, Context); |
| 1058 | return true; |
| 1059 | |
| 1060 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1061 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1062 | if (!FromTypePtr) |
| 1063 | return false; |
| 1064 | |
| 1065 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1067 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1068 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1069 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1070 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1072 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1073 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1074 | return true; |
| 1075 | } |
| 1076 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1077 | // When we're overloading in C, we allow a special kind of pointer |
| 1078 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1080 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1082 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1084 | return true; |
| 1085 | } |
| 1086 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1087 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1089 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1090 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1091 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1092 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1093 | // necessitates this conversion is ill-formed. The result of the |
| 1094 | // conversion is a pointer to the base class sub-object of the |
| 1095 | // derived class object. The null pointer value is converted to |
| 1096 | // the null pointer value of the destination type. |
| 1097 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1098 | // Note that we do not check for ambiguity or inaccessibility |
| 1099 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1100 | if (getLangOptions().CPlusPlus && |
| 1101 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1102 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1103 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1104 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1106 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1107 | ToType, Context); |
| 1108 | return true; |
| 1109 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1111 | return false; |
| 1112 | } |
| 1113 | |
| 1114 | /// isObjCPointerConversion - Determines whether this is an |
| 1115 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1116 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1118 | QualType& ConvertedType, |
| 1119 | bool &IncompatibleObjC) { |
| 1120 | if (!getLangOptions().ObjC1) |
| 1121 | return false; |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1122 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1123 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1124 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1126 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1127 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1128 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1129 | // 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] | 1130 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1131 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1132 | ConvertedType = ToType; |
| 1133 | return true; |
| 1134 | } |
| 1135 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1137 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1139 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1140 | ConvertedType = ToType; |
| 1141 | return true; |
| 1142 | } |
| 1143 | // Objective C++: We're able to convert from a pointer to an |
| 1144 | // interface to a pointer to a different interface. |
| 1145 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1146 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1147 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1148 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1149 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1150 | FromObjCPtr->getPointeeType())) |
| 1151 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1152 | ConvertedType = ToType; |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
| 1156 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1157 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1158 | // interfaces, which is permitted. However, we're going to |
| 1159 | // complain about it. |
| 1160 | IncompatibleObjC = true; |
| 1161 | ConvertedType = FromType; |
| 1162 | return true; |
| 1163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1165 | // 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] | 1166 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1167 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1168 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1169 | else if (const BlockPointerType *ToBlockPtr = |
| 1170 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1171 | // 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] | 1172 | // to a block pointer type. |
| 1173 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1174 | ConvertedType = ToType; |
| 1175 | return true; |
| 1176 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1177 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1178 | } |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1179 | else if (FromType->getAs<BlockPointerType>() && |
| 1180 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1181 | // 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] | 1182 | // pointer to any object. |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1183 | ConvertedType = ToType; |
| 1184 | return true; |
| 1185 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1186 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1187 | return false; |
| 1188 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1189 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1190 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1191 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1192 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1193 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1194 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1195 | return false; |
| 1196 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1197 | // If we have pointers to pointers, recursively check whether this |
| 1198 | // is an Objective-C conversion. |
| 1199 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1200 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1201 | IncompatibleObjC)) { |
| 1202 | // We always complain about this conversion. |
| 1203 | IncompatibleObjC = true; |
| 1204 | ConvertedType = ToType; |
| 1205 | return true; |
| 1206 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1207 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1208 | // as in I* to id. |
| 1209 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1210 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1211 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1212 | IncompatibleObjC)) { |
| 1213 | ConvertedType = ToType; |
| 1214 | return true; |
| 1215 | } |
| 1216 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1217 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1218 | // differences in the argument and result types are in Objective-C |
| 1219 | // pointer conversions. If so, we permit the conversion (but |
| 1220 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1222 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1223 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1224 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1225 | if (FromFunctionType && ToFunctionType) { |
| 1226 | // If the function types are exactly the same, this isn't an |
| 1227 | // Objective-C pointer conversion. |
| 1228 | if (Context.getCanonicalType(FromPointeeType) |
| 1229 | == Context.getCanonicalType(ToPointeeType)) |
| 1230 | return false; |
| 1231 | |
| 1232 | // Perform the quick checks that will tell us whether these |
| 1233 | // function types are obviously different. |
| 1234 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1235 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1236 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1237 | return false; |
| 1238 | |
| 1239 | bool HasObjCConversion = false; |
| 1240 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1241 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1242 | // Okay, the types match exactly. Nothing to do. |
| 1243 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1244 | ToFunctionType->getResultType(), |
| 1245 | ConvertedType, IncompatibleObjC)) { |
| 1246 | // Okay, we have an Objective-C pointer conversion. |
| 1247 | HasObjCConversion = true; |
| 1248 | } else { |
| 1249 | // Function types are too different. Abort. |
| 1250 | return false; |
| 1251 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1253 | // Check argument types. |
| 1254 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1255 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1256 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1257 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1258 | if (Context.getCanonicalType(FromArgType) |
| 1259 | == Context.getCanonicalType(ToArgType)) { |
| 1260 | // Okay, the types match exactly. Nothing to do. |
| 1261 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1262 | ConvertedType, IncompatibleObjC)) { |
| 1263 | // Okay, we have an Objective-C pointer conversion. |
| 1264 | HasObjCConversion = true; |
| 1265 | } else { |
| 1266 | // Argument types are too different. Abort. |
| 1267 | return false; |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | if (HasObjCConversion) { |
| 1272 | // We had an Objective-C conversion. Allow this pointer |
| 1273 | // conversion, but complain about it. |
| 1274 | ConvertedType = ToType; |
| 1275 | IncompatibleObjC = true; |
| 1276 | return true; |
| 1277 | } |
| 1278 | } |
| 1279 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1280 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1283 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1284 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1285 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1286 | /// conversions for which IsPointerConversion has already returned |
| 1287 | /// true. It returns true and produces a diagnostic if there was an |
| 1288 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1289 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1290 | CastExpr::CastKind &Kind, |
| 1291 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1292 | QualType FromType = From->getType(); |
| 1293 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1294 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1295 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1296 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1297 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1299 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1300 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1301 | // We must have a derived-to-base conversion. Check an |
| 1302 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1303 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1304 | From->getExprLoc(), |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1305 | From->getSourceRange(), |
| 1306 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1307 | return true; |
| 1308 | |
| 1309 | // The conversion was successful. |
| 1310 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1314 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1316 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1317 | // Objective-C++ conversions are always okay. |
| 1318 | // FIXME: We should have a different class of conversions for the |
| 1319 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1320 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1321 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1322 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1323 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1324 | return false; |
| 1325 | } |
| 1326 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1327 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1328 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1329 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1330 | /// If so, returns true and places the converted type (that might differ from |
| 1331 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1332 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1333 | QualType ToType, |
| 1334 | bool InOverloadResolution, |
| 1335 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1336 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1337 | if (!ToTypePtr) |
| 1338 | return false; |
| 1339 | |
| 1340 | // 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] | 1341 | if (From->isNullPointerConstant(Context, |
| 1342 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1343 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1344 | ConvertedType = ToType; |
| 1345 | return true; |
| 1346 | } |
| 1347 | |
| 1348 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1349 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1350 | if (!FromTypePtr) |
| 1351 | return false; |
| 1352 | |
| 1353 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1354 | // where D is derived from B (C++ 4.11p2). |
| 1355 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1356 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1357 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1358 | |
| 1359 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1360 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1361 | ToClass.getTypePtr()); |
| 1362 | return true; |
| 1363 | } |
| 1364 | |
| 1365 | return false; |
| 1366 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1367 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1368 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1369 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1370 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1371 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1372 | /// true and produces a diagnostic if there was an error, or returns false |
| 1373 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1375 | CastExpr::CastKind &Kind, |
| 1376 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1377 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1378 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1379 | if (!FromPtrType) { |
| 1380 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1381 | assert(From->isNullPointerConstant(Context, |
| 1382 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1383 | "Expr must be null pointer constant!"); |
| 1384 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1385 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1386 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1387 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1388 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1389 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1390 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1391 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1392 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1393 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1394 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1395 | // FIXME: What about dependent types? |
| 1396 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1397 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1398 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1399 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/ true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1400 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1401 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1402 | assert(DerivationOkay && |
| 1403 | "Should not have been called if derivation isn't OK."); |
| 1404 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1405 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1406 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1407 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1408 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1409 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1410 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1411 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1412 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1413 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1414 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1415 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1416 | << FromClass << ToClass << QualType(VBase, 0) |
| 1417 | << From->getSourceRange(); |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1421 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1422 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1423 | Paths.front(), |
| 1424 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1425 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1426 | // Must be a base to derived member conversion. |
| 1427 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1428 | return false; |
| 1429 | } |
| 1430 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1431 | /// IsQualificationConversion - Determines whether the conversion from |
| 1432 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1433 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | bool |
| 1435 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1436 | FromType = Context.getCanonicalType(FromType); |
| 1437 | ToType = Context.getCanonicalType(ToType); |
| 1438 | |
| 1439 | // If FromType and ToType are the same type, this is not a |
| 1440 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1441 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1442 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1444 | // (C++ 4.4p4): |
| 1445 | // A conversion can add cv-qualifiers at levels other than the first |
| 1446 | // in multi-level pointers, subject to the following rules: [...] |
| 1447 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1448 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1449 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1450 | // Within each iteration of the loop, we check the qualifiers to |
| 1451 | // determine if this still looks like a qualification |
| 1452 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1453 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1454 | // until there are no more pointers or pointers-to-members left to |
| 1455 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1456 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1457 | |
| 1458 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1459 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1460 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1461 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1463 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1464 | // every cv for 0 < k < j. |
| 1465 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1466 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1467 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1469 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1470 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1472 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1473 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1474 | |
| 1475 | // We are left with FromType and ToType being the pointee types |
| 1476 | // after unwrapping the original FromType and ToType the same number |
| 1477 | // of types. If we unwrapped any pointers, and if FromType and |
| 1478 | // ToType have the same unqualified type (since we checked |
| 1479 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1480 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1483 | /// Determines whether there is a user-defined conversion sequence |
| 1484 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1485 | /// ToType. If such a conversion exists, User will contain the |
| 1486 | /// user-defined conversion sequence that performs such a conversion |
| 1487 | /// and this routine will return true. Otherwise, this routine returns |
| 1488 | /// false and User is unspecified. |
| 1489 | /// |
| 1490 | /// \param AllowConversionFunctions true if the conversion should |
| 1491 | /// consider conversion functions at all. If false, only constructors |
| 1492 | /// will be considered. |
| 1493 | /// |
| 1494 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1495 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1496 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1497 | /// |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1498 | /// \param UserCast true if looking for user defined conversion for a static |
| 1499 | /// cast. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1500 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1501 | UserDefinedConversionSequence& User, |
| 1502 | OverloadCandidateSet& CandidateSet, |
| 1503 | bool AllowConversionFunctions, |
| 1504 | bool AllowExplicit, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1505 | bool UserCast) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1506 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1507 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1508 | // We're not going to find any constructors. |
| 1509 | } else if (CXXRecordDecl *ToRecordDecl |
| 1510 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1511 | // C++ [over.match.ctor]p1: |
| 1512 | // When objects of class type are direct-initialized (8.5), or |
| 1513 | // copy-initialized from an expression of the same or a |
| 1514 | // derived class type (8.5), overload resolution selects the |
| 1515 | // constructor. [...] For copy-initialization, the candidate |
| 1516 | // functions are all the converting constructors (12.3.1) of |
| 1517 | // that class. The argument list is the expression-list within |
| 1518 | // the parentheses of the initializer. |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1519 | bool SuppressUserConversions = !UserCast; |
| 1520 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1521 | IsDerivedFrom(From->getType(), ToType)) { |
| 1522 | SuppressUserConversions = false; |
| 1523 | AllowConversionFunctions = false; |
| 1524 | } |
| 1525 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1527 | = Context.DeclarationNames.getCXXConstructorName( |
| 1528 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1529 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1531 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1532 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1533 | NamedDecl *D = *Con; |
| 1534 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1535 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1536 | // Find the constructor (which may be a template). |
| 1537 | CXXConstructorDecl *Constructor = 0; |
| 1538 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1539 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1540 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1542 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1543 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1544 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1545 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1546 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1547 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1548 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1549 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1550 | /*ExplicitArgs*/ 0, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1551 | &From, 1, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 1552 | SuppressUserConversions); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1553 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1554 | // Allow one user-defined conversion when user specifies a |
| 1555 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1556 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1557 | &From, 1, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 1558 | SuppressUserConversions); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1559 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1560 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1564 | if (!AllowConversionFunctions) { |
| 1565 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1567 | PDiag(0) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1568 | << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1569 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | } else if (const RecordType *FromRecordType |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1571 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1573 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1574 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1575 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1576 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1577 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1578 | E = Conversions->end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1579 | DeclAccessPair FoundDecl = I.getPair(); |
| 1580 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1581 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1582 | if (isa<UsingShadowDecl>(D)) |
| 1583 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1584 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1585 | CXXConversionDecl *Conv; |
| 1586 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1587 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1588 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1589 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1590 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1591 | |
| 1592 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1593 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1594 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1595 | ActingContext, From, ToType, |
| 1596 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1597 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1598 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1599 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1603 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1604 | |
| 1605 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1606 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1607 | case OR_Success: |
| 1608 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1610 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1611 | // C++ [over.ics.user]p1: |
| 1612 | // If the user-defined conversion is specified by a |
| 1613 | // constructor (12.3.1), the initial standard conversion |
| 1614 | // sequence converts the source type to the type required by |
| 1615 | // the argument of the constructor. |
| 1616 | // |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1617 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1618 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1619 | User.EllipsisConversion = true; |
| 1620 | else { |
| 1621 | User.Before = Best->Conversions[0].Standard; |
| 1622 | User.EllipsisConversion = false; |
| 1623 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1624 | User.ConversionFunction = Constructor; |
| 1625 | User.After.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1626 | User.After.setFromType( |
| 1627 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1628 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1629 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1630 | } else if (CXXConversionDecl *Conversion |
| 1631 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1632 | // C++ [over.ics.user]p1: |
| 1633 | // |
| 1634 | // [...] If the user-defined conversion is specified by a |
| 1635 | // conversion function (12.3.2), the initial standard |
| 1636 | // conversion sequence converts the source type to the |
| 1637 | // implicit object parameter of the conversion function. |
| 1638 | User.Before = Best->Conversions[0].Standard; |
| 1639 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1640 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
| 1642 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1643 | // The second standard conversion sequence converts the |
| 1644 | // result of the user-defined conversion to the target type |
| 1645 | // for the sequence. Since an implicit conversion sequence |
| 1646 | // is an initialization, the special rules for |
| 1647 | // initialization by user-defined conversion apply when |
| 1648 | // selecting the best user-defined conversion for a |
| 1649 | // user-defined conversion sequence (see 13.3.3 and |
| 1650 | // 13.3.3.1). |
| 1651 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1652 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1653 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1654 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1655 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1656 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1658 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1659 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1660 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1661 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1662 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1663 | |
| 1664 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1665 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1668 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1669 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1670 | |
| 1671 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1672 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1673 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1674 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1675 | OverloadingResult OvResult = |
| 1676 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 7b23e41 | 2010-04-16 17:25:05 +0000 | [diff] [blame] | 1677 | CandidateSet, true, false, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1678 | if (OvResult == OR_Ambiguous) |
| 1679 | Diag(From->getSourceRange().getBegin(), |
| 1680 | diag::err_typecheck_ambiguous_condition) |
| 1681 | << From->getType() << ToType << From->getSourceRange(); |
| 1682 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1683 | Diag(From->getSourceRange().getBegin(), |
| 1684 | diag::err_typecheck_nonviable_condition) |
| 1685 | << From->getType() << ToType << From->getSourceRange(); |
| 1686 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1687 | return false; |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1688 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1689 | return true; |
| 1690 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1691 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1692 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1693 | /// conversion sequences to determine whether one is better than the |
| 1694 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1696 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1697 | const ImplicitConversionSequence& ICS2) |
| 1698 | { |
| 1699 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1700 | // conversion sequences (as defined in 13.3.3.1) |
| 1701 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1702 | // conversion sequence than a user-defined conversion sequence or |
| 1703 | // an ellipsis conversion sequence, and |
| 1704 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1705 | // conversion sequence than an ellipsis conversion sequence |
| 1706 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1708 | // C++0x [over.best.ics]p10: |
| 1709 | // For the purpose of ranking implicit conversion sequences as |
| 1710 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1711 | // treated as a user-defined sequence that is indistinguishable |
| 1712 | // from any other user-defined conversion sequence. |
| 1713 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1714 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1715 | return ImplicitConversionSequence::Better; |
| 1716 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1717 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1718 | return ImplicitConversionSequence::Worse; |
| 1719 | } |
| 1720 | |
| 1721 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1722 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1723 | |
| 1724 | // Two implicit conversion sequences of the same form are |
| 1725 | // indistinguishable conversion sequences unless one of the |
| 1726 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1727 | if (ICS1.isStandard()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1728 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1729 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1730 | // User-defined conversion sequence U1 is a better conversion |
| 1731 | // sequence than another user-defined conversion sequence U2 if |
| 1732 | // they contain the same user-defined conversion function or |
| 1733 | // constructor and if the second standard conversion sequence of |
| 1734 | // U1 is better than the second standard conversion sequence of |
| 1735 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1737 | ICS2.UserDefined.ConversionFunction) |
| 1738 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1739 | ICS2.UserDefined.After); |
| 1740 | } |
| 1741 | |
| 1742 | return ImplicitConversionSequence::Indistinguishable; |
| 1743 | } |
| 1744 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1745 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 1746 | // determine if one is a proper subset of the other. |
| 1747 | static ImplicitConversionSequence::CompareKind |
| 1748 | compareStandardConversionSubsets(ASTContext &Context, |
| 1749 | const StandardConversionSequence& SCS1, |
| 1750 | const StandardConversionSequence& SCS2) { |
| 1751 | ImplicitConversionSequence::CompareKind Result |
| 1752 | = ImplicitConversionSequence::Indistinguishable; |
| 1753 | |
| 1754 | if (SCS1.Second != SCS2.Second) { |
| 1755 | if (SCS1.Second == ICK_Identity) |
| 1756 | Result = ImplicitConversionSequence::Better; |
| 1757 | else if (SCS2.Second == ICK_Identity) |
| 1758 | Result = ImplicitConversionSequence::Worse; |
| 1759 | else |
| 1760 | return ImplicitConversionSequence::Indistinguishable; |
| 1761 | } else if (!Context.hasSameType(SCS1.getToType(1), SCS2.getToType(1))) |
| 1762 | return ImplicitConversionSequence::Indistinguishable; |
| 1763 | |
| 1764 | if (SCS1.Third == SCS2.Third) { |
| 1765 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 1766 | : ImplicitConversionSequence::Indistinguishable; |
| 1767 | } |
| 1768 | |
| 1769 | if (SCS1.Third == ICK_Identity) |
| 1770 | return Result == ImplicitConversionSequence::Worse |
| 1771 | ? ImplicitConversionSequence::Indistinguishable |
| 1772 | : ImplicitConversionSequence::Better; |
| 1773 | |
| 1774 | if (SCS2.Third == ICK_Identity) |
| 1775 | return Result == ImplicitConversionSequence::Better |
| 1776 | ? ImplicitConversionSequence::Indistinguishable |
| 1777 | : ImplicitConversionSequence::Worse; |
| 1778 | |
| 1779 | return ImplicitConversionSequence::Indistinguishable; |
| 1780 | } |
| 1781 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1782 | /// CompareStandardConversionSequences - Compare two standard |
| 1783 | /// conversion sequences to determine whether one is better than the |
| 1784 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1786 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1787 | const StandardConversionSequence& SCS2) |
| 1788 | { |
| 1789 | // Standard conversion sequence S1 is a better conversion sequence |
| 1790 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1791 | |
| 1792 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1793 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1794 | // excluding any Lvalue Transformation; the identity conversion |
| 1795 | // sequence is considered to be a subsequence of any |
| 1796 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1797 | if (ImplicitConversionSequence::CompareKind CK |
| 1798 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 1799 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1800 | |
| 1801 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1802 | // defined below), or, if not that, |
| 1803 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1804 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1805 | if (Rank1 < Rank2) |
| 1806 | return ImplicitConversionSequence::Better; |
| 1807 | else if (Rank2 < Rank1) |
| 1808 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1810 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1811 | // are indistinguishable unless one of the following rules |
| 1812 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1814 | // A conversion that is not a conversion of a pointer, or |
| 1815 | // pointer to member, to bool is better than another conversion |
| 1816 | // that is such a conversion. |
| 1817 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1818 | return SCS2.isPointerConversionToBool() |
| 1819 | ? ImplicitConversionSequence::Better |
| 1820 | : ImplicitConversionSequence::Worse; |
| 1821 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1822 | // C++ [over.ics.rank]p4b2: |
| 1823 | // |
| 1824 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1825 | // conversion of B* to A* is better than conversion of B* to |
| 1826 | // void*, and conversion of A* to void* is better than conversion |
| 1827 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1829 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1831 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1832 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1833 | // Exactly one of the conversion sequences is a conversion to |
| 1834 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1835 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1836 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1837 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1838 | // Neither conversion sequence converts to a void pointer; compare |
| 1839 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1840 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1841 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1842 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1843 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1844 | // Both conversion sequences are conversions to void |
| 1845 | // pointers. Compare the source types to determine if there's an |
| 1846 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1847 | QualType FromType1 = SCS1.getFromType(); |
| 1848 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1849 | |
| 1850 | // Adjust the types we're converting from via the array-to-pointer |
| 1851 | // conversion, if we need to. |
| 1852 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1853 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1854 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1855 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1856 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1857 | QualType FromPointee1 |
| 1858 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1859 | QualType FromPointee2 |
| 1860 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1861 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1862 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1863 | return ImplicitConversionSequence::Better; |
| 1864 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1865 | return ImplicitConversionSequence::Worse; |
| 1866 | |
| 1867 | // Objective-C++: If one interface is more specific than the |
| 1868 | // other, it is the better one. |
| 1869 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1870 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1871 | if (FromIface1 && FromIface1) { |
| 1872 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1873 | return ImplicitConversionSequence::Better; |
| 1874 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1875 | return ImplicitConversionSequence::Worse; |
| 1876 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1877 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1878 | |
| 1879 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1880 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1882 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1883 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1884 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1885 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1886 | // C++0x [over.ics.rank]p3b4: |
| 1887 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1888 | // implicit object parameter of a non-static member function declared |
| 1889 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1890 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1891 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1892 | // or if the member function in this case has a ref qualifier. |
| 1893 | // (Of course, we don't have ref qualifiers yet.) |
| 1894 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1895 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1896 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1897 | |
| 1898 | // C++ [over.ics.rank]p3b4: |
| 1899 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1900 | // which the references refer are the same type except for |
| 1901 | // top-level cv-qualifiers, and the type to which the reference |
| 1902 | // initialized by S2 refers is more cv-qualified than the type |
| 1903 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1904 | QualType T1 = SCS1.getToType(2); |
| 1905 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1906 | T1 = Context.getCanonicalType(T1); |
| 1907 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1908 | Qualifiers T1Quals, T2Quals; |
| 1909 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1910 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1911 | if (UnqualT1 == UnqualT2) { |
| 1912 | // If the type is an array type, promote the element qualifiers to the type |
| 1913 | // for comparison. |
| 1914 | if (isa<ArrayType>(T1) && T1Quals) |
| 1915 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1916 | if (isa<ArrayType>(T2) && T2Quals) |
| 1917 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1918 | if (T2.isMoreQualifiedThan(T1)) |
| 1919 | return ImplicitConversionSequence::Better; |
| 1920 | else if (T1.isMoreQualifiedThan(T2)) |
| 1921 | return ImplicitConversionSequence::Worse; |
| 1922 | } |
| 1923 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1924 | |
| 1925 | return ImplicitConversionSequence::Indistinguishable; |
| 1926 | } |
| 1927 | |
| 1928 | /// CompareQualificationConversions - Compares two standard conversion |
| 1929 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1931 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1932 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1934 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1935 | // -- S1 and S2 differ only in their qualification conversion and |
| 1936 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1937 | // cv-qualification signature of type T1 is a proper subset of |
| 1938 | // the cv-qualification signature of type T2, and S1 is not the |
| 1939 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1940 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1941 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1942 | return ImplicitConversionSequence::Indistinguishable; |
| 1943 | |
| 1944 | // FIXME: the example in the standard doesn't use a qualification |
| 1945 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1946 | QualType T1 = SCS1.getToType(2); |
| 1947 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1948 | T1 = Context.getCanonicalType(T1); |
| 1949 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1950 | Qualifiers T1Quals, T2Quals; |
| 1951 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1952 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1953 | |
| 1954 | // If the types are the same, we won't learn anything by unwrapped |
| 1955 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1956 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1957 | return ImplicitConversionSequence::Indistinguishable; |
| 1958 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1959 | // If the type is an array type, promote the element qualifiers to the type |
| 1960 | // for comparison. |
| 1961 | if (isa<ArrayType>(T1) && T1Quals) |
| 1962 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1963 | if (isa<ArrayType>(T2) && T2Quals) |
| 1964 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1965 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1967 | = ImplicitConversionSequence::Indistinguishable; |
| 1968 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1969 | // Within each iteration of the loop, we check the qualifiers to |
| 1970 | // determine if this still looks like a qualification |
| 1971 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1972 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1973 | // until there are no more pointers or pointers-to-members left |
| 1974 | // to unwrap. This essentially mimics what |
| 1975 | // IsQualificationConversion does, but here we're checking for a |
| 1976 | // strict subset of qualifiers. |
| 1977 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1978 | // The qualifiers are the same, so this doesn't tell us anything |
| 1979 | // about how the sequences rank. |
| 1980 | ; |
| 1981 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1982 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1983 | if (Result == ImplicitConversionSequence::Worse) |
| 1984 | // Neither has qualifiers that are a subset of the other's |
| 1985 | // qualifiers. |
| 1986 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1988 | Result = ImplicitConversionSequence::Better; |
| 1989 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1990 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1991 | if (Result == ImplicitConversionSequence::Better) |
| 1992 | // Neither has qualifiers that are a subset of the other's |
| 1993 | // qualifiers. |
| 1994 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1996 | Result = ImplicitConversionSequence::Worse; |
| 1997 | } else { |
| 1998 | // Qualifiers are disjoint. |
| 1999 | return ImplicitConversionSequence::Indistinguishable; |
| 2000 | } |
| 2001 | |
| 2002 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2003 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2004 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2007 | // Check that the winning standard conversion sequence isn't using |
| 2008 | // the deprecated string literal array to pointer conversion. |
| 2009 | switch (Result) { |
| 2010 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2011 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2012 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2013 | break; |
| 2014 | |
| 2015 | case ImplicitConversionSequence::Indistinguishable: |
| 2016 | break; |
| 2017 | |
| 2018 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2019 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2020 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2027 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2028 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2029 | /// various kinds of derived-to-base conversions (C++ |
| 2030 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2031 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2032 | ImplicitConversionSequence::CompareKind |
| 2033 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2034 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2035 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2036 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2037 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2038 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Adjust the types we're converting from via the array-to-pointer |
| 2041 | // conversion, if we need to. |
| 2042 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2043 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2044 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2045 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2046 | |
| 2047 | // Canonicalize all of the types. |
| 2048 | FromType1 = Context.getCanonicalType(FromType1); |
| 2049 | ToType1 = Context.getCanonicalType(ToType1); |
| 2050 | FromType2 = Context.getCanonicalType(FromType2); |
| 2051 | ToType2 = Context.getCanonicalType(ToType2); |
| 2052 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2053 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2054 | // |
| 2055 | // If class B is derived directly or indirectly from class A and |
| 2056 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2057 | // |
| 2058 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2059 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2060 | |
| 2061 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2063 | SCS2.Second == ICK_Pointer_Conversion && |
| 2064 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2065 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2066 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2068 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2070 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2071 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2072 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2073 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2074 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2075 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2076 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2077 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2078 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2079 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2080 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2081 | // -- 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] | 2082 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2083 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2084 | return ImplicitConversionSequence::Better; |
| 2085 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2086 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2087 | |
| 2088 | if (ToIface1 && ToIface2) { |
| 2089 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2090 | return ImplicitConversionSequence::Better; |
| 2091 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2092 | return ImplicitConversionSequence::Worse; |
| 2093 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2094 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2095 | |
| 2096 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2097 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2098 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2099 | return ImplicitConversionSequence::Better; |
| 2100 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2101 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2103 | if (FromIface1 && FromIface2) { |
| 2104 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2105 | return ImplicitConversionSequence::Better; |
| 2106 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2107 | return ImplicitConversionSequence::Worse; |
| 2108 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2109 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2112 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2113 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2114 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2115 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2116 | const MemberPointerType * FromMemPointer1 = |
| 2117 | FromType1->getAs<MemberPointerType>(); |
| 2118 | const MemberPointerType * ToMemPointer1 = |
| 2119 | ToType1->getAs<MemberPointerType>(); |
| 2120 | const MemberPointerType * FromMemPointer2 = |
| 2121 | FromType2->getAs<MemberPointerType>(); |
| 2122 | const MemberPointerType * ToMemPointer2 = |
| 2123 | ToType2->getAs<MemberPointerType>(); |
| 2124 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2125 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2126 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2127 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2128 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2129 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2130 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2131 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2132 | // 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] | 2133 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2134 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2135 | return ImplicitConversionSequence::Worse; |
| 2136 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2137 | return ImplicitConversionSequence::Better; |
| 2138 | } |
| 2139 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2140 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2141 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2142 | return ImplicitConversionSequence::Better; |
| 2143 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2144 | return ImplicitConversionSequence::Worse; |
| 2145 | } |
| 2146 | } |
| 2147 | |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2148 | if ((SCS1.ReferenceBinding || SCS1.CopyConstructor) && |
| 2149 | (SCS2.ReferenceBinding || SCS2.CopyConstructor) && |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2150 | SCS1.Second == ICK_Derived_To_Base) { |
| 2151 | // -- 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] | 2152 | // -- binding of an expression of type C to a reference of type |
| 2153 | // B& is better than binding an expression of type C to a |
| 2154 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2155 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2156 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2157 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2158 | return ImplicitConversionSequence::Better; |
| 2159 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2160 | return ImplicitConversionSequence::Worse; |
| 2161 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2163 | // -- 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] | 2164 | // -- binding of an expression of type B to a reference of type |
| 2165 | // A& is better than binding an expression of type C to a |
| 2166 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2167 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2168 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2169 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2170 | return ImplicitConversionSequence::Better; |
| 2171 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2172 | return ImplicitConversionSequence::Worse; |
| 2173 | } |
| 2174 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2175 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2176 | return ImplicitConversionSequence::Indistinguishable; |
| 2177 | } |
| 2178 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2179 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2180 | /// determine whether they are reference-related, |
| 2181 | /// reference-compatible, reference-compatible with added |
| 2182 | /// qualification, or incompatible, for use in C++ initialization by |
| 2183 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2184 | /// type, and the first type (T1) is the pointee type of the reference |
| 2185 | /// type being initialized. |
| 2186 | Sema::ReferenceCompareResult |
| 2187 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2188 | QualType OrigT1, QualType OrigT2, |
| 2189 | bool& DerivedToBase) { |
| 2190 | assert(!OrigT1->isReferenceType() && |
| 2191 | "T1 must be the pointee type of the reference type"); |
| 2192 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2193 | |
| 2194 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2195 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2196 | Qualifiers T1Quals, T2Quals; |
| 2197 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2198 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2199 | |
| 2200 | // C++ [dcl.init.ref]p4: |
| 2201 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2202 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2203 | // T1 is a base class of T2. |
| 2204 | if (UnqualT1 == UnqualT2) |
| 2205 | DerivedToBase = false; |
| 2206 | else if (!RequireCompleteType(Loc, OrigT1, PDiag()) && |
| 2207 | !RequireCompleteType(Loc, OrigT2, PDiag()) && |
| 2208 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2209 | DerivedToBase = true; |
| 2210 | else |
| 2211 | return Ref_Incompatible; |
| 2212 | |
| 2213 | // At this point, we know that T1 and T2 are reference-related (at |
| 2214 | // least). |
| 2215 | |
| 2216 | // If the type is an array type, promote the element qualifiers to the type |
| 2217 | // for comparison. |
| 2218 | if (isa<ArrayType>(T1) && T1Quals) |
| 2219 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2220 | if (isa<ArrayType>(T2) && T2Quals) |
| 2221 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2222 | |
| 2223 | // C++ [dcl.init.ref]p4: |
| 2224 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2225 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2226 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2227 | // overload resolution, cases for which cv1 is greater |
| 2228 | // cv-qualification than cv2 are identified as |
| 2229 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2230 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2231 | return Ref_Compatible; |
| 2232 | else if (T1.isMoreQualifiedThan(T2)) |
| 2233 | return Ref_Compatible_With_Added_Qualification; |
| 2234 | else |
| 2235 | return Ref_Related; |
| 2236 | } |
| 2237 | |
| 2238 | /// \brief Compute an implicit conversion sequence for reference |
| 2239 | /// initialization. |
| 2240 | static ImplicitConversionSequence |
| 2241 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2242 | SourceLocation DeclLoc, |
| 2243 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame^] | 2244 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2245 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2246 | |
| 2247 | // Most paths end in a failed conversion. |
| 2248 | ImplicitConversionSequence ICS; |
| 2249 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2250 | |
| 2251 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2252 | QualType T2 = Init->getType(); |
| 2253 | |
| 2254 | // If the initializer is the address of an overloaded function, try |
| 2255 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2256 | // type of the resulting function. |
| 2257 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2258 | DeclAccessPair Found; |
| 2259 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2260 | false, Found)) |
| 2261 | T2 = Fn->getType(); |
| 2262 | } |
| 2263 | |
| 2264 | // Compute some basic properties of the types and the initializer. |
| 2265 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2266 | bool DerivedToBase = false; |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame^] | 2267 | Expr::isLvalueResult InitLvalue = Init->isLvalue(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2268 | Sema::ReferenceCompareResult RefRelationship |
| 2269 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
| 2270 | |
| 2271 | // C++ [dcl.init.ref]p5: |
| 2272 | // A reference to type "cv1 T1" is initialized by an expression |
| 2273 | // of type "cv2 T2" as follows: |
| 2274 | |
| 2275 | // -- If the initializer expression |
| 2276 | |
| 2277 | // C++ [over.ics.ref]p3: |
| 2278 | // Except for an implicit object parameter, for which see 13.3.1, |
| 2279 | // a standard conversion sequence cannot be formed if it requires |
| 2280 | // binding an lvalue reference to non-const to an rvalue or |
| 2281 | // binding an rvalue reference to an lvalue. |
| 2282 | if (isRValRef && InitLvalue == Expr::LV_Valid) |
| 2283 | return ICS; |
| 2284 | |
| 2285 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2286 | // reference-compatible with "cv2 T2," or |
| 2287 | // |
| 2288 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2289 | if (InitLvalue == Expr::LV_Valid && |
| 2290 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2291 | // C++ [over.ics.ref]p1: |
| 2292 | // When a parameter of reference type binds directly (8.5.3) |
| 2293 | // to an argument expression, the implicit conversion sequence |
| 2294 | // is the identity conversion, unless the argument expression |
| 2295 | // has a type that is a derived class of the parameter type, |
| 2296 | // in which case the implicit conversion sequence is a |
| 2297 | // derived-to-base Conversion (13.3.3.1). |
| 2298 | ICS.setStandard(); |
| 2299 | ICS.Standard.First = ICK_Identity; |
| 2300 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2301 | ICS.Standard.Third = ICK_Identity; |
| 2302 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2303 | ICS.Standard.setToType(0, T2); |
| 2304 | ICS.Standard.setToType(1, T1); |
| 2305 | ICS.Standard.setToType(2, T1); |
| 2306 | ICS.Standard.ReferenceBinding = true; |
| 2307 | ICS.Standard.DirectBinding = true; |
| 2308 | ICS.Standard.RRefBinding = false; |
| 2309 | ICS.Standard.CopyConstructor = 0; |
| 2310 | |
| 2311 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2312 | // derived-to-base conversions is suppressed when we're |
| 2313 | // computing the implicit conversion sequence (C++ |
| 2314 | // [over.best.ics]p2). |
| 2315 | return ICS; |
| 2316 | } |
| 2317 | |
| 2318 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2319 | // not reference-related to T2, and can be implicitly |
| 2320 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2321 | // is reference-compatible with "cv3 T3" 92) (this |
| 2322 | // conversion is selected by enumerating the applicable |
| 2323 | // conversion functions (13.3.1.6) and choosing the best |
| 2324 | // one through overload resolution (13.3)), |
| 2325 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
| 2326 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2327 | RefRelationship == Sema::Ref_Incompatible) { |
| 2328 | CXXRecordDecl *T2RecordDecl |
| 2329 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2330 | |
| 2331 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2332 | const UnresolvedSetImpl *Conversions |
| 2333 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2334 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2335 | E = Conversions->end(); I != E; ++I) { |
| 2336 | NamedDecl *D = *I; |
| 2337 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2338 | if (isa<UsingShadowDecl>(D)) |
| 2339 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2340 | |
| 2341 | FunctionTemplateDecl *ConvTemplate |
| 2342 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2343 | CXXConversionDecl *Conv; |
| 2344 | if (ConvTemplate) |
| 2345 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2346 | else |
| 2347 | Conv = cast<CXXConversionDecl>(D); |
| 2348 | |
| 2349 | // If the conversion function doesn't return a reference type, |
| 2350 | // it can't be considered for this conversion. |
| 2351 | if (Conv->getConversionType()->isLValueReferenceType() && |
| 2352 | (AllowExplicit || !Conv->isExplicit())) { |
| 2353 | if (ConvTemplate) |
| 2354 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2355 | Init, DeclType, CandidateSet); |
| 2356 | else |
| 2357 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2358 | DeclType, CandidateSet); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | OverloadCandidateSet::iterator Best; |
| 2363 | switch (S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2364 | case OR_Success: |
| 2365 | // C++ [over.ics.ref]p1: |
| 2366 | // |
| 2367 | // [...] If the parameter binds directly to the result of |
| 2368 | // applying a conversion function to the argument |
| 2369 | // expression, the implicit conversion sequence is a |
| 2370 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2371 | // second standard conversion sequence either an identity |
| 2372 | // conversion or, if the conversion function returns an |
| 2373 | // entity of a type that is a derived class of the parameter |
| 2374 | // type, a derived-to-base Conversion. |
| 2375 | if (!Best->FinalConversion.DirectBinding) |
| 2376 | break; |
| 2377 | |
| 2378 | ICS.setUserDefined(); |
| 2379 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2380 | ICS.UserDefined.After = Best->FinalConversion; |
| 2381 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2382 | ICS.UserDefined.EllipsisConversion = false; |
| 2383 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2384 | ICS.UserDefined.After.DirectBinding && |
| 2385 | "Expected a direct reference binding!"); |
| 2386 | return ICS; |
| 2387 | |
| 2388 | case OR_Ambiguous: |
| 2389 | ICS.setAmbiguous(); |
| 2390 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2391 | Cand != CandidateSet.end(); ++Cand) |
| 2392 | if (Cand->Viable) |
| 2393 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2394 | return ICS; |
| 2395 | |
| 2396 | case OR_No_Viable_Function: |
| 2397 | case OR_Deleted: |
| 2398 | // There was no suitable conversion, or we found a deleted |
| 2399 | // conversion; continue with other checks. |
| 2400 | break; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | // -- Otherwise, the reference shall be to a non-volatile const |
| 2405 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 2406 | // rvalue reference and the initializer expression shall be an rvalue. |
| 2407 | if (!isRValRef && T1.getCVRQualifiers() != Qualifiers::Const) |
| 2408 | return ICS; |
| 2409 | |
| 2410 | // -- If the initializer expression is an rvalue, with T2 a |
| 2411 | // class type, and "cv1 T1" is reference-compatible with |
| 2412 | // "cv2 T2," the reference is bound in one of the |
| 2413 | // following ways (the choice is implementation-defined): |
| 2414 | // |
| 2415 | // -- The reference is bound to the object represented by |
| 2416 | // the rvalue (see 3.10) or to a sub-object within that |
| 2417 | // object. |
| 2418 | // |
| 2419 | // -- A temporary of type "cv1 T2" [sic] is created, and |
| 2420 | // a constructor is called to copy the entire rvalue |
| 2421 | // object into the temporary. The reference is bound to |
| 2422 | // the temporary or to a sub-object within the |
| 2423 | // temporary. |
| 2424 | // |
| 2425 | // The constructor that would be used to make the copy |
| 2426 | // shall be callable whether or not the copy is actually |
| 2427 | // done. |
| 2428 | // |
| 2429 | // Note that C++0x [dcl.init.ref]p5 takes away this implementation |
| 2430 | // freedom, so we will always take the first option and never build |
| 2431 | // a temporary in this case. |
| 2432 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
| 2433 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2434 | ICS.setStandard(); |
| 2435 | ICS.Standard.First = ICK_Identity; |
| 2436 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2437 | ICS.Standard.Third = ICK_Identity; |
| 2438 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2439 | ICS.Standard.setToType(0, T2); |
| 2440 | ICS.Standard.setToType(1, T1); |
| 2441 | ICS.Standard.setToType(2, T1); |
| 2442 | ICS.Standard.ReferenceBinding = true; |
| 2443 | ICS.Standard.DirectBinding = false; |
| 2444 | ICS.Standard.RRefBinding = isRValRef; |
| 2445 | ICS.Standard.CopyConstructor = 0; |
| 2446 | return ICS; |
| 2447 | } |
| 2448 | |
| 2449 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2450 | // initialized from the initializer expression using the |
| 2451 | // rules for a non-reference copy initialization (8.5). The |
| 2452 | // reference is then bound to the temporary. If T1 is |
| 2453 | // reference-related to T2, cv1 must be the same |
| 2454 | // cv-qualification as, or greater cv-qualification than, |
| 2455 | // cv2; otherwise, the program is ill-formed. |
| 2456 | if (RefRelationship == Sema::Ref_Related) { |
| 2457 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2458 | // we would be reference-compatible or reference-compatible with |
| 2459 | // added qualification. But that wasn't the case, so the reference |
| 2460 | // initialization fails. |
| 2461 | return ICS; |
| 2462 | } |
| 2463 | |
| 2464 | // If at least one of the types is a class type, the types are not |
| 2465 | // related, and we aren't allowed any user conversions, the |
| 2466 | // reference binding fails. This case is important for breaking |
| 2467 | // recursion, since TryImplicitConversion below will attempt to |
| 2468 | // create a temporary through the use of a copy constructor. |
| 2469 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 2470 | (T1->isRecordType() || T2->isRecordType())) |
| 2471 | return ICS; |
| 2472 | |
| 2473 | // C++ [over.ics.ref]p2: |
| 2474 | // |
| 2475 | // When a parameter of reference type is not bound directly to |
| 2476 | // an argument expression, the conversion sequence is the one |
| 2477 | // required to convert the argument expression to the |
| 2478 | // underlying type of the reference according to |
| 2479 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 2480 | // to copy-initializing a temporary of the underlying type with |
| 2481 | // the argument expression. Any difference in top-level |
| 2482 | // cv-qualification is subsumed by the initialization itself |
| 2483 | // and does not constitute a conversion. |
| 2484 | ICS = S.TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 2485 | /*AllowExplicit=*/false, |
| 2486 | /*ForceRValue=*/false, |
| 2487 | /*InOverloadResolution=*/false); |
| 2488 | |
| 2489 | // Of course, that's still a reference binding. |
| 2490 | if (ICS.isStandard()) { |
| 2491 | ICS.Standard.ReferenceBinding = true; |
| 2492 | ICS.Standard.RRefBinding = isRValRef; |
| 2493 | } else if (ICS.isUserDefined()) { |
| 2494 | ICS.UserDefined.After.ReferenceBinding = true; |
| 2495 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 2496 | } |
| 2497 | return ICS; |
| 2498 | } |
| 2499 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2500 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2501 | /// ToType from the expression From. Return the implicit conversion |
| 2502 | /// sequence required to pass this argument, which may be a bad |
| 2503 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2504 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2505 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2506 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | ImplicitConversionSequence |
| 2508 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2509 | bool SuppressUserConversions, bool ForceRValue, |
| 2510 | bool InOverloadResolution) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2511 | if (ToType->isReferenceType()) |
| 2512 | return TryReferenceInit(*this, From, ToType, |
| 2513 | /*FIXME:*/From->getLocStart(), |
| 2514 | SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame^] | 2515 | /*AllowExplicit=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2516 | |
| 2517 | return TryImplicitConversion(From, ToType, |
| 2518 | SuppressUserConversions, |
| 2519 | /*AllowExplicit=*/false, |
| 2520 | ForceRValue, |
| 2521 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2524 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2525 | /// parameter of the given member function (@c Method) from the |
| 2526 | /// expression @p From. |
| 2527 | ImplicitConversionSequence |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2528 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2529 | CXXMethodDecl *Method, |
| 2530 | CXXRecordDecl *ActingContext) { |
| 2531 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2532 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2533 | // const volatile object. |
| 2534 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2535 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2536 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2537 | |
| 2538 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2539 | // to exit early. |
| 2540 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2541 | |
| 2542 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2543 | QualType FromType = OrigFromType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2544 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2545 | FromType = PT->getPointeeType(); |
| 2546 | |
| 2547 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2548 | |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2549 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2550 | // where X is the class of which the function is a member |
| 2551 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2552 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2554 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2555 | // reference binding here, that allows class rvalues to bind to |
| 2556 | // non-constant references. |
| 2557 | |
| 2558 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2559 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2560 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2561 | if (ImplicitParamType.getCVRQualifiers() |
| 2562 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2563 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2564 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2565 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2566 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2567 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2568 | |
| 2569 | // Check that we have either the same type or a derived type. It |
| 2570 | // affects the conversion rank. |
| 2571 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2572 | ImplicitConversionKind SecondKind; |
| 2573 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2574 | SecondKind = ICK_Identity; |
| 2575 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2576 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2577 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2578 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2579 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2580 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2581 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2582 | |
| 2583 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2584 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2585 | ICS.Standard.setAsIdentityConversion(); |
| 2586 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2587 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2588 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2589 | ICS.Standard.ReferenceBinding = true; |
| 2590 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2591 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2592 | return ICS; |
| 2593 | } |
| 2594 | |
| 2595 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2596 | /// the implicit object parameter for the given Method with the given |
| 2597 | /// expression. |
| 2598 | bool |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2599 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 2600 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2601 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2602 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2603 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2605 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2607 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2608 | FromRecordType = PT->getPointeeType(); |
| 2609 | DestType = Method->getThisType(Context); |
| 2610 | } else { |
| 2611 | FromRecordType = From->getType(); |
| 2612 | DestType = ImplicitParamRecordType; |
| 2613 | } |
| 2614 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2615 | // Note that we always use the true parent context when performing |
| 2616 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | ImplicitConversionSequence ICS |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2618 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2619 | Method->getParent()); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2620 | if (ICS.isBad()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2621 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2622 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2623 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2625 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2626 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2628 | if (!Context.hasSameType(From->getType(), DestType)) |
| 2629 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
| 2630 | /*isLvalue=*/!From->getType()->getAs<PointerType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2631 | return false; |
| 2632 | } |
| 2633 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2634 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2635 | /// expression From to bool (C++0x [conv]p3). |
| 2636 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2638 | // FIXME: Are these flags correct? |
| 2639 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2641 | /*ForceRValue=*/false, |
| 2642 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2646 | /// of the expression From to bool (C++0x [conv]p3). |
| 2647 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2648 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2649 | if (!ICS.isBad()) |
| 2650 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2651 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2652 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2653 | return Diag(From->getSourceRange().getBegin(), |
| 2654 | diag::err_typecheck_bool_condition) |
| 2655 | << From->getType() << From->getSourceRange(); |
| 2656 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2659 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2660 | /// candidate functions, using the given function call arguments. If |
| 2661 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2662 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2663 | /// |
| 2664 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2665 | /// based on an incomplete set of function arguments. This feature is used by |
| 2666 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | void |
| 2668 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2669 | DeclAccessPair FoundDecl, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2670 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2671 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2672 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2673 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2675 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2676 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2678 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2680 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2681 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2682 | // If we get here, it's because we're calling a member function |
| 2683 | // that is named without a member access expression (e.g., |
| 2684 | // "this->f") that was either written explicitly or created |
| 2685 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2686 | // function, e.g., X::f(). We use an empty type for the implied |
| 2687 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2688 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2689 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2690 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2691 | SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2692 | return; |
| 2693 | } |
| 2694 | // We treat a constructor like a non-member function, since its object |
| 2695 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2698 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2699 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2701 | // Overload resolution is always an unevaluated context. |
| 2702 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2703 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2704 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2705 | // C++ [class.copy]p3: |
| 2706 | // A member function template is never instantiated to perform the copy |
| 2707 | // of a class object to an object of its class type. |
| 2708 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2709 | if (NumArgs == 1 && |
| 2710 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 2711 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 2712 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2713 | return; |
| 2714 | } |
| 2715 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2716 | // Add this candidate |
| 2717 | CandidateSet.push_back(OverloadCandidate()); |
| 2718 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2719 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2720 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2721 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2722 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2723 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2724 | |
| 2725 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2726 | |
| 2727 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2728 | // parameters is viable only if it has an ellipsis in its parameter |
| 2729 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2730 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2731 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2732 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2733 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2734 | return; |
| 2735 | } |
| 2736 | |
| 2737 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2738 | // is viable only if the (m+1)st parameter has a default argument |
| 2739 | // (8.3.6). For the purposes of overload resolution, the |
| 2740 | // parameter list is truncated on the right, so that there are |
| 2741 | // exactly m parameters. |
| 2742 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2743 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2744 | // Not enough arguments. |
| 2745 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2746 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2747 | return; |
| 2748 | } |
| 2749 | |
| 2750 | // Determine the implicit conversion sequences for each of the |
| 2751 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2752 | Candidate.Conversions.resize(NumArgs); |
| 2753 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2754 | if (ArgIdx < NumArgsInProto) { |
| 2755 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2756 | // exist for each argument an implicit conversion sequence |
| 2757 | // (13.3.3.1) that converts that argument to the corresponding |
| 2758 | // parameter of F. |
| 2759 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | Candidate.Conversions[ArgIdx] |
| 2761 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 2762 | SuppressUserConversions, |
| 2763 | /*ForceRValue=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2764 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2765 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2766 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2767 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2768 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2769 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2770 | } else { |
| 2771 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2772 | // argument for which there is no corresponding parameter is |
| 2773 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2774 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2779 | /// \brief Add all of the function declarations in the given function set to |
| 2780 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2781 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2782 | Expr **Args, unsigned NumArgs, |
| 2783 | OverloadCandidateSet& CandidateSet, |
| 2784 | bool SuppressUserConversions) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2785 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2786 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 2787 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2788 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2789 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2790 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2791 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2792 | CandidateSet, SuppressUserConversions); |
| 2793 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2794 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2795 | SuppressUserConversions); |
| 2796 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2797 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2798 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2799 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2800 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2801 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2802 | /*FIXME: explicit args */ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2803 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2804 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2805 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2806 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2807 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2808 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2809 | Args, NumArgs, CandidateSet, |
| 2810 | SuppressUserConversions); |
| 2811 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2812 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2815 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2816 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2817 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2818 | QualType ObjectType, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2819 | Expr **Args, unsigned NumArgs, |
| 2820 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2821 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2822 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2823 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2824 | |
| 2825 | if (isa<UsingShadowDecl>(Decl)) |
| 2826 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2827 | |
| 2828 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2829 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2830 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2831 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 2832 | /*ExplicitArgs*/ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2833 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2834 | CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2835 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2836 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2837 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2838 | ObjectType, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2839 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2843 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2844 | /// of candidate functions, using the given function call arguments |
| 2845 | /// and the object argument (@c Object). For example, in a call |
| 2846 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2847 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2848 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2849 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2851 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2852 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 2853 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2854 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2855 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2857 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2858 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2859 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2860 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2862 | if (!CandidateSet.isNewCandidate(Method)) |
| 2863 | return; |
| 2864 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2865 | // Overload resolution is always an unevaluated context. |
| 2866 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2867 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2868 | // Add this candidate |
| 2869 | CandidateSet.push_back(OverloadCandidate()); |
| 2870 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2871 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2872 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2873 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2874 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2875 | |
| 2876 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2877 | |
| 2878 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2879 | // parameters is viable only if it has an ellipsis in its parameter |
| 2880 | // list (8.3.5). |
| 2881 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2882 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2883 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2884 | return; |
| 2885 | } |
| 2886 | |
| 2887 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2888 | // is viable only if the (m+1)st parameter has a default argument |
| 2889 | // (8.3.6). For the purposes of overload resolution, the |
| 2890 | // parameter list is truncated on the right, so that there are |
| 2891 | // exactly m parameters. |
| 2892 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2893 | if (NumArgs < MinRequiredArgs) { |
| 2894 | // Not enough arguments. |
| 2895 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2896 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2897 | return; |
| 2898 | } |
| 2899 | |
| 2900 | Candidate.Viable = true; |
| 2901 | Candidate.Conversions.resize(NumArgs + 1); |
| 2902 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2903 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2904 | // The implicit object argument is ignored. |
| 2905 | Candidate.IgnoreObjectArgument = true; |
| 2906 | else { |
| 2907 | // Determine the implicit conversion sequence for the object |
| 2908 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2909 | Candidate.Conversions[0] |
| 2910 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2911 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2912 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2913 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2914 | return; |
| 2915 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | // Determine the implicit conversion sequences for each of the |
| 2919 | // arguments. |
| 2920 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2921 | if (ArgIdx < NumArgsInProto) { |
| 2922 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2923 | // exist for each argument an implicit conversion sequence |
| 2924 | // (13.3.3.1) that converts that argument to the corresponding |
| 2925 | // parameter of F. |
| 2926 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | Candidate.Conversions[ArgIdx + 1] |
| 2928 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2929 | SuppressUserConversions, |
| 2930 | /*ForceRValue=*/false, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2931 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2932 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2933 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2934 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2935 | break; |
| 2936 | } |
| 2937 | } else { |
| 2938 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2939 | // argument for which there is no corresponding parameter is |
| 2940 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2941 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2942 | } |
| 2943 | } |
| 2944 | } |
| 2945 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2946 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2947 | /// set, using template argument deduction to produce an appropriate member |
| 2948 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2950 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2951 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2952 | CXXRecordDecl *ActingContext, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2953 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2954 | QualType ObjectType, |
| 2955 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2956 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2957 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2958 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2959 | return; |
| 2960 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2961 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2963 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | // 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] | 2965 | // candidate functions in the usual way.113) A given name can refer to one |
| 2966 | // or more function templates and also to a set of overloaded non-template |
| 2967 | // functions. In such a case, the candidate functions generated from each |
| 2968 | // function template are combined with the set of non-template candidate |
| 2969 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2970 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2971 | FunctionDecl *Specialization = 0; |
| 2972 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2973 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2974 | Args, NumArgs, Specialization, Info)) { |
| 2975 | // FIXME: Record what happened with template argument deduction, so |
| 2976 | // that we can give the user a beautiful diagnostic. |
| 2977 | (void)Result; |
| 2978 | return; |
| 2979 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2981 | // Add the function template specialization produced by template argument |
| 2982 | // deduction as a candidate. |
| 2983 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2985 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2986 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2987 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2988 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2991 | /// \brief Add a C++ function template specialization as a candidate |
| 2992 | /// in the candidate set, using template argument deduction to produce |
| 2993 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2995 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2996 | DeclAccessPair FoundDecl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2997 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2998 | Expr **Args, unsigned NumArgs, |
| 2999 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3000 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3001 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3002 | return; |
| 3003 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3004 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3006 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | // 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] | 3008 | // candidate functions in the usual way.113) A given name can refer to one |
| 3009 | // or more function templates and also to a set of overloaded non-template |
| 3010 | // functions. In such a case, the candidate functions generated from each |
| 3011 | // function template are combined with the set of non-template candidate |
| 3012 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3013 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3014 | FunctionDecl *Specialization = 0; |
| 3015 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3016 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3017 | Args, NumArgs, Specialization, Info)) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3018 | CandidateSet.push_back(OverloadCandidate()); |
| 3019 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3020 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3021 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3022 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3023 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3024 | Candidate.IsSurrogate = false; |
| 3025 | Candidate.IgnoreObjectArgument = false; |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 3026 | |
| 3027 | // TODO: record more information about failed template arguments |
| 3028 | Candidate.DeductionFailure.Result = Result; |
| 3029 | Candidate.DeductionFailure.TemplateParameter = Info.Param.getOpaqueValue(); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3030 | return; |
| 3031 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3033 | // Add the function template specialization produced by template argument |
| 3034 | // deduction as a candidate. |
| 3035 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3036 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3037 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3038 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3040 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3041 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3042 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | /// 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] | 3044 | /// (which may or may not be the same type as the type that the |
| 3045 | /// conversion function produces). |
| 3046 | void |
| 3047 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3048 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3049 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3050 | Expr *From, QualType ToType, |
| 3051 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3052 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3053 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 3054 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3055 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3056 | return; |
| 3057 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3058 | // Overload resolution is always an unevaluated context. |
| 3059 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3060 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3061 | // Add this candidate |
| 3062 | CandidateSet.push_back(OverloadCandidate()); |
| 3063 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3064 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3065 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3066 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3067 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3068 | Candidate.FinalConversion.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3069 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3070 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3071 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3072 | // Determine the implicit conversion sequence for the implicit |
| 3073 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3074 | Candidate.Viable = true; |
| 3075 | Candidate.Conversions.resize(1); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3076 | Candidate.Conversions[0] |
| 3077 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 3078 | ActingContext); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3079 | // Conversion functions to a different type in the base class is visible in |
| 3080 | // the derived class. So, a derived to base conversion should not participate |
| 3081 | // in overload resolution. |
| 3082 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 3083 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3084 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3085 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3086 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3087 | return; |
| 3088 | } |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3089 | |
| 3090 | // We won't go through a user-define type conversion function to convert a |
| 3091 | // derived to base as such conversions are given Conversion Rank. They only |
| 3092 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3093 | QualType FromCanon |
| 3094 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3095 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3096 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3097 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3098 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3099 | return; |
| 3100 | } |
| 3101 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3102 | |
| 3103 | // To determine what the conversion from the result of calling the |
| 3104 | // conversion function to the type we're eventually trying to |
| 3105 | // convert to (ToType), we need to synthesize a call to the |
| 3106 | // conversion function and attempt copy initialization from it. This |
| 3107 | // makes sure that we get the right semantics with respect to |
| 3108 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3109 | // call on the stack and we don't need its arguments to be |
| 3110 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3112 | From->getLocStart()); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3113 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3114 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3115 | &ConversionRef, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | |
| 3117 | // 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] | 3118 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3119 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3121 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3122 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | ImplicitConversionSequence ICS = |
| 3124 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3125 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3126 | /*ForceRValue=*/false, |
| 3127 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3129 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3130 | case ImplicitConversionSequence::StandardConversion: |
| 3131 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3132 | |
| 3133 | // C++ [over.ics.user]p3: |
| 3134 | // If the user-defined conversion is specified by a specialization of a |
| 3135 | // conversion function template, the second standard conversion sequence |
| 3136 | // shall have exact match rank. |
| 3137 | if (Conversion->getPrimaryTemplate() && |
| 3138 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3139 | Candidate.Viable = false; |
| 3140 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3141 | } |
| 3142 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3143 | break; |
| 3144 | |
| 3145 | case ImplicitConversionSequence::BadConversion: |
| 3146 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3147 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3148 | break; |
| 3149 | |
| 3150 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3152 | "Can only end up with a standard conversion sequence or failure"); |
| 3153 | } |
| 3154 | } |
| 3155 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3156 | /// \brief Adds a conversion function template specialization |
| 3157 | /// candidate to the overload set, using template argument deduction |
| 3158 | /// to deduce the template arguments of the conversion function |
| 3159 | /// template from the type that we are converting to (C++ |
| 3160 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3162 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3163 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3164 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3165 | Expr *From, QualType ToType, |
| 3166 | OverloadCandidateSet &CandidateSet) { |
| 3167 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3168 | "Only conversion function templates permitted here"); |
| 3169 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3170 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3171 | return; |
| 3172 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3173 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3174 | CXXConversionDecl *Specialization = 0; |
| 3175 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3177 | Specialization, Info)) { |
| 3178 | // FIXME: Record what happened with template argument deduction, so |
| 3179 | // that we can give the user a beautiful diagnostic. |
| 3180 | (void)Result; |
| 3181 | return; |
| 3182 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3184 | // Add the conversion function template specialization produced by |
| 3185 | // template argument deduction as a candidate. |
| 3186 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3187 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3188 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3191 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3192 | /// converts the given @c Object to a function pointer via the |
| 3193 | /// conversion function @c Conversion, and then attempts to call it |
| 3194 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3195 | /// the type of function that we'll eventually be calling. |
| 3196 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3197 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3198 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3199 | const FunctionProtoType *Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3200 | QualType ObjectType, |
| 3201 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3202 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3203 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3204 | return; |
| 3205 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3206 | // Overload resolution is always an unevaluated context. |
| 3207 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3208 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3209 | CandidateSet.push_back(OverloadCandidate()); |
| 3210 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3211 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3212 | Candidate.Function = 0; |
| 3213 | Candidate.Surrogate = Conversion; |
| 3214 | Candidate.Viable = true; |
| 3215 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3216 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3217 | Candidate.Conversions.resize(NumArgs + 1); |
| 3218 | |
| 3219 | // Determine the implicit conversion sequence for the implicit |
| 3220 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | ImplicitConversionSequence ObjectInit |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3222 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3223 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3224 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3225 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3226 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3227 | return; |
| 3228 | } |
| 3229 | |
| 3230 | // The first conversion is actually a user-defined conversion whose |
| 3231 | // first conversion is ObjectInit's standard conversion (which is |
| 3232 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3233 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3234 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3235 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3236 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3238 | = Candidate.Conversions[0].UserDefined.Before; |
| 3239 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3240 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3242 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3243 | |
| 3244 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3245 | // parameters is viable only if it has an ellipsis in its parameter |
| 3246 | // list (8.3.5). |
| 3247 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3248 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3249 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3250 | return; |
| 3251 | } |
| 3252 | |
| 3253 | // Function types don't have any default arguments, so just check if |
| 3254 | // we have enough arguments. |
| 3255 | if (NumArgs < NumArgsInProto) { |
| 3256 | // Not enough arguments. |
| 3257 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3258 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3259 | return; |
| 3260 | } |
| 3261 | |
| 3262 | // Determine the implicit conversion sequences for each of the |
| 3263 | // arguments. |
| 3264 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3265 | if (ArgIdx < NumArgsInProto) { |
| 3266 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3267 | // exist for each argument an implicit conversion sequence |
| 3268 | // (13.3.3.1) that converts that argument to the corresponding |
| 3269 | // parameter of F. |
| 3270 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | Candidate.Conversions[ArgIdx + 1] |
| 3272 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3273 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3274 | /*ForceRValue=*/false, |
| 3275 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3276 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3277 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3278 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3279 | break; |
| 3280 | } |
| 3281 | } else { |
| 3282 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3283 | // argument for which there is no corresponding parameter is |
| 3284 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3285 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | } |
| 3289 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3290 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 3291 | // operator overloading logic over to the scheme used by binary operators, which |
| 3292 | // works for template instantiation. |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3293 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3294 | SourceLocation OpLoc, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3295 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3296 | OverloadCandidateSet& CandidateSet, |
| 3297 | SourceRange OpRange) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3298 | UnresolvedSet<16> Fns; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3299 | |
| 3300 | QualType T1 = Args[0]->getType(); |
| 3301 | QualType T2; |
| 3302 | if (NumArgs > 1) |
| 3303 | T2 = Args[1]->getType(); |
| 3304 | |
| 3305 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3306 | if (S) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3307 | LookupOverloadedOperatorName(Op, S, T1, T2, Fns); |
| 3308 | AddFunctionCandidates(Fns, Args, NumArgs, CandidateSet, false); |
| 3309 | AddArgumentDependentLookupCandidates(OpName, false, Args, NumArgs, 0, |
| 3310 | CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3311 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3312 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | /// \brief Add overload candidates for overloaded operators that are |
| 3316 | /// member functions. |
| 3317 | /// |
| 3318 | /// Add the overloaded operator candidates that are member functions |
| 3319 | /// for the operator Op that was used in an operator expression such |
| 3320 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3321 | /// CandidateSet will store the added overload candidates. (C++ |
| 3322 | /// [over.match.oper]). |
| 3323 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3324 | SourceLocation OpLoc, |
| 3325 | Expr **Args, unsigned NumArgs, |
| 3326 | OverloadCandidateSet& CandidateSet, |
| 3327 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3328 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3329 | |
| 3330 | // C++ [over.match.oper]p3: |
| 3331 | // For a unary operator @ with an operand of a type whose |
| 3332 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3333 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3334 | // a right operand of a type whose cv-unqualified version is T2, |
| 3335 | // three sets of candidate functions, designated member |
| 3336 | // candidates, non-member candidates and built-in candidates, are |
| 3337 | // constructed as follows: |
| 3338 | QualType T1 = Args[0]->getType(); |
| 3339 | QualType T2; |
| 3340 | if (NumArgs > 1) |
| 3341 | T2 = Args[1]->getType(); |
| 3342 | |
| 3343 | // -- If T1 is a class type, the set of member candidates is the |
| 3344 | // result of the qualified lookup of T1::operator@ |
| 3345 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3346 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3347 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3348 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3349 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3350 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3352 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3353 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3354 | Operators.suppressDiagnostics(); |
| 3355 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3357 | OperEnd = Operators.end(); |
| 3358 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3359 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3360 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3361 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3362 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3363 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3366 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3367 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3368 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3369 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3370 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3371 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3372 | /// (at the beginning of the argument list) that will be contextually |
| 3373 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3375 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3376 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3377 | bool IsAssignmentOperator, |
| 3378 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3379 | // Overload resolution is always an unevaluated context. |
| 3380 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3381 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3382 | // Add this candidate |
| 3383 | CandidateSet.push_back(OverloadCandidate()); |
| 3384 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3385 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3386 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3387 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3388 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3389 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3390 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3391 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3392 | |
| 3393 | // Determine the implicit conversion sequences for each of the |
| 3394 | // arguments. |
| 3395 | Candidate.Viable = true; |
| 3396 | Candidate.Conversions.resize(NumArgs); |
| 3397 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3398 | // C++ [over.match.oper]p4: |
| 3399 | // For the built-in assignment operators, conversions of the |
| 3400 | // left operand are restricted as follows: |
| 3401 | // -- no temporaries are introduced to hold the left operand, and |
| 3402 | // -- no user-defined conversions are applied to the left |
| 3403 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3405 | // |
| 3406 | // We block these conversions by turning off user-defined |
| 3407 | // conversions, since that is the only way that initialization of |
| 3408 | // a reference to a non-class type can occur from something that |
| 3409 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3410 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3412 | "Contextual conversion to bool requires bool type"); |
| 3413 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3414 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3415 | Candidate.Conversions[ArgIdx] |
| 3416 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3417 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3418 | /*ForceRValue=*/false, |
| 3419 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3420 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3421 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3422 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3423 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3430 | /// candidate operator functions for built-in operators (C++ |
| 3431 | /// [over.built]). The types are separated into pointer types and |
| 3432 | /// enumeration types. |
| 3433 | class BuiltinCandidateTypeSet { |
| 3434 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3435 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3436 | |
| 3437 | /// PointerTypes - The set of pointer types that will be used in the |
| 3438 | /// built-in candidates. |
| 3439 | TypeSet PointerTypes; |
| 3440 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3441 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3442 | /// used in the built-in candidates. |
| 3443 | TypeSet MemberPointerTypes; |
| 3444 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3445 | /// EnumerationTypes - The set of enumeration types that will be |
| 3446 | /// used in the built-in candidates. |
| 3447 | TypeSet EnumerationTypes; |
| 3448 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3449 | /// Sema - The semantic analysis instance where we are building the |
| 3450 | /// candidate type set. |
| 3451 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3452 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3453 | /// Context - The AST context in which we will build the type sets. |
| 3454 | ASTContext &Context; |
| 3455 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3456 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3457 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3458 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3459 | |
| 3460 | public: |
| 3461 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3462 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3463 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3465 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3467 | void AddTypesConvertedFrom(QualType Ty, |
| 3468 | SourceLocation Loc, |
| 3469 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3470 | bool AllowExplicitConversions, |
| 3471 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3472 | |
| 3473 | /// pointer_begin - First pointer type found; |
| 3474 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3475 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3476 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3477 | iterator pointer_end() { return PointerTypes.end(); } |
| 3478 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3479 | /// member_pointer_begin - First member pointer type found; |
| 3480 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3481 | |
| 3482 | /// member_pointer_end - Past the last member pointer type found; |
| 3483 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3484 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3485 | /// enumeration_begin - First enumeration type found; |
| 3486 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3487 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3488 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3489 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3490 | }; |
| 3491 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3492 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3493 | /// the set of pointer types along with any more-qualified variants of |
| 3494 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3495 | /// will add "int const *", "int const volatile *", "int const |
| 3496 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3497 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3498 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3499 | /// |
| 3500 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3501 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3502 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3503 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3504 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3505 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3506 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3507 | return false; |
| 3508 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3509 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3510 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3511 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3512 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3513 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3514 | // (the qualifier would sink to the element type), and for another, the |
| 3515 | // only overload situation where it matters is subscript or pointer +- int, |
| 3516 | // and those shouldn't have qualifier variants anyway. |
| 3517 | if (PointeeTy->isArrayType()) |
| 3518 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3519 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3520 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3521 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3522 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3523 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3524 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3525 | // Iterate through all strict supersets of BaseCVR. |
| 3526 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3527 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3528 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3529 | // in the types. |
| 3530 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3531 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3532 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3533 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | return true; |
| 3537 | } |
| 3538 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3539 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3540 | /// to the set of pointer types along with any more-qualified variants of |
| 3541 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3542 | /// will add "int const *", "int const volatile *", "int const |
| 3543 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3544 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3545 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3546 | /// |
| 3547 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3548 | bool |
| 3549 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3550 | QualType Ty) { |
| 3551 | // Insert this type. |
| 3552 | if (!MemberPointerTypes.insert(Ty)) |
| 3553 | return false; |
| 3554 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3555 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3556 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3557 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3558 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3559 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3560 | // (the qualifier would sink to the element type), and for another, the |
| 3561 | // only overload situation where it matters is subscript or pointer +- int, |
| 3562 | // and those shouldn't have qualifier variants anyway. |
| 3563 | if (PointeeTy->isArrayType()) |
| 3564 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3565 | const Type *ClassTy = PointerTy->getClass(); |
| 3566 | |
| 3567 | // Iterate through all strict supersets of the pointee type's CVR |
| 3568 | // qualifiers. |
| 3569 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3570 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3571 | if ((CVR | BaseCVR) != CVR) continue; |
| 3572 | |
| 3573 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3574 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | return true; |
| 3578 | } |
| 3579 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3580 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3581 | /// 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] | 3582 | /// primarily interested in pointer types and enumeration types. We also |
| 3583 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3584 | /// AllowUserConversions is true if we should look at the conversion |
| 3585 | /// functions of a class type, and AllowExplicitConversions if we |
| 3586 | /// should also include the explicit conversion functions of a class |
| 3587 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3589 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3590 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3591 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3592 | bool AllowExplicitConversions, |
| 3593 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3594 | // Only deal with canonical types. |
| 3595 | Ty = Context.getCanonicalType(Ty); |
| 3596 | |
| 3597 | // Look through reference types; they aren't part of the type of an |
| 3598 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3599 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3600 | Ty = RefTy->getPointeeType(); |
| 3601 | |
| 3602 | // We don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3603 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3604 | |
Sebastian Redl | 65ae200 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3605 | // If we're dealing with an array type, decay to the pointer. |
| 3606 | if (Ty->isArrayType()) |
| 3607 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3608 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3609 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3610 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3611 | |
| 3612 | // Insert our type, and its more-qualified variants, into the set |
| 3613 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3614 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3615 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3616 | } else if (Ty->isMemberPointerType()) { |
| 3617 | // Member pointers are far easier, since the pointee can't be converted. |
| 3618 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3619 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3620 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3621 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3622 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3623 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3624 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3625 | // No conversion functions in incomplete types. |
| 3626 | return; |
| 3627 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3629 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3630 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ae01f78 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3631 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3632 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3633 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3634 | NamedDecl *D = I.getDecl(); |
| 3635 | if (isa<UsingShadowDecl>(D)) |
| 3636 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3637 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3639 | // about which builtin types we can convert to. |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3640 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3641 | continue; |
| 3642 | |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3643 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3644 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3645 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3646 | VisibleQuals); |
| 3647 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | } |
| 3652 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3653 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3654 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3655 | /// given type to the candidate set. |
| 3656 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3657 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3659 | unsigned NumArgs, |
| 3660 | OverloadCandidateSet &CandidateSet) { |
| 3661 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3662 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3663 | // T& operator=(T&, T) |
| 3664 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3665 | ParamTypes[1] = T; |
| 3666 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3667 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3669 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3670 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3671 | ParamTypes[0] |
| 3672 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3673 | ParamTypes[1] = T; |
| 3674 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3676 | } |
| 3677 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3679 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3680 | /// 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] | 3681 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3682 | Qualifiers VRQuals; |
| 3683 | const RecordType *TyRec; |
| 3684 | if (const MemberPointerType *RHSMPType = |
| 3685 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3686 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3687 | else |
| 3688 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3689 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3690 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3691 | VRQuals.addVolatile(); |
| 3692 | VRQuals.addRestrict(); |
| 3693 | return VRQuals; |
| 3694 | } |
| 3695 | |
| 3696 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3697 | if (!ClassDecl->hasDefinition()) |
| 3698 | return VRQuals; |
| 3699 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3700 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3701 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3702 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3703 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3704 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3705 | NamedDecl *D = I.getDecl(); |
| 3706 | if (isa<UsingShadowDecl>(D)) |
| 3707 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3708 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3709 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3710 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3711 | CanTy = ResTypeRef->getPointeeType(); |
| 3712 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3713 | // as see them. |
| 3714 | bool done = false; |
| 3715 | while (!done) { |
| 3716 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3717 | CanTy = ResTypePtr->getPointeeType(); |
| 3718 | else if (const MemberPointerType *ResTypeMPtr = |
| 3719 | CanTy->getAs<MemberPointerType>()) |
| 3720 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3721 | else |
| 3722 | done = true; |
| 3723 | if (CanTy.isVolatileQualified()) |
| 3724 | VRQuals.addVolatile(); |
| 3725 | if (CanTy.isRestrictQualified()) |
| 3726 | VRQuals.addRestrict(); |
| 3727 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3728 | return VRQuals; |
| 3729 | } |
| 3730 | } |
| 3731 | } |
| 3732 | return VRQuals; |
| 3733 | } |
| 3734 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3735 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3736 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3737 | /// on the operator @p Op and the arguments given. For example, if the |
| 3738 | /// operator is a binary '+', this routine might add "int |
| 3739 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3740 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3742 | SourceLocation OpLoc, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3743 | Expr **Args, unsigned NumArgs, |
| 3744 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3745 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3746 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3747 | // that the first few of these types are the promoted integral |
| 3748 | // types; these types need to be first. |
| 3749 | // FIXME: What about complex? |
| 3750 | const unsigned FirstIntegralType = 0; |
| 3751 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3753 | LastPromotedIntegralType = 13; |
| 3754 | const unsigned FirstPromotedArithmeticType = 7, |
| 3755 | LastPromotedArithmeticType = 16; |
| 3756 | const unsigned NumArithmeticTypes = 16; |
| 3757 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3759 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3760 | Context.SignedCharTy, Context.ShortTy, |
| 3761 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3762 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3763 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3764 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3765 | }; |
Douglas Gregor | b8440a7 | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3766 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3767 | "Invalid first promoted integral type"); |
| 3768 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3769 | == Context.UnsignedLongLongTy && |
| 3770 | "Invalid last promoted integral type"); |
| 3771 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3772 | "Invalid first promoted arithmetic type"); |
| 3773 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3774 | == Context.LongDoubleTy && |
| 3775 | "Invalid last promoted arithmetic type"); |
| 3776 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3777 | // Find all of the types that the arguments can convert to, but only |
| 3778 | // if the operator we're looking at has built-in operator candidates |
| 3779 | // that make use of these types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3780 | Qualifiers VisibleTypeConversionsQuals; |
| 3781 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3782 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3783 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3784 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3785 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3786 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3787 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3788 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3789 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3790 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3791 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3792 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3793 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3794 | OpLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3795 | true, |
| 3796 | (Op == OO_Exclaim || |
| 3797 | Op == OO_AmpAmp || |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3798 | Op == OO_PipePipe), |
| 3799 | VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | bool isComparison = false; |
| 3803 | switch (Op) { |
| 3804 | case OO_None: |
| 3805 | case NUM_OVERLOADED_OPERATORS: |
| 3806 | assert(false && "Expected an overloaded operator"); |
| 3807 | break; |
| 3808 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3809 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3810 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3811 | goto UnaryStar; |
| 3812 | else |
| 3813 | goto BinaryStar; |
| 3814 | break; |
| 3815 | |
| 3816 | case OO_Plus: // '+' is either unary or binary |
| 3817 | if (NumArgs == 1) |
| 3818 | goto UnaryPlus; |
| 3819 | else |
| 3820 | goto BinaryPlus; |
| 3821 | break; |
| 3822 | |
| 3823 | case OO_Minus: // '-' is either unary or binary |
| 3824 | if (NumArgs == 1) |
| 3825 | goto UnaryMinus; |
| 3826 | else |
| 3827 | goto BinaryMinus; |
| 3828 | break; |
| 3829 | |
| 3830 | case OO_Amp: // '&' is either unary or binary |
| 3831 | if (NumArgs == 1) |
| 3832 | goto UnaryAmp; |
| 3833 | else |
| 3834 | goto BinaryAmp; |
| 3835 | |
| 3836 | case OO_PlusPlus: |
| 3837 | case OO_MinusMinus: |
| 3838 | // C++ [over.built]p3: |
| 3839 | // |
| 3840 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3841 | // is either volatile or empty, there exist candidate operator |
| 3842 | // functions of the form |
| 3843 | // |
| 3844 | // VQ T& operator++(VQ T&); |
| 3845 | // T operator++(VQ T&, int); |
| 3846 | // |
| 3847 | // C++ [over.built]p4: |
| 3848 | // |
| 3849 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3850 | // than bool, and VQ is either volatile or empty, there exist |
| 3851 | // candidate operator functions of the form |
| 3852 | // |
| 3853 | // VQ T& operator--(VQ T&); |
| 3854 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3856 | Arith < NumArithmeticTypes; ++Arith) { |
| 3857 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3859 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3860 | |
| 3861 | // Non-volatile version. |
| 3862 | if (NumArgs == 1) |
| 3863 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3864 | else |
| 3865 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3866 | // heuristic to reduce number of builtin candidates in the set. |
| 3867 | // Add volatile version only if there are conversions to a volatile type. |
| 3868 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3869 | // Volatile version |
| 3870 | ParamTypes[0] |
| 3871 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3872 | if (NumArgs == 1) |
| 3873 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3874 | else |
| 3875 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3876 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | // C++ [over.built]p5: |
| 3880 | // |
| 3881 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3882 | // cv-unqualified object type, and VQ is either volatile or |
| 3883 | // empty, there exist candidate operator functions of the form |
| 3884 | // |
| 3885 | // T*VQ& operator++(T*VQ&); |
| 3886 | // T*VQ& operator--(T*VQ&); |
| 3887 | // T* operator++(T*VQ&, int); |
| 3888 | // T* operator--(T*VQ&, int); |
| 3889 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3890 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3891 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3892 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3893 | continue; |
| 3894 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | QualType ParamTypes[2] = { |
| 3896 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3897 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3899 | // Without volatile |
| 3900 | if (NumArgs == 1) |
| 3901 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3902 | else |
| 3903 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3904 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3905 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3906 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3907 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3908 | ParamTypes[0] |
| 3909 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3910 | if (NumArgs == 1) |
| 3911 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3912 | else |
| 3913 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3914 | } |
| 3915 | } |
| 3916 | break; |
| 3917 | |
| 3918 | UnaryStar: |
| 3919 | // C++ [over.built]p6: |
| 3920 | // For every cv-qualified or cv-unqualified object type T, there |
| 3921 | // exist candidate operator functions of the form |
| 3922 | // |
| 3923 | // T& operator*(T*); |
| 3924 | // |
| 3925 | // C++ [over.built]p7: |
| 3926 | // For every function type T, there exist candidate operator |
| 3927 | // functions of the form |
| 3928 | // T& operator*(T*); |
| 3929 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3930 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3931 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3932 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3934 | &ParamTy, Args, 1, CandidateSet); |
| 3935 | } |
| 3936 | break; |
| 3937 | |
| 3938 | UnaryPlus: |
| 3939 | // C++ [over.built]p8: |
| 3940 | // For every type T, there exist candidate operator functions of |
| 3941 | // the form |
| 3942 | // |
| 3943 | // T* operator+(T*); |
| 3944 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3945 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3946 | QualType ParamTy = *Ptr; |
| 3947 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3948 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3949 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3950 | // Fall through |
| 3951 | |
| 3952 | UnaryMinus: |
| 3953 | // C++ [over.built]p9: |
| 3954 | // For every promoted arithmetic type T, there exist candidate |
| 3955 | // operator functions of the form |
| 3956 | // |
| 3957 | // T operator+(T); |
| 3958 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3959 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3960 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3961 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3962 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3963 | } |
| 3964 | break; |
| 3965 | |
| 3966 | case OO_Tilde: |
| 3967 | // C++ [over.built]p10: |
| 3968 | // For every promoted integral type T, there exist candidate |
| 3969 | // operator functions of the form |
| 3970 | // |
| 3971 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3972 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3973 | Int < LastPromotedIntegralType; ++Int) { |
| 3974 | QualType IntTy = ArithmeticTypes[Int]; |
| 3975 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3976 | } |
| 3977 | break; |
| 3978 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3979 | case OO_New: |
| 3980 | case OO_Delete: |
| 3981 | case OO_Array_New: |
| 3982 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3983 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3984 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3985 | break; |
| 3986 | |
| 3987 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3988 | UnaryAmp: |
| 3989 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3990 | // C++ [over.match.oper]p3: |
| 3991 | // -- For the operator ',', the unary operator '&', or the |
| 3992 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3993 | break; |
| 3994 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3995 | case OO_EqualEqual: |
| 3996 | case OO_ExclaimEqual: |
| 3997 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3998 | // For every pointer to member type T, there exist candidate operator |
| 3999 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4000 | // |
| 4001 | // bool operator==(T,T); |
| 4002 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4004 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4005 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4006 | MemPtr != MemPtrEnd; |
| 4007 | ++MemPtr) { |
| 4008 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4009 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4010 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4012 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4013 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4014 | case OO_Less: |
| 4015 | case OO_Greater: |
| 4016 | case OO_LessEqual: |
| 4017 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4018 | // C++ [over.built]p15: |
| 4019 | // |
| 4020 | // For every pointer or enumeration type T, there exist |
| 4021 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4022 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4023 | // bool operator<(T, T); |
| 4024 | // bool operator>(T, T); |
| 4025 | // bool operator<=(T, T); |
| 4026 | // bool operator>=(T, T); |
| 4027 | // bool operator==(T, T); |
| 4028 | // bool operator!=(T, T); |
| 4029 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4030 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4031 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4032 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4033 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4035 | = CandidateTypes.enumeration_begin(); |
| 4036 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4037 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4038 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4039 | } |
| 4040 | |
| 4041 | // Fall through. |
| 4042 | isComparison = true; |
| 4043 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4044 | BinaryPlus: |
| 4045 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4046 | if (!isComparison) { |
| 4047 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4048 | |
| 4049 | // C++ [over.built]p13: |
| 4050 | // |
| 4051 | // For every cv-qualified or cv-unqualified object type T |
| 4052 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4054 | // T* operator+(T*, ptrdiff_t); |
| 4055 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4056 | // T* operator-(T*, ptrdiff_t); |
| 4057 | // T* operator+(ptrdiff_t, T*); |
| 4058 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4059 | // |
| 4060 | // C++ [over.built]p14: |
| 4061 | // |
| 4062 | // For every T, where T is a pointer to object type, there |
| 4063 | // exist candidate operator functions of the form |
| 4064 | // |
| 4065 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4067 | = CandidateTypes.pointer_begin(); |
| 4068 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4069 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4070 | |
| 4071 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4072 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4073 | |
| 4074 | if (Op == OO_Plus) { |
| 4075 | // T* operator+(ptrdiff_t, T*); |
| 4076 | ParamTypes[0] = ParamTypes[1]; |
| 4077 | ParamTypes[1] = *Ptr; |
| 4078 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4079 | } else { |
| 4080 | // ptrdiff_t operator-(T, T); |
| 4081 | ParamTypes[1] = *Ptr; |
| 4082 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4083 | Args, 2, CandidateSet); |
| 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | // Fall through |
| 4088 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4089 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4090 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4091 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4092 | // C++ [over.built]p12: |
| 4093 | // |
| 4094 | // For every pair of promoted arithmetic types L and R, there |
| 4095 | // exist candidate operator functions of the form |
| 4096 | // |
| 4097 | // LR operator*(L, R); |
| 4098 | // LR operator/(L, R); |
| 4099 | // LR operator+(L, R); |
| 4100 | // LR operator-(L, R); |
| 4101 | // bool operator<(L, R); |
| 4102 | // bool operator>(L, R); |
| 4103 | // bool operator<=(L, R); |
| 4104 | // bool operator>=(L, R); |
| 4105 | // bool operator==(L, R); |
| 4106 | // bool operator!=(L, R); |
| 4107 | // |
| 4108 | // where LR is the result of the usual arithmetic conversions |
| 4109 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4110 | // |
| 4111 | // C++ [over.built]p24: |
| 4112 | // |
| 4113 | // For every pair of promoted arithmetic types L and R, there exist |
| 4114 | // candidate operator functions of the form |
| 4115 | // |
| 4116 | // LR operator?(bool, L, R); |
| 4117 | // |
| 4118 | // where LR is the result of the usual arithmetic conversions |
| 4119 | // between types L and R. |
| 4120 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4122 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4124 | Right < LastPromotedArithmeticType; ++Right) { |
| 4125 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4126 | QualType Result |
| 4127 | = isComparison |
| 4128 | ? Context.BoolTy |
| 4129 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4130 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4131 | } |
| 4132 | } |
| 4133 | break; |
| 4134 | |
| 4135 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4136 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4137 | case OO_Caret: |
| 4138 | case OO_Pipe: |
| 4139 | case OO_LessLess: |
| 4140 | case OO_GreaterGreater: |
| 4141 | // C++ [over.built]p17: |
| 4142 | // |
| 4143 | // For every pair of promoted integral types L and R, there |
| 4144 | // exist candidate operator functions of the form |
| 4145 | // |
| 4146 | // LR operator%(L, R); |
| 4147 | // LR operator&(L, R); |
| 4148 | // LR operator^(L, R); |
| 4149 | // LR operator|(L, R); |
| 4150 | // L operator<<(L, R); |
| 4151 | // L operator>>(L, R); |
| 4152 | // |
| 4153 | // where LR is the result of the usual arithmetic conversions |
| 4154 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4155 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4156 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4158 | Right < LastPromotedIntegralType; ++Right) { |
| 4159 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4160 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4161 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4162 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4163 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4164 | } |
| 4165 | } |
| 4166 | break; |
| 4167 | |
| 4168 | case OO_Equal: |
| 4169 | // C++ [over.built]p20: |
| 4170 | // |
| 4171 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4172 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4173 | // empty, there exist candidate operator functions of the form |
| 4174 | // |
| 4175 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4176 | for (BuiltinCandidateTypeSet::iterator |
| 4177 | Enum = CandidateTypes.enumeration_begin(), |
| 4178 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4179 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4180 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4181 | CandidateSet); |
| 4182 | for (BuiltinCandidateTypeSet::iterator |
| 4183 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4184 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4185 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4187 | CandidateSet); |
| 4188 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4189 | |
| 4190 | case OO_PlusEqual: |
| 4191 | case OO_MinusEqual: |
| 4192 | // C++ [over.built]p19: |
| 4193 | // |
| 4194 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4195 | // volatile or empty, there exist candidate operator functions |
| 4196 | // of the form |
| 4197 | // |
| 4198 | // T*VQ& operator=(T*VQ&, T*); |
| 4199 | // |
| 4200 | // C++ [over.built]p21: |
| 4201 | // |
| 4202 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4203 | // cv-unqualified object type and VQ is either volatile or |
| 4204 | // empty, there exist candidate operator functions of the form |
| 4205 | // |
| 4206 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4207 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4208 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4209 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4210 | QualType ParamTypes[2]; |
| 4211 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4212 | |
| 4213 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4214 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4215 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4216 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4217 | |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4218 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4219 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4220 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4221 | ParamTypes[0] |
| 4222 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4223 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4224 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4225 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4226 | } |
| 4227 | // Fall through. |
| 4228 | |
| 4229 | case OO_StarEqual: |
| 4230 | case OO_SlashEqual: |
| 4231 | // C++ [over.built]p18: |
| 4232 | // |
| 4233 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 4234 | // VQ is either volatile or empty, and R is a promoted |
| 4235 | // arithmetic type, there exist candidate operator functions of |
| 4236 | // the form |
| 4237 | // |
| 4238 | // VQ L& operator=(VQ L&, R); |
| 4239 | // VQ L& operator*=(VQ L&, R); |
| 4240 | // VQ L& operator/=(VQ L&, R); |
| 4241 | // VQ L& operator+=(VQ L&, R); |
| 4242 | // VQ L& operator-=(VQ L&, R); |
| 4243 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4245 | Right < LastPromotedArithmeticType; ++Right) { |
| 4246 | QualType ParamTypes[2]; |
| 4247 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4248 | |
| 4249 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4250 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4251 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4252 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4253 | |
| 4254 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4255 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4256 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 4257 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4258 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4259 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4260 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4261 | } |
| 4262 | } |
| 4263 | break; |
| 4264 | |
| 4265 | case OO_PercentEqual: |
| 4266 | case OO_LessLessEqual: |
| 4267 | case OO_GreaterGreaterEqual: |
| 4268 | case OO_AmpEqual: |
| 4269 | case OO_CaretEqual: |
| 4270 | case OO_PipeEqual: |
| 4271 | // C++ [over.built]p22: |
| 4272 | // |
| 4273 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 4274 | // is either volatile or empty, and R is a promoted integral |
| 4275 | // type, there exist candidate operator functions of the form |
| 4276 | // |
| 4277 | // VQ L& operator%=(VQ L&, R); |
| 4278 | // VQ L& operator<<=(VQ L&, R); |
| 4279 | // VQ L& operator>>=(VQ L&, R); |
| 4280 | // VQ L& operator&=(VQ L&, R); |
| 4281 | // VQ L& operator^=(VQ L&, R); |
| 4282 | // VQ L& operator|=(VQ L&, R); |
| 4283 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4284 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4285 | Right < LastPromotedIntegralType; ++Right) { |
| 4286 | QualType ParamTypes[2]; |
| 4287 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4288 | |
| 4289 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4290 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4291 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a4a9334 | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4292 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4293 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4294 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4295 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4296 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4297 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4298 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } |
| 4301 | break; |
| 4302 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4303 | case OO_Exclaim: { |
| 4304 | // C++ [over.operator]p23: |
| 4305 | // |
| 4306 | // There also exist candidate operator functions of the form |
| 4307 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4309 | // bool operator&&(bool, bool); [BELOW] |
| 4310 | // bool operator||(bool, bool); [BELOW] |
| 4311 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4312 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4313 | /*IsAssignmentOperator=*/false, |
| 4314 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4315 | break; |
| 4316 | } |
| 4317 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4318 | case OO_AmpAmp: |
| 4319 | case OO_PipePipe: { |
| 4320 | // C++ [over.operator]p23: |
| 4321 | // |
| 4322 | // There also exist candidate operator functions of the form |
| 4323 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4324 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4325 | // bool operator&&(bool, bool); |
| 4326 | // bool operator||(bool, bool); |
| 4327 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4328 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4329 | /*IsAssignmentOperator=*/false, |
| 4330 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4331 | break; |
| 4332 | } |
| 4333 | |
| 4334 | case OO_Subscript: |
| 4335 | // C++ [over.built]p13: |
| 4336 | // |
| 4337 | // For every cv-qualified or cv-unqualified object type T there |
| 4338 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4340 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4341 | // T& operator[](T*, ptrdiff_t); |
| 4342 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4343 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4344 | // T& operator[](ptrdiff_t, T*); |
| 4345 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4346 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4347 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4348 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4349 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4350 | |
| 4351 | // T& operator[](T*, ptrdiff_t) |
| 4352 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4353 | |
| 4354 | // T& operator[](ptrdiff_t, T*); |
| 4355 | ParamTypes[0] = ParamTypes[1]; |
| 4356 | ParamTypes[1] = *Ptr; |
| 4357 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4358 | } |
| 4359 | break; |
| 4360 | |
| 4361 | case OO_ArrowStar: |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4362 | // C++ [over.built]p11: |
| 4363 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4364 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4365 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4366 | // there exist candidate operator functions of the form |
| 4367 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4368 | // where CV12 is the union of CV1 and CV2. |
| 4369 | { |
| 4370 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4371 | CandidateTypes.pointer_begin(); |
| 4372 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4373 | QualType C1Ty = (*Ptr); |
| 4374 | QualType C1; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4375 | QualifierCollector Q1; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4376 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4377 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4378 | if (!isa<RecordType>(C1)) |
| 4379 | continue; |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4380 | // heuristic to reduce number of builtin candidates in the set. |
| 4381 | // Add volatile/restrict version only if there are conversions to a |
| 4382 | // volatile/restrict type. |
| 4383 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4384 | continue; |
| 4385 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4386 | continue; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4387 | } |
| 4388 | for (BuiltinCandidateTypeSet::iterator |
| 4389 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4390 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4391 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4392 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4393 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 12df37c | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4394 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4395 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4396 | break; |
| 4397 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4398 | // build CV12 T& |
| 4399 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4400 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4401 | T.isVolatileQualified()) |
| 4402 | continue; |
| 4403 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4404 | T.isRestrictQualified()) |
| 4405 | continue; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4406 | T = Q1.apply(T); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4407 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4408 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4409 | } |
| 4410 | } |
| 4411 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4412 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4413 | |
| 4414 | case OO_Conditional: |
| 4415 | // Note that we don't consider the first argument, since it has been |
| 4416 | // contextually converted to bool long ago. The candidates below are |
| 4417 | // therefore added as binary. |
| 4418 | // |
| 4419 | // C++ [over.built]p24: |
| 4420 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4421 | // there exist candidate operator functions of the form |
| 4422 | // |
| 4423 | // T operator?(bool, T, T); |
| 4424 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4425 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4426 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4427 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4428 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4429 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4430 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4431 | CandidateTypes.member_pointer_begin(), |
| 4432 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4433 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4434 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4435 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4436 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4437 | } |
| 4438 | } |
| 4439 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4440 | /// \brief Add function candidates found via argument-dependent lookup |
| 4441 | /// to the set of overloading candidates. |
| 4442 | /// |
| 4443 | /// This routine performs argument-dependent name lookup based on the |
| 4444 | /// given function name (which may also be an operator name) and adds |
| 4445 | /// all of the overload candidates found by ADL to the overload |
| 4446 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4447 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4448 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4449 | bool Operator, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4450 | Expr **Args, unsigned NumArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4451 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4452 | OverloadCandidateSet& CandidateSet, |
| 4453 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4454 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4455 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 4456 | // FIXME: This approach for uniquing ADL results (and removing |
| 4457 | // redundant candidates from the set) relies on pointer-equality, |
| 4458 | // which means we need to key off the canonical decl. However, |
| 4459 | // always going back to the canonical decl might not get us the |
| 4460 | // right set of default arguments. What default arguments are |
| 4461 | // we supposed to consider on ADL candidates, anyway? |
| 4462 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4463 | // FIXME: Pass in the explicit template arguments? |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4464 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4466 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4467 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4468 | CandEnd = CandidateSet.end(); |
| 4469 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4470 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4471 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4472 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4473 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4474 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4475 | |
| 4476 | // For each of the ADL candidates we found, add it to the overload |
| 4477 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4478 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4479 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4480 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4481 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4482 | continue; |
| 4483 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4484 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 4485 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4486 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4487 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4488 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4489 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4490 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4493 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4494 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4495 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4496 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4497 | const OverloadCandidate& Cand2, |
| 4498 | SourceLocation Loc) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4499 | // Define viable functions to be better candidates than non-viable |
| 4500 | // functions. |
| 4501 | if (!Cand2.Viable) |
| 4502 | return Cand1.Viable; |
| 4503 | else if (!Cand1.Viable) |
| 4504 | return false; |
| 4505 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4506 | // C++ [over.match.best]p1: |
| 4507 | // |
| 4508 | // -- if F is a static member function, ICS1(F) is defined such |
| 4509 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4510 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4511 | // better nor worse than ICS1(F). |
| 4512 | unsigned StartArg = 0; |
| 4513 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4514 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4516 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | // A viable function F1 is defined to be a better function than another |
| 4518 | // 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] | 4519 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4520 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4521 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4522 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4523 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4524 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4525 | Cand2.Conversions[ArgIdx])) { |
| 4526 | case ImplicitConversionSequence::Better: |
| 4527 | // Cand1 has a better conversion sequence. |
| 4528 | HasBetterConversion = true; |
| 4529 | break; |
| 4530 | |
| 4531 | case ImplicitConversionSequence::Worse: |
| 4532 | // Cand1 can't be better than Cand2. |
| 4533 | return false; |
| 4534 | |
| 4535 | case ImplicitConversionSequence::Indistinguishable: |
| 4536 | // Do nothing. |
| 4537 | break; |
| 4538 | } |
| 4539 | } |
| 4540 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4541 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4542 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4543 | if (HasBetterConversion) |
| 4544 | return true; |
| 4545 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4546 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4547 | // specialization, or, if not that, |
| 4548 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4549 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4550 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4551 | |
| 4552 | // -- F1 and F2 are function template specializations, and the function |
| 4553 | // template for F1 is more specialized than the template for F2 |
| 4554 | // 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] | 4555 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4556 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4557 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4558 | if (FunctionTemplateDecl *BetterTemplate |
| 4559 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4560 | Cand2.Function->getPrimaryTemplate(), |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4561 | Loc, |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4562 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4563 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4564 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4566 | // -- the context is an initialization by user-defined conversion |
| 4567 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4568 | // from the return type of F1 to the destination type (i.e., |
| 4569 | // the type of the entity being initialized) is a better |
| 4570 | // conversion sequence than the standard conversion sequence |
| 4571 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4572 | if (Cand1.Function && Cand2.Function && |
| 4573 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4574 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4575 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4576 | Cand2.FinalConversion)) { |
| 4577 | case ImplicitConversionSequence::Better: |
| 4578 | // Cand1 has a better conversion sequence. |
| 4579 | return true; |
| 4580 | |
| 4581 | case ImplicitConversionSequence::Worse: |
| 4582 | // Cand1 can't be better than Cand2. |
| 4583 | return false; |
| 4584 | |
| 4585 | case ImplicitConversionSequence::Indistinguishable: |
| 4586 | // Do nothing |
| 4587 | break; |
| 4588 | } |
| 4589 | } |
| 4590 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4591 | return false; |
| 4592 | } |
| 4593 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4594 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4595 | /// within an overload candidate set. |
| 4596 | /// |
| 4597 | /// \param CandidateSet the set of candidate functions. |
| 4598 | /// |
| 4599 | /// \param Loc the location of the function name (or operator symbol) for |
| 4600 | /// which overload resolution occurs. |
| 4601 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4603 | /// function, Best points to the candidate function found. |
| 4604 | /// |
| 4605 | /// \returns The result of overload resolution. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4606 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4607 | SourceLocation Loc, |
| 4608 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4609 | // Find the best viable function. |
| 4610 | Best = CandidateSet.end(); |
| 4611 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4612 | Cand != CandidateSet.end(); ++Cand) { |
| 4613 | if (Cand->Viable) { |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4614 | if (Best == CandidateSet.end() || |
| 4615 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4616 | Best = Cand; |
| 4617 | } |
| 4618 | } |
| 4619 | |
| 4620 | // If we didn't find any viable functions, abort. |
| 4621 | if (Best == CandidateSet.end()) |
| 4622 | return OR_No_Viable_Function; |
| 4623 | |
| 4624 | // Make sure that this function is better than every other viable |
| 4625 | // function. If not, we have an ambiguity. |
| 4626 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4627 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4629 | Cand != Best && |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4630 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4631 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4632 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4633 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4634 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4636 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4637 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4639 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4640 | return OR_Deleted; |
| 4641 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4642 | // C++ [basic.def.odr]p2: |
| 4643 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4644 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4645 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4646 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4647 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4648 | if (Best->Function) |
| 4649 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4650 | return OR_Success; |
| 4651 | } |
| 4652 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4653 | namespace { |
| 4654 | |
| 4655 | enum OverloadCandidateKind { |
| 4656 | oc_function, |
| 4657 | oc_method, |
| 4658 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4659 | oc_function_template, |
| 4660 | oc_method_template, |
| 4661 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4662 | oc_implicit_default_constructor, |
| 4663 | oc_implicit_copy_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4664 | oc_implicit_copy_assignment |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4665 | }; |
| 4666 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4667 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4668 | FunctionDecl *Fn, |
| 4669 | std::string &Description) { |
| 4670 | bool isTemplate = false; |
| 4671 | |
| 4672 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4673 | isTemplate = true; |
| 4674 | Description = S.getTemplateArgumentBindingsText( |
| 4675 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4676 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4677 | |
| 4678 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4679 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4680 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4681 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4682 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4683 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4687 | // This actually gets spelled 'candidate function' for now, but |
| 4688 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4689 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4690 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4691 | |
| 4692 | assert(Meth->isCopyAssignment() |
| 4693 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4694 | return oc_implicit_copy_assignment; |
| 4695 | } |
| 4696 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4697 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | } // end anonymous namespace |
| 4701 | |
| 4702 | // Notes the location of an overload candidate. |
| 4703 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4704 | std::string FnDesc; |
| 4705 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4706 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4707 | << (unsigned) K << FnDesc; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4710 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4711 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4712 | /// target types of the conversion. |
| 4713 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4714 | SourceLocation CaretLoc, |
| 4715 | const PartialDiagnostic &PDiag) { |
| 4716 | Diag(CaretLoc, PDiag) |
| 4717 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4718 | for (AmbiguousConversionSequence::const_iterator |
| 4719 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4720 | NoteOverloadCandidate(*I); |
| 4721 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4724 | namespace { |
| 4725 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4726 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4727 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4728 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4729 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4730 | FunctionDecl *Fn = Cand->Function; |
| 4731 | |
| 4732 | // There's a conversion slot for the object argument if this is a |
| 4733 | // non-constructor method. Note that 'I' corresponds the |
| 4734 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4735 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4736 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4737 | if (I == 0) |
| 4738 | isObjectArgument = true; |
| 4739 | else |
| 4740 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4743 | std::string FnDesc; |
| 4744 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4745 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4746 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4747 | QualType FromTy = Conv.Bad.getFromType(); |
| 4748 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4749 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4750 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4751 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4752 | Expr *E = FromExpr->IgnoreParens(); |
| 4753 | if (isa<UnaryOperator>(E)) |
| 4754 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4755 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4756 | |
| 4757 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 4758 | << (unsigned) FnKind << FnDesc |
| 4759 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4760 | << ToTy << Name << I+1; |
| 4761 | return; |
| 4762 | } |
| 4763 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4764 | // Do some hand-waving analysis to see if the non-viability is due |
| 4765 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4766 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4767 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4768 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4769 | CToTy = RT->getPointeeType(); |
| 4770 | else { |
| 4771 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4772 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4773 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4774 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4775 | } |
| 4776 | |
| 4777 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4778 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4779 | // It is dumb that we have to do this here. |
| 4780 | while (isa<ArrayType>(CFromTy)) |
| 4781 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4782 | while (isa<ArrayType>(CToTy)) |
| 4783 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4784 | |
| 4785 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4786 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4787 | |
| 4788 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4789 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4790 | << (unsigned) FnKind << FnDesc |
| 4791 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4792 | << FromTy |
| 4793 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4794 | << (unsigned) isObjectArgument << I+1; |
| 4795 | return; |
| 4796 | } |
| 4797 | |
| 4798 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4799 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4800 | |
| 4801 | if (isObjectArgument) { |
| 4802 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4803 | << (unsigned) FnKind << FnDesc |
| 4804 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4805 | << FromTy << (CVR - 1); |
| 4806 | } else { |
| 4807 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4808 | << (unsigned) FnKind << FnDesc |
| 4809 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4810 | << FromTy << (CVR - 1) << I+1; |
| 4811 | } |
| 4812 | return; |
| 4813 | } |
| 4814 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4815 | // Diagnose references or pointers to incomplete types differently, |
| 4816 | // since it's far from impossible that the incompleteness triggered |
| 4817 | // the failure. |
| 4818 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 4819 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 4820 | TempFromTy = PTy->getPointeeType(); |
| 4821 | if (TempFromTy->isIncompleteType()) { |
| 4822 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 4823 | << (unsigned) FnKind << FnDesc |
| 4824 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4825 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4826 | return; |
| 4827 | } |
| 4828 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4829 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4830 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4831 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4832 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 4833 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4837 | unsigned NumFormalArgs) { |
| 4838 | // TODO: treat calls to a missing default constructor as a special case |
| 4839 | |
| 4840 | FunctionDecl *Fn = Cand->Function; |
| 4841 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4842 | |
| 4843 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4844 | |
| 4845 | // at least / at most / exactly |
| 4846 | unsigned mode, modeCount; |
| 4847 | if (NumFormalArgs < MinParams) { |
| 4848 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4849 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4850 | mode = 0; // "at least" |
| 4851 | else |
| 4852 | mode = 2; // "exactly" |
| 4853 | modeCount = MinParams; |
| 4854 | } else { |
| 4855 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4856 | if (MinParams != FnTy->getNumArgs()) |
| 4857 | mode = 1; // "at most" |
| 4858 | else |
| 4859 | mode = 2; // "exactly" |
| 4860 | modeCount = FnTy->getNumArgs(); |
| 4861 | } |
| 4862 | |
| 4863 | std::string Description; |
| 4864 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4865 | |
| 4866 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4867 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4870 | /// Diagnose a failed template-argument deduction. |
| 4871 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 4872 | Expr **Args, unsigned NumArgs) { |
| 4873 | FunctionDecl *Fn = Cand->Function; // pattern |
| 4874 | |
| 4875 | TemplateParameter Param = TemplateParameter::getFromOpaqueValue( |
| 4876 | Cand->DeductionFailure.TemplateParameter); |
| 4877 | |
| 4878 | switch (Cand->DeductionFailure.Result) { |
| 4879 | case Sema::TDK_Success: |
| 4880 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 4881 | |
| 4882 | case Sema::TDK_Incomplete: { |
| 4883 | NamedDecl *ParamD; |
| 4884 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 4885 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 4886 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
| 4887 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 4888 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 4889 | << ParamD->getDeclName(); |
| 4890 | return; |
| 4891 | } |
| 4892 | |
| 4893 | // TODO: diagnose these individually, then kill off |
| 4894 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
| 4895 | case Sema::TDK_InstantiationDepth: |
| 4896 | case Sema::TDK_Inconsistent: |
| 4897 | case Sema::TDK_InconsistentQuals: |
| 4898 | case Sema::TDK_SubstitutionFailure: |
| 4899 | case Sema::TDK_NonDeducedMismatch: |
| 4900 | case Sema::TDK_TooManyArguments: |
| 4901 | case Sema::TDK_TooFewArguments: |
| 4902 | case Sema::TDK_InvalidExplicitArguments: |
| 4903 | case Sema::TDK_FailedOverloadResolution: |
| 4904 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 4905 | return; |
| 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 4910 | /// already generated a primary error at the call site. |
| 4911 | /// |
| 4912 | /// It really does need to be a single diagnostic with its caret |
| 4913 | /// pointed at the candidate declaration. Yes, this creates some |
| 4914 | /// major challenges of technical writing. Yes, this makes pointing |
| 4915 | /// out problems with specific arguments quite awkward. It's still |
| 4916 | /// better than generating twenty screens of text for every failed |
| 4917 | /// overload. |
| 4918 | /// |
| 4919 | /// It would be great to be able to express per-candidate problems |
| 4920 | /// more richly for those diagnostic clients that cared, but we'd |
| 4921 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4922 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4923 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4924 | FunctionDecl *Fn = Cand->Function; |
| 4925 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4926 | // Note deleted candidates, but only if they're viable. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4927 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4928 | std::string FnDesc; |
| 4929 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4930 | |
| 4931 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4932 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4933 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4936 | // We don't really have anything else to say about viable candidates. |
| 4937 | if (Cand->Viable) { |
| 4938 | S.NoteOverloadCandidate(Fn); |
| 4939 | return; |
| 4940 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4941 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4942 | switch (Cand->FailureKind) { |
| 4943 | case ovl_fail_too_many_arguments: |
| 4944 | case ovl_fail_too_few_arguments: |
| 4945 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4946 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4947 | case ovl_fail_bad_deduction: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4948 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 4949 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4950 | case ovl_fail_trivial_conversion: |
| 4951 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4952 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4953 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4954 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4955 | case ovl_fail_bad_conversion: { |
| 4956 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 4957 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4958 | if (Cand->Conversions[I].isBad()) |
| 4959 | return DiagnoseBadConversion(S, Cand, I); |
| 4960 | |
| 4961 | // FIXME: this currently happens when we're called from SemaInit |
| 4962 | // when user-conversion overload fails. Figure out how to handle |
| 4963 | // those conditions and diagnose them well. |
| 4964 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4965 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4966 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4970 | // Desugar the type of the surrogate down to a function type, |
| 4971 | // retaining as many typedefs as possible while still showing |
| 4972 | // the function type (and, therefore, its parameter types). |
| 4973 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4974 | bool isLValueReference = false; |
| 4975 | bool isRValueReference = false; |
| 4976 | bool isPointer = false; |
| 4977 | if (const LValueReferenceType *FnTypeRef = |
| 4978 | FnType->getAs<LValueReferenceType>()) { |
| 4979 | FnType = FnTypeRef->getPointeeType(); |
| 4980 | isLValueReference = true; |
| 4981 | } else if (const RValueReferenceType *FnTypeRef = |
| 4982 | FnType->getAs<RValueReferenceType>()) { |
| 4983 | FnType = FnTypeRef->getPointeeType(); |
| 4984 | isRValueReference = true; |
| 4985 | } |
| 4986 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4987 | FnType = FnTypePtr->getPointeeType(); |
| 4988 | isPointer = true; |
| 4989 | } |
| 4990 | // Desugar down to a function type. |
| 4991 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4992 | // Reconstruct the pointer/reference as appropriate. |
| 4993 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4994 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4995 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4996 | |
| 4997 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4998 | << FnType; |
| 4999 | } |
| 5000 | |
| 5001 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5002 | const char *Opc, |
| 5003 | SourceLocation OpLoc, |
| 5004 | OverloadCandidate *Cand) { |
| 5005 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5006 | std::string TypeStr("operator"); |
| 5007 | TypeStr += Opc; |
| 5008 | TypeStr += "("; |
| 5009 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5010 | if (Cand->Conversions.size() == 1) { |
| 5011 | TypeStr += ")"; |
| 5012 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5013 | } else { |
| 5014 | TypeStr += ", "; |
| 5015 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5016 | TypeStr += ")"; |
| 5017 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5022 | OverloadCandidate *Cand) { |
| 5023 | unsigned NoOperands = Cand->Conversions.size(); |
| 5024 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5025 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5026 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5027 | if (!ICS.isAmbiguous()) continue; |
| 5028 | |
| 5029 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5030 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5034 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5035 | if (Cand->Function) |
| 5036 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5037 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5038 | return Cand->Surrogate->getLocation(); |
| 5039 | return SourceLocation(); |
| 5040 | } |
| 5041 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5042 | struct CompareOverloadCandidatesForDisplay { |
| 5043 | Sema &S; |
| 5044 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5045 | |
| 5046 | bool operator()(const OverloadCandidate *L, |
| 5047 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5048 | // Fast-path this check. |
| 5049 | if (L == R) return false; |
| 5050 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5051 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5052 | if (L->Viable) { |
| 5053 | if (!R->Viable) return true; |
| 5054 | |
| 5055 | // TODO: introduce a tri-valued comparison for overload |
| 5056 | // candidates. Would be more worthwhile if we had a sort |
| 5057 | // that could exploit it. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5058 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 5059 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5060 | } else if (R->Viable) |
| 5061 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5062 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5063 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5064 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5065 | // Criteria by which we can sort non-viable candidates: |
| 5066 | if (!L->Viable) { |
| 5067 | // 1. Arity mismatches come after other candidates. |
| 5068 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5069 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5070 | return false; |
| 5071 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5072 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5073 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5074 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5075 | // 2. Bad conversions come first and are ordered by the number |
| 5076 | // of bad conversions and quality of good conversions. |
| 5077 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5078 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5079 | return true; |
| 5080 | |
| 5081 | // If there's any ordering between the defined conversions... |
| 5082 | // FIXME: this might not be transitive. |
| 5083 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5084 | |
| 5085 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5086 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5087 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5088 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 5089 | R->Conversions[I])) { |
| 5090 | case ImplicitConversionSequence::Better: |
| 5091 | leftBetter++; |
| 5092 | break; |
| 5093 | |
| 5094 | case ImplicitConversionSequence::Worse: |
| 5095 | leftBetter--; |
| 5096 | break; |
| 5097 | |
| 5098 | case ImplicitConversionSequence::Indistinguishable: |
| 5099 | break; |
| 5100 | } |
| 5101 | } |
| 5102 | if (leftBetter > 0) return true; |
| 5103 | if (leftBetter < 0) return false; |
| 5104 | |
| 5105 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 5106 | return false; |
| 5107 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5108 | // TODO: others? |
| 5109 | } |
| 5110 | |
| 5111 | // Sort everything else by location. |
| 5112 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 5113 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 5114 | |
| 5115 | // Put candidates without locations (e.g. builtins) at the end. |
| 5116 | if (LLoc.isInvalid()) return false; |
| 5117 | if (RLoc.isInvalid()) return true; |
| 5118 | |
| 5119 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5120 | } |
| 5121 | }; |
| 5122 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5123 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 5124 | /// computes up to the first |
| 5125 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 5126 | Expr **Args, unsigned NumArgs) { |
| 5127 | assert(!Cand->Viable); |
| 5128 | |
| 5129 | // Don't do anything on failures other than bad conversion. |
| 5130 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 5131 | |
| 5132 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5133 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5134 | unsigned ConvCount = Cand->Conversions.size(); |
| 5135 | while (true) { |
| 5136 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 5137 | ConvIdx++; |
| 5138 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 5139 | break; |
| 5140 | } |
| 5141 | |
| 5142 | if (ConvIdx == ConvCount) |
| 5143 | return; |
| 5144 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5145 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 5146 | "remaining conversion is initialized?"); |
| 5147 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame^] | 5148 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5149 | // operation somehow. |
| 5150 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5151 | |
| 5152 | const FunctionProtoType* Proto; |
| 5153 | unsigned ArgIdx = ConvIdx; |
| 5154 | |
| 5155 | if (Cand->IsSurrogate) { |
| 5156 | QualType ConvType |
| 5157 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 5158 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5159 | ConvType = ConvPtrType->getPointeeType(); |
| 5160 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 5161 | ArgIdx--; |
| 5162 | } else if (Cand->Function) { |
| 5163 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 5164 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 5165 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 5166 | ArgIdx--; |
| 5167 | } else { |
| 5168 | // Builtin binary operator with a bad first conversion. |
| 5169 | assert(ConvCount <= 3); |
| 5170 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 5171 | Cand->Conversions[ConvIdx] |
| 5172 | = S.TryCopyInitialization(Args[ConvIdx], |
| 5173 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame^] | 5174 | SuppressUserConversions, |
| 5175 | /*ForceRValue=*/false, |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5176 | /*InOverloadResolution*/ true); |
| 5177 | return; |
| 5178 | } |
| 5179 | |
| 5180 | // Fill in the rest of the conversions. |
| 5181 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5182 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 5183 | if (ArgIdx < NumArgsInProto) |
| 5184 | Cand->Conversions[ConvIdx] |
| 5185 | = S.TryCopyInitialization(Args[ArgIdx], Proto->getArgType(ArgIdx), |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame^] | 5186 | SuppressUserConversions, |
| 5187 | /*ForceRValue=*/false, |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5188 | /*InOverloadResolution=*/true); |
| 5189 | else |
| 5190 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 5191 | } |
| 5192 | } |
| 5193 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5194 | } // end anonymous namespace |
| 5195 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5196 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 5197 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5198 | /// set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5199 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5200 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5201 | OverloadCandidateDisplayKind OCD, |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5202 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5203 | const char *Opc, |
Fariborz Jahanian | 29f9d39 | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 5204 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5205 | // Sort the candidates by viability and position. Sorting directly would |
| 5206 | // be prohibitive, so we make a set of pointers and sort those. |
| 5207 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 5208 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 5209 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5210 | LastCand = CandidateSet.end(); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5211 | Cand != LastCand; ++Cand) { |
| 5212 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5213 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5214 | else if (OCD == OCD_AllCandidates) { |
| 5215 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 5216 | Cands.push_back(Cand); |
| 5217 | } |
| 5218 | } |
| 5219 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5220 | std::sort(Cands.begin(), Cands.end(), |
| 5221 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5222 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5223 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5224 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5225 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 5226 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 5227 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 5228 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5229 | if (Cand->Function) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5230 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5231 | else if (Cand->IsSurrogate) |
| 5232 | NoteSurrogateCandidate(*this, Cand); |
| 5233 | |
| 5234 | // This a builtin candidate. We do not, in general, want to list |
| 5235 | // every possible builtin candidate. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5236 | else if (Cand->Viable) { |
| 5237 | // Generally we only see ambiguities including viable builtin |
| 5238 | // operators if overload resolution got screwed up by an |
| 5239 | // ambiguous user-defined conversion. |
| 5240 | // |
| 5241 | // FIXME: It's quite possible for different conversions to see |
| 5242 | // different ambiguities, though. |
| 5243 | if (!ReportedAmbiguousConversions) { |
| 5244 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 5245 | ReportedAmbiguousConversions = true; |
| 5246 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5247 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5248 | // If this is a viable builtin, print it. |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5249 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5250 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5251 | } |
| 5252 | } |
| 5253 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5254 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5255 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5256 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5257 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5258 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5261 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 5262 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 5263 | /// expression with overloaded function type and @p ToType is the type |
| 5264 | /// we're trying to resolve to. For example: |
| 5265 | /// |
| 5266 | /// @code |
| 5267 | /// int f(double); |
| 5268 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5269 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5270 | /// int (*pfd)(double) = f; // selects f(double) |
| 5271 | /// @endcode |
| 5272 | /// |
| 5273 | /// This routine returns the resulting FunctionDecl if it could be |
| 5274 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 5275 | /// routine will emit diagnostics if there is an error. |
| 5276 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5277 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5278 | bool Complain, |
| 5279 | DeclAccessPair &FoundResult) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5280 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5281 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5282 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5283 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5284 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 5285 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5286 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5287 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5288 | FunctionType = MemTypePtr->getPointeeType(); |
| 5289 | IsMember = true; |
| 5290 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5291 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5292 | // C++ [over.over]p1: |
| 5293 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5294 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5295 | // C++ [over.over]p1: |
| 5296 | // [...] The overloaded function name can be preceded by the & |
| 5297 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5298 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5299 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 5300 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 5301 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 5302 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5303 | } |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5304 | |
| 5305 | // We expect a pointer or reference to function, or a function pointer. |
| 5306 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 5307 | if (!FunctionType->isFunctionType()) { |
| 5308 | if (Complain) |
| 5309 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 5310 | << OvlExpr->getName() << ToType; |
| 5311 | |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
| 5315 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5316 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5317 | // Look through all of the overloaded functions, searching for one |
| 5318 | // whose type matches exactly. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5319 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5320 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 5321 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5322 | bool FoundNonTemplateFunction = false; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5323 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5324 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5325 | // Look through any using declarations to find the underlying function. |
| 5326 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5327 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5328 | // C++ [over.over]p3: |
| 5329 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 5330 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5331 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5332 | // type "pointer-to-member-function." |
| 5333 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5334 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5336 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5337 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5338 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5340 | // static when converting to member pointer. |
| 5341 | if (Method->isStatic() == IsMember) |
| 5342 | continue; |
| 5343 | } else if (IsMember) |
| 5344 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5345 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5346 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5347 | // If the name is a function template, template argument deduction is |
| 5348 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5349 | // resulting template argument list is used to generate a single |
| 5350 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5351 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5352 | // 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] | 5353 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5354 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5355 | if (TemplateDeductionResult Result |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5356 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5357 | FunctionType, Specialization, Info)) { |
| 5358 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5359 | (void)Result; |
| 5360 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5361 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5362 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5364 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5365 | Matches.push_back(std::make_pair(I.getPair(), |
| 5366 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5367 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5368 | |
| 5369 | continue; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5371 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5372 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5373 | // Skip non-static functions when converting to pointer, and static |
| 5374 | // when converting to member pointer. |
| 5375 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5376 | continue; |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5377 | |
| 5378 | // If we have explicit template arguments, skip non-templates. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5379 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5380 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5381 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5382 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5383 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5384 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 5385 | QualType ResultTy; |
| 5386 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5387 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5388 | ResultTy)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5389 | Matches.push_back(std::make_pair(I.getPair(), |
| 5390 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5391 | FoundNonTemplateFunction = true; |
| 5392 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5393 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5396 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5397 | if (Matches.empty()) { |
| 5398 | if (Complain) { |
| 5399 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 5400 | << OvlExpr->getName() << FunctionType; |
| 5401 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5402 | E = OvlExpr->decls_end(); |
| 5403 | I != E; ++I) |
| 5404 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 5405 | NoteOverloadCandidate(F); |
| 5406 | } |
| 5407 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5408 | return 0; |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5409 | } else if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5410 | FunctionDecl *Result = Matches[0].second; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5411 | FoundResult = Matches[0].first; |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5412 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5413 | if (Complain) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5414 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5415 | return Result; |
| 5416 | } |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5417 | |
| 5418 | // C++ [over.over]p4: |
| 5419 | // If more than one function is selected, [...] |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5420 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5421 | // [...] and any given function template specialization F1 is |
| 5422 | // eliminated if the set contains a second function template |
| 5423 | // specialization whose function template is more specialized |
| 5424 | // than the function template of F1 according to the partial |
| 5425 | // ordering rules of 14.5.5.2. |
| 5426 | |
| 5427 | // The algorithm specified above is quadratic. We instead use a |
| 5428 | // two-pass algorithm (similar to the one used to identify the |
| 5429 | // best viable function in an overload set) that identifies the |
| 5430 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5431 | |
| 5432 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 5433 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5434 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5435 | |
| 5436 | UnresolvedSetIterator Result = |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5437 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5438 | TPOC_Other, From->getLocStart(), |
| 5439 | PDiag(), |
| 5440 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5441 | << Matches[0].second->getDeclName(), |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5442 | PDiag(diag::note_ovl_candidate) |
| 5443 | << (unsigned) oc_function_template); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5444 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5445 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5446 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
| 5447 | if (Complain) |
| 5448 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5449 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5450 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5451 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5452 | // [...] any function template specializations in the set are |
| 5453 | // eliminated if the set also contains a non-template function, [...] |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5454 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5455 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5456 | ++I; |
| 5457 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5458 | Matches[I] = Matches[--N]; |
| 5459 | Matches.set_size(N); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5460 | } |
| 5461 | } |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5462 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5463 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5464 | // selected function. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5465 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5466 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5467 | FoundResult = Matches[0].first; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5468 | if (Complain) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5469 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
| 5470 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5471 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5472 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5473 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5474 | // returns (even if we issue the diagnostics here). |
| 5475 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5476 | << Matches[0].second->getDeclName(); |
| 5477 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5478 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5479 | return 0; |
| 5480 | } |
| 5481 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5482 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5483 | /// resolve that overloaded function expression down to a single function. |
| 5484 | /// |
| 5485 | /// This routine can only resolve template-ids that refer to a single function |
| 5486 | /// template, where that template-id refers to a single template whose template |
| 5487 | /// arguments are either provided by the template-id or have defaults, |
| 5488 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5489 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5490 | // C++ [over.over]p1: |
| 5491 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5492 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5493 | // C++ [over.over]p1: |
| 5494 | // [...] The overloaded function name can be preceded by the & |
| 5495 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5496 | |
| 5497 | if (From->getType() != Context.OverloadTy) |
| 5498 | return 0; |
| 5499 | |
| 5500 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5501 | |
| 5502 | // If we didn't actually find any template-ids, we're done. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5503 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5504 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5505 | |
| 5506 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5507 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5508 | |
| 5509 | // Look through all of the overloaded functions, searching for one |
| 5510 | // whose type matches exactly. |
| 5511 | FunctionDecl *Matched = 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5512 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5513 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5514 | // C++0x [temp.arg.explicit]p3: |
| 5515 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5516 | // where deduction is not done, if a template argument list is |
| 5517 | // specified and it, along with any default template arguments, |
| 5518 | // identifies a single function template specialization, then the |
| 5519 | // template-id is an lvalue for the function template specialization. |
| 5520 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5521 | |
| 5522 | // C++ [over.over]p2: |
| 5523 | // If the name is a function template, template argument deduction is |
| 5524 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5525 | // resulting template argument list is used to generate a single |
| 5526 | // function template specialization, which is added to the set of |
| 5527 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5528 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5529 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5530 | if (TemplateDeductionResult Result |
| 5531 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5532 | Specialization, Info)) { |
| 5533 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5534 | (void)Result; |
| 5535 | continue; |
| 5536 | } |
| 5537 | |
| 5538 | // Multiple matches; we can't resolve to a single declaration. |
| 5539 | if (Matched) |
| 5540 | return 0; |
| 5541 | |
| 5542 | Matched = Specialization; |
| 5543 | } |
| 5544 | |
| 5545 | return Matched; |
| 5546 | } |
| 5547 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5548 | /// \brief Add a single candidate to the overload set. |
| 5549 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5550 | DeclAccessPair FoundDecl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5551 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5552 | Expr **Args, unsigned NumArgs, |
| 5553 | OverloadCandidateSet &CandidateSet, |
| 5554 | bool PartialOverloading) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5555 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5556 | if (isa<UsingShadowDecl>(Callee)) |
| 5557 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5558 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5559 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5560 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5561 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5562 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5563 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | if (FunctionTemplateDecl *FuncTemplate |
| 5567 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5568 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 5569 | ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5570 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5571 | return; |
| 5572 | } |
| 5573 | |
| 5574 | assert(false && "unhandled case in overloaded call candidate"); |
| 5575 | |
| 5576 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5580 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5581 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5582 | Expr **Args, unsigned NumArgs, |
| 5583 | OverloadCandidateSet &CandidateSet, |
| 5584 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5585 | |
| 5586 | #ifndef NDEBUG |
| 5587 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5588 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5589 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5590 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5591 | // and let Y be the lookup set produced by argument dependent |
| 5592 | // lookup (defined as follows). If X contains |
| 5593 | // |
| 5594 | // -- a declaration of a class member, or |
| 5595 | // |
| 5596 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5597 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5598 | // |
| 5599 | // -- a declaration that is neither a function or a function |
| 5600 | // template |
| 5601 | // |
| 5602 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5603 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5604 | if (ULE->requiresADL()) { |
| 5605 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5606 | E = ULE->decls_end(); I != E; ++I) { |
| 5607 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5608 | assert(isa<UsingShadowDecl>(*I) || |
| 5609 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5610 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5611 | } |
| 5612 | } |
| 5613 | #endif |
| 5614 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5615 | // It would be nice to avoid this copy. |
| 5616 | TemplateArgumentListInfo TABuffer; |
| 5617 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5618 | if (ULE->hasExplicitTemplateArgs()) { |
| 5619 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5620 | ExplicitTemplateArgs = &TABuffer; |
| 5621 | } |
| 5622 | |
| 5623 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5624 | E = ULE->decls_end(); I != E; ++I) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5625 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5626 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5627 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5628 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5629 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5630 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 5631 | Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5632 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5633 | CandidateSet, |
| 5634 | PartialOverloading); |
| 5635 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5636 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5637 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5638 | Expr **Args, unsigned NumArgs) { |
| 5639 | Fn->Destroy(SemaRef.Context); |
| 5640 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5641 | Args[Arg]->Destroy(SemaRef.Context); |
| 5642 | return SemaRef.ExprError(); |
| 5643 | } |
| 5644 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5645 | /// Attempts to recover from a call where no functions were found. |
| 5646 | /// |
| 5647 | /// Returns true if new candidates were found. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5648 | static Sema::OwningExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5649 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5650 | UnresolvedLookupExpr *ULE, |
| 5651 | SourceLocation LParenLoc, |
| 5652 | Expr **Args, unsigned NumArgs, |
| 5653 | SourceLocation *CommaLocs, |
| 5654 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5655 | |
| 5656 | CXXScopeSpec SS; |
| 5657 | if (ULE->getQualifier()) { |
| 5658 | SS.setScopeRep(ULE->getQualifier()); |
| 5659 | SS.setRange(ULE->getQualifierRange()); |
| 5660 | } |
| 5661 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5662 | TemplateArgumentListInfo TABuffer; |
| 5663 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5664 | if (ULE->hasExplicitTemplateArgs()) { |
| 5665 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5666 | ExplicitTemplateArgs = &TABuffer; |
| 5667 | } |
| 5668 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5669 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5670 | Sema::LookupOrdinaryName); |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5671 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R)) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5672 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5673 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5674 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5675 | |
| 5676 | // Build an implicit member call if appropriate. Just drop the |
| 5677 | // casts and such from the call, we don't really care. |
| 5678 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5679 | if ((*R.begin())->isCXXClassMember()) |
| 5680 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5681 | else if (ExplicitTemplateArgs) |
| 5682 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5683 | else |
| 5684 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5685 | |
| 5686 | if (NewFn.isInvalid()) |
| 5687 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5688 | |
| 5689 | Fn->Destroy(SemaRef.Context); |
| 5690 | |
| 5691 | // This shouldn't cause an infinite loop because we're giving it |
| 5692 | // an expression with non-empty lookup results, which should never |
| 5693 | // end up here. |
| 5694 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5695 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5696 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5697 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5698 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5699 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5700 | /// (which eventually refers to the declaration Func) and the call |
| 5701 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5702 | /// to a specific function. If overload resolution succeeds, returns |
| 5703 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 5704 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5705 | /// arguments and Fn, and returns NULL. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5706 | Sema::OwningExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5707 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5708 | SourceLocation LParenLoc, |
| 5709 | Expr **Args, unsigned NumArgs, |
| 5710 | SourceLocation *CommaLocs, |
| 5711 | SourceLocation RParenLoc) { |
| 5712 | #ifndef NDEBUG |
| 5713 | if (ULE->requiresADL()) { |
| 5714 | // To do ADL, we must have found an unqualified name. |
| 5715 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5716 | |
| 5717 | // We don't perform ADL for implicit declarations of builtins. |
| 5718 | // Verify that this was correctly set up. |
| 5719 | FunctionDecl *F; |
| 5720 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5721 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5722 | F->getBuiltinID() && F->isImplicit()) |
| 5723 | assert(0 && "performing ADL for builtin"); |
| 5724 | |
| 5725 | // We don't perform ADL in C. |
| 5726 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5727 | } |
| 5728 | #endif |
| 5729 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5730 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5731 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5732 | // Add the functions denoted by the callee to the set of candidate |
| 5733 | // functions, including those from argument-dependent lookup. |
| 5734 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5735 | |
| 5736 | // If we found nothing, try to recover. |
| 5737 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5738 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5739 | if (CandidateSet.empty()) |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5740 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5741 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5742 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5743 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5744 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5745 | case OR_Success: { |
| 5746 | FunctionDecl *FDecl = Best->Function; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5747 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5748 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5749 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5750 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5751 | |
| 5752 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5753 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5754 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5755 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5756 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5757 | break; |
| 5758 | |
| 5759 | case OR_Ambiguous: |
| 5760 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5761 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5762 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5763 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5764 | |
| 5765 | case OR_Deleted: |
| 5766 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5767 | << Best->Function->isDeleted() |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5768 | << ULE->getName() |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5769 | << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5770 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5771 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5775 | // return NULL. |
| 5776 | Fn->Destroy(Context); |
| 5777 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5778 | Args[Arg]->Destroy(Context); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5779 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5782 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5783 | return Functions.size() > 1 || |
| 5784 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5785 | } |
| 5786 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5787 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5788 | /// operator. |
| 5789 | /// |
| 5790 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5791 | /// |
| 5792 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5793 | /// operator. |
| 5794 | /// |
| 5795 | /// \param Functions The set of non-member functions that will be |
| 5796 | /// considered by overload resolution. The caller needs to build this |
| 5797 | /// set based on the context using, e.g., |
| 5798 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5799 | /// set should not contain any member functions; those will be added |
| 5800 | /// by CreateOverloadedUnaryOp(). |
| 5801 | /// |
| 5802 | /// \param input The input argument. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5803 | Sema::OwningExprResult |
| 5804 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 5805 | const UnresolvedSetImpl &Fns, |
| 5806 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5807 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5808 | Expr *Input = (Expr *)input.get(); |
| 5809 | |
| 5810 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5811 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5812 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5813 | |
| 5814 | Expr *Args[2] = { Input, 0 }; |
| 5815 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5816 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5817 | // For post-increment and post-decrement, add the implicit '0' as |
| 5818 | // the second argument, so that we know this is a post-increment or |
| 5819 | // post-decrement. |
| 5820 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5821 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5822 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5823 | SourceLocation()); |
| 5824 | NumArgs = 2; |
| 5825 | } |
| 5826 | |
| 5827 | if (Input->isTypeDependent()) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5828 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5829 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5830 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5831 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5832 | /*ADL*/ true, IsOverloaded(Fns)); |
| 5833 | Fn->addDecls(Fns.begin(), Fns.end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5834 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5835 | input.release(); |
| 5836 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5837 | &Args[0], NumArgs, |
| 5838 | Context.DependentTy, |
| 5839 | OpLoc)); |
| 5840 | } |
| 5841 | |
| 5842 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5843 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5844 | |
| 5845 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5846 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5847 | |
| 5848 | // Add operator candidates that are member functions. |
| 5849 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5850 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5851 | // Add candidates from ADL. |
| 5852 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | 6ec89d4 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 5853 | Args, NumArgs, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5854 | /*ExplicitTemplateArgs*/ 0, |
| 5855 | CandidateSet); |
| 5856 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5857 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5858 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5859 | |
| 5860 | // Perform overload resolution. |
| 5861 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5862 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5863 | case OR_Success: { |
| 5864 | // We found a built-in operator or an overloaded operator. |
| 5865 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5866 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5867 | if (FnDecl) { |
| 5868 | // We matched an overloaded operator. Build a call to that |
| 5869 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5870 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5871 | // Convert the arguments. |
| 5872 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5873 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5874 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5875 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 5876 | Best->FoundDecl, Method)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5877 | return ExprError(); |
| 5878 | } else { |
| 5879 | // Convert the arguments. |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5880 | OwningExprResult InputInit |
| 5881 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5882 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5883 | SourceLocation(), |
| 5884 | move(input)); |
| 5885 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5886 | return ExprError(); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5887 | |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5888 | input = move(InputInit); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5889 | Input = (Expr *)input.get(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | // Determine the result type |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5893 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5894 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5895 | // Build the actual expression node. |
| 5896 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5897 | SourceLocation()); |
| 5898 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5899 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5900 | input.release(); |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5901 | Args[0] = Input; |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5902 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5903 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5904 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5905 | |
| 5906 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5907 | FnDecl)) |
| 5908 | return ExprError(); |
| 5909 | |
| 5910 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5911 | } else { |
| 5912 | // We matched a built-in operator. Convert the arguments, then |
| 5913 | // break out so that we will build the appropriate built-in |
| 5914 | // operator node. |
| 5915 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5916 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5917 | return ExprError(); |
| 5918 | |
| 5919 | break; |
| 5920 | } |
| 5921 | } |
| 5922 | |
| 5923 | case OR_No_Viable_Function: |
| 5924 | // No viable function; fall through to handling this as a |
| 5925 | // built-in operator, which will produce an error message for us. |
| 5926 | break; |
| 5927 | |
| 5928 | case OR_Ambiguous: |
| 5929 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5930 | << UnaryOperator::getOpcodeStr(Opc) |
| 5931 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5932 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5933 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5934 | return ExprError(); |
| 5935 | |
| 5936 | case OR_Deleted: |
| 5937 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5938 | << Best->Function->isDeleted() |
| 5939 | << UnaryOperator::getOpcodeStr(Opc) |
| 5940 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5941 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5942 | return ExprError(); |
| 5943 | } |
| 5944 | |
| 5945 | // Either we found no viable overloaded operator or we matched a |
| 5946 | // built-in operator. In either case, fall through to trying to |
| 5947 | // build a built-in operation. |
| 5948 | input.release(); |
| 5949 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5950 | } |
| 5951 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5952 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5953 | /// operator. |
| 5954 | /// |
| 5955 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5956 | /// |
| 5957 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5958 | /// operator. |
| 5959 | /// |
| 5960 | /// \param Functions The set of non-member functions that will be |
| 5961 | /// considered by overload resolution. The caller needs to build this |
| 5962 | /// set based on the context using, e.g., |
| 5963 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5964 | /// set should not contain any member functions; those will be added |
| 5965 | /// by CreateOverloadedBinOp(). |
| 5966 | /// |
| 5967 | /// \param LHS Left-hand argument. |
| 5968 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5969 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5970 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5971 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5972 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5973 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5974 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5975 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5976 | |
| 5977 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5978 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5979 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5980 | |
| 5981 | // If either side is type-dependent, create an appropriate dependent |
| 5982 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5983 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5984 | if (Fns.empty()) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5985 | // If there are no functions to store, just build a dependent |
| 5986 | // BinaryOperator or CompoundAssignment. |
| 5987 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5988 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5989 | Context.DependentTy, OpLoc)); |
| 5990 | |
| 5991 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5992 | Context.DependentTy, |
| 5993 | Context.DependentTy, |
| 5994 | Context.DependentTy, |
| 5995 | OpLoc)); |
| 5996 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5997 | |
| 5998 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5999 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6000 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6001 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6002 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6003 | /*ADL*/ true, IsOverloaded(Fns)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6005 | Fn->addDecls(Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6006 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6007 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6008 | Context.DependentTy, |
| 6009 | OpLoc)); |
| 6010 | } |
| 6011 | |
| 6012 | // If this is the .* operator, which is not overloadable, just |
| 6013 | // create a built-in binary operator. |
| 6014 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6015 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6016 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6017 | // If this is the assignment operator, we only perform overload resolution |
| 6018 | // if the left-hand side is a class or enumeration type. This is actually |
| 6019 | // a hack. The standard requires that we do overload resolution between the |
| 6020 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6021 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6022 | // Note that we go the traditional code path for compound assignment forms. |
| 6023 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6024 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6025 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6026 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6027 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6028 | |
| 6029 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6030 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6031 | |
| 6032 | // Add operator candidates that are member functions. |
| 6033 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6034 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6035 | // Add candidates from ADL. |
| 6036 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6037 | Args, 2, |
| 6038 | /*ExplicitTemplateArgs*/ 0, |
| 6039 | CandidateSet); |
| 6040 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6041 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6042 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6043 | |
| 6044 | // Perform overload resolution. |
| 6045 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6046 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6047 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6048 | // We found a built-in operator or an overloaded operator. |
| 6049 | FunctionDecl *FnDecl = Best->Function; |
| 6050 | |
| 6051 | if (FnDecl) { |
| 6052 | // We matched an overloaded operator. Build a call to that |
| 6053 | // operator. |
| 6054 | |
| 6055 | // Convert the arguments. |
| 6056 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6057 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6058 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6059 | |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6060 | OwningExprResult Arg1 |
| 6061 | = PerformCopyInitialization( |
| 6062 | InitializedEntity::InitializeParameter( |
| 6063 | FnDecl->getParamDecl(0)), |
| 6064 | SourceLocation(), |
| 6065 | Owned(Args[1])); |
| 6066 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6067 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6068 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6069 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6070 | Best->FoundDecl, Method)) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6071 | return ExprError(); |
| 6072 | |
| 6073 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6074 | } else { |
| 6075 | // Convert the arguments. |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6076 | OwningExprResult Arg0 |
| 6077 | = PerformCopyInitialization( |
| 6078 | InitializedEntity::InitializeParameter( |
| 6079 | FnDecl->getParamDecl(0)), |
| 6080 | SourceLocation(), |
| 6081 | Owned(Args[0])); |
| 6082 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6083 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6084 | |
| 6085 | OwningExprResult Arg1 |
| 6086 | = PerformCopyInitialization( |
| 6087 | InitializedEntity::InitializeParameter( |
| 6088 | FnDecl->getParamDecl(1)), |
| 6089 | SourceLocation(), |
| 6090 | Owned(Args[1])); |
| 6091 | if (Arg1.isInvalid()) |
| 6092 | return ExprError(); |
| 6093 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 6094 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | // Determine the result type |
| 6098 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6099 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6100 | ResultTy = ResultTy.getNonReferenceType(); |
| 6101 | |
| 6102 | // Build the actual expression node. |
| 6103 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 6104 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6105 | UsualUnaryConversions(FnExpr); |
| 6106 | |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6107 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6108 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 6109 | Args, 2, ResultTy, |
| 6110 | OpLoc)); |
| 6111 | |
| 6112 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6113 | FnDecl)) |
| 6114 | return ExprError(); |
| 6115 | |
| 6116 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6117 | } else { |
| 6118 | // We matched a built-in operator. Convert the arguments, then |
| 6119 | // break out so that we will build the appropriate built-in |
| 6120 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6121 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6122 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6123 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6124 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6125 | return ExprError(); |
| 6126 | |
| 6127 | break; |
| 6128 | } |
| 6129 | } |
| 6130 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6131 | case OR_No_Viable_Function: { |
| 6132 | // C++ [over.match.oper]p9: |
| 6133 | // If the operator is the operator , [...] and there are no |
| 6134 | // viable functions, then the operator is assumed to be the |
| 6135 | // built-in operator and interpreted according to clause 5. |
| 6136 | if (Opc == BinaryOperator::Comma) |
| 6137 | break; |
| 6138 | |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6139 | // For class as left operand for assignment or compound assigment operator |
| 6140 | // do not fall through to handling in built-in, but report that no overloaded |
| 6141 | // assignment operator found |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6142 | OwningExprResult Result = ExprError(); |
| 6143 | if (Args[0]->getType()->isRecordType() && |
| 6144 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6145 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6146 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6147 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6148 | } else { |
| 6149 | // No viable function; try to create a built-in operation, which will |
| 6150 | // produce an error. Then, show the non-viable candidates. |
| 6151 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6152 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6153 | assert(Result.isInvalid() && |
| 6154 | "C++ binary operator overloading is missing candidates!"); |
| 6155 | if (Result.isInvalid()) |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6156 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6157 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6158 | return move(Result); |
| 6159 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6160 | |
| 6161 | case OR_Ambiguous: |
| 6162 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6163 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6164 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6165 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6166 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6167 | return ExprError(); |
| 6168 | |
| 6169 | case OR_Deleted: |
| 6170 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6171 | << Best->Function->isDeleted() |
| 6172 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6173 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6174 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6175 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6176 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6177 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6178 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6179 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6182 | Action::OwningExprResult |
| 6183 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 6184 | SourceLocation RLoc, |
| 6185 | ExprArg Base, ExprArg Idx) { |
| 6186 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 6187 | static_cast<Expr*>(Idx.get()) }; |
| 6188 | DeclarationName OpName = |
| 6189 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 6190 | |
| 6191 | // If either side is type-dependent, create an appropriate dependent |
| 6192 | // expression. |
| 6193 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 6194 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6195 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6196 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6197 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6198 | 0, SourceRange(), OpName, LLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6199 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6200 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6201 | |
| 6202 | Base.release(); |
| 6203 | Idx.release(); |
| 6204 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 6205 | Args, 2, |
| 6206 | Context.DependentTy, |
| 6207 | RLoc)); |
| 6208 | } |
| 6209 | |
| 6210 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6211 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6212 | |
| 6213 | // Subscript can only be overloaded as a member function. |
| 6214 | |
| 6215 | // Add operator candidates that are member functions. |
| 6216 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6217 | |
| 6218 | // Add builtin operator candidates. |
| 6219 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6220 | |
| 6221 | // Perform overload resolution. |
| 6222 | OverloadCandidateSet::iterator Best; |
| 6223 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 6224 | case OR_Success: { |
| 6225 | // We found a built-in operator or an overloaded operator. |
| 6226 | FunctionDecl *FnDecl = Best->Function; |
| 6227 | |
| 6228 | if (FnDecl) { |
| 6229 | // We matched an overloaded operator. Build a call to that |
| 6230 | // operator. |
| 6231 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6232 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6233 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6234 | // Convert the arguments. |
| 6235 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6236 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6237 | Best->FoundDecl, Method)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6238 | return ExprError(); |
| 6239 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 6240 | // Convert the arguments. |
| 6241 | OwningExprResult InputInit |
| 6242 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6243 | FnDecl->getParamDecl(0)), |
| 6244 | SourceLocation(), |
| 6245 | Owned(Args[1])); |
| 6246 | if (InputInit.isInvalid()) |
| 6247 | return ExprError(); |
| 6248 | |
| 6249 | Args[1] = InputInit.takeAs<Expr>(); |
| 6250 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6251 | // Determine the result type |
| 6252 | QualType ResultTy |
| 6253 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 6254 | ResultTy = ResultTy.getNonReferenceType(); |
| 6255 | |
| 6256 | // Build the actual expression node. |
| 6257 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6258 | LLoc); |
| 6259 | UsualUnaryConversions(FnExpr); |
| 6260 | |
| 6261 | Base.release(); |
| 6262 | Idx.release(); |
| 6263 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6264 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 6265 | FnExpr, Args, 2, |
| 6266 | ResultTy, RLoc)); |
| 6267 | |
| 6268 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 6269 | FnDecl)) |
| 6270 | return ExprError(); |
| 6271 | |
| 6272 | return MaybeBindToTemporary(TheCall.release()); |
| 6273 | } else { |
| 6274 | // We matched a built-in operator. Convert the arguments, then |
| 6275 | // break out so that we will build the appropriate built-in |
| 6276 | // operator node. |
| 6277 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6278 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6279 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6280 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6281 | return ExprError(); |
| 6282 | |
| 6283 | break; |
| 6284 | } |
| 6285 | } |
| 6286 | |
| 6287 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6288 | if (CandidateSet.empty()) |
| 6289 | Diag(LLoc, diag::err_ovl_no_oper) |
| 6290 | << Args[0]->getType() << /*subscript*/ 0 |
| 6291 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 6292 | else |
| 6293 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 6294 | << Args[0]->getType() |
| 6295 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6296 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6297 | "[]", LLoc); |
| 6298 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | case OR_Ambiguous: |
| 6302 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 6303 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6304 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6305 | "[]", LLoc); |
| 6306 | return ExprError(); |
| 6307 | |
| 6308 | case OR_Deleted: |
| 6309 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 6310 | << Best->Function->isDeleted() << "[]" |
| 6311 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6312 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6313 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6314 | return ExprError(); |
| 6315 | } |
| 6316 | |
| 6317 | // We matched a built-in operator; build it. |
| 6318 | Base.release(); |
| 6319 | Idx.release(); |
| 6320 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 6321 | Owned(Args[1]), RLoc); |
| 6322 | } |
| 6323 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6324 | /// BuildCallToMemberFunction - Build a call to a member |
| 6325 | /// function. MemExpr is the expression that refers to the member |
| 6326 | /// function (and includes the object parameter), Args/NumArgs are the |
| 6327 | /// arguments to the function call (not including the object |
| 6328 | /// parameter). The caller needs to validate that the member |
| 6329 | /// expression refers to a member function or an overloaded member |
| 6330 | /// function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6331 | Sema::OwningExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6332 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 6333 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6334 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 6335 | SourceLocation RParenLoc) { |
| 6336 | // Dig out the member expression. This holds both the object |
| 6337 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6338 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6339 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6340 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6341 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 6342 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6343 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6344 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6345 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6346 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6347 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6348 | Qualifier = MemExpr->getQualifier(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6349 | } else { |
| 6350 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6351 | Qualifier = UnresExpr->getQualifier(); |
| 6352 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6353 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6355 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6356 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6357 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6358 | // FIXME: avoid copy. |
| 6359 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6360 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6361 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6362 | TemplateArgs = &TemplateArgsBuffer; |
| 6363 | } |
| 6364 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6365 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6366 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6367 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6368 | NamedDecl *Func = *I; |
| 6369 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6370 | if (isa<UsingShadowDecl>(Func)) |
| 6371 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6372 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6373 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6374 | // If explicit template arguments were provided, we can't call a |
| 6375 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6376 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6377 | continue; |
| 6378 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6379 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6380 | Args, NumArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6381 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6382 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6383 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6384 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6385 | ObjectType, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6386 | CandidateSet, |
| 6387 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6388 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6389 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6390 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6391 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6392 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6393 | OverloadCandidateSet::iterator Best; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6394 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6395 | case OR_Success: |
| 6396 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6397 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6398 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6399 | break; |
| 6400 | |
| 6401 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6402 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6403 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6404 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6405 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6406 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6407 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6408 | |
| 6409 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6410 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6411 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6412 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6413 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6414 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6415 | |
| 6416 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6417 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6418 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6419 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6420 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6421 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6422 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6423 | } |
| 6424 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6425 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6426 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6427 | // If overload resolution picked a static member, build a |
| 6428 | // non-member call based on that function. |
| 6429 | if (Method->isStatic()) { |
| 6430 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6431 | Args, NumArgs, RParenLoc); |
| 6432 | } |
| 6433 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6434 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6438 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6439 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6440 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6441 | Method->getResultType().getNonReferenceType(), |
| 6442 | RParenLoc)); |
| 6443 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6444 | // Check for a valid return type. |
| 6445 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6446 | TheCall.get(), Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6447 | return ExprError(); |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6448 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6449 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6450 | // We only need to do this if there was actually an overload; otherwise |
| 6451 | // it was done at lookup. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6452 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6453 | if (!Method->isStatic() && |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6454 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 6455 | FoundDecl, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6456 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6457 | MemExpr->setBase(ObjectArg); |
| 6458 | |
| 6459 | // Convert the rest of the arguments |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6460 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6461 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6462 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6463 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6464 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6465 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6466 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 6467 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6468 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6471 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6472 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6473 | /// overloaded function call operator (@c operator()) or performing a |
| 6474 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6475 | Sema::ExprResult |
| 6476 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 6477 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6478 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6479 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6480 | SourceLocation RParenLoc) { |
| 6481 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6482 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6483 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6484 | // C++ [over.call.object]p1: |
| 6485 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6486 | // 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] | 6487 | // candidate functions includes at least the function call |
| 6488 | // operators of T. The function call operators of T are obtained by |
| 6489 | // ordinary lookup of the name operator() in the context of |
| 6490 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6491 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6492 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6493 | |
| 6494 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6495 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6496 | << Object->getSourceRange())) |
| 6497 | return true; |
| 6498 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6499 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6500 | LookupQualifiedName(R, Record->getDecl()); |
| 6501 | R.suppressDiagnostics(); |
| 6502 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6503 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6504 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6505 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6506 | Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6507 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6508 | } |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6509 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6510 | // C++ [over.call.object]p2: |
| 6511 | // In addition, for each conversion function declared in T of the |
| 6512 | // form |
| 6513 | // |
| 6514 | // operator conversion-type-id () cv-qualifier; |
| 6515 | // |
| 6516 | // where cv-qualifier is the same cv-qualification as, or a |
| 6517 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 6518 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6519 | // R", or the type "reference to pointer to function of |
| 6520 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6521 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6522 | // is also considered as a candidate function. Similarly, |
| 6523 | // surrogate call functions are added to the set of candidate |
| 6524 | // functions for each conversion function declared in an |
| 6525 | // accessible base class provided the function is not hidden |
| 6526 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6527 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 6528 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6529 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6530 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6531 | NamedDecl *D = *I; |
| 6532 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6533 | if (isa<UsingShadowDecl>(D)) |
| 6534 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6535 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6536 | // Skip over templated conversion functions; they aren't |
| 6537 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6538 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6539 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6540 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6541 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6543 | // Strip the reference type (if any) and then the pointer type (if |
| 6544 | // any) to get down to what might be a function type. |
| 6545 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6546 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6547 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6548 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6549 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6550 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6551 | Object->getType(), Args, NumArgs, |
| 6552 | CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6553 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6555 | // Perform overload resolution. |
| 6556 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6557 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6558 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6559 | // Overload resolution succeeded; we'll build the appropriate call |
| 6560 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6561 | break; |
| 6562 | |
| 6563 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6564 | if (CandidateSet.empty()) |
| 6565 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6566 | << Object->getType() << /*call*/ 1 |
| 6567 | << Object->getSourceRange(); |
| 6568 | else |
| 6569 | Diag(Object->getSourceRange().getBegin(), |
| 6570 | diag::err_ovl_no_viable_object_call) |
| 6571 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6572 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6573 | break; |
| 6574 | |
| 6575 | case OR_Ambiguous: |
| 6576 | Diag(Object->getSourceRange().getBegin(), |
| 6577 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6578 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6579 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6580 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6581 | |
| 6582 | case OR_Deleted: |
| 6583 | Diag(Object->getSourceRange().getBegin(), |
| 6584 | diag::err_ovl_deleted_object_call) |
| 6585 | << Best->Function->isDeleted() |
| 6586 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6587 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6588 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6589 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6590 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6591 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6592 | // We had an error; delete all of the subexpressions and return |
| 6593 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6594 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6595 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6596 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6597 | return true; |
| 6598 | } |
| 6599 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6600 | if (Best->Function == 0) { |
| 6601 | // Since there is no function declaration, this is one of the |
| 6602 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6603 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6604 | = cast<CXXConversionDecl>( |
| 6605 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6606 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6607 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6608 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6609 | // We selected one of the surrogate functions that converts the |
| 6610 | // object parameter to a function pointer. Perform the conversion |
| 6611 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6612 | |
| 6613 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6614 | // and then call it. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6615 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 6616 | Conv); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6617 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6618 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6619 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
Douglas Gregor | e5e775b | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 6620 | CommaLocs, RParenLoc).result(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6623 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6624 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6625 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6626 | // that calls this method, using Object for the implicit object |
| 6627 | // parameter and passing along the remaining arguments. |
| 6628 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6629 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6630 | |
| 6631 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6632 | unsigned NumArgsToCheck = NumArgs; |
| 6633 | |
| 6634 | // Build the full argument list for the method call (the |
| 6635 | // implicit object parameter is placed at the beginning of the |
| 6636 | // list). |
| 6637 | Expr **MethodArgs; |
| 6638 | if (NumArgs < NumArgsInProto) { |
| 6639 | NumArgsToCheck = NumArgsInProto; |
| 6640 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6641 | } else { |
| 6642 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6643 | } |
| 6644 | MethodArgs[0] = Object; |
| 6645 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6646 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6647 | |
| 6648 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6649 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6650 | UsualUnaryConversions(NewFn); |
| 6651 | |
| 6652 | // Once we've built TheCall, all of the expressions are properly |
| 6653 | // owned. |
| 6654 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6655 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6656 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6657 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6658 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6659 | delete [] MethodArgs; |
| 6660 | |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6661 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6662 | Method)) |
| 6663 | return true; |
| 6664 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6665 | // We may have default arguments. If so, we need to allocate more |
| 6666 | // slots in the call for them. |
| 6667 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6668 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6669 | else if (NumArgs > NumArgsInProto) |
| 6670 | NumArgsToCheck = NumArgsInProto; |
| 6671 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6672 | bool IsError = false; |
| 6673 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6674 | // Initialize the implicit object parameter. |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6675 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6676 | Best->FoundDecl, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6677 | TheCall->setArg(0, Object); |
| 6678 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6679 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6680 | // Check the argument types. |
| 6681 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6682 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6683 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6684 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6685 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6686 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 6687 | |
| 6688 | OwningExprResult InputInit |
| 6689 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6690 | Method->getParamDecl(i)), |
| 6691 | SourceLocation(), Owned(Arg)); |
| 6692 | |
| 6693 | IsError |= InputInit.isInvalid(); |
| 6694 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6695 | } else { |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 6696 | OwningExprResult DefArg |
| 6697 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6698 | if (DefArg.isInvalid()) { |
| 6699 | IsError = true; |
| 6700 | break; |
| 6701 | } |
| 6702 | |
| 6703 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6704 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6705 | |
| 6706 | TheCall->setArg(i + 1, Arg); |
| 6707 | } |
| 6708 | |
| 6709 | // If this is a variadic call, handle args passed through "...". |
| 6710 | if (Proto->isVariadic()) { |
| 6711 | // Promote the arguments (C99 6.5.2.2p7). |
| 6712 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6713 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6714 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6715 | TheCall->setArg(i + 1, Arg); |
| 6716 | } |
| 6717 | } |
| 6718 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6719 | if (IsError) return true; |
| 6720 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6721 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6722 | return true; |
| 6723 | |
Douglas Gregor | e5e775b | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 6724 | return MaybeBindToTemporary(TheCall.release()).result(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6727 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6728 | /// (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] | 6729 | /// @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] | 6730 | Sema::OwningExprResult |
| 6731 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6732 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6733 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6734 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6735 | SourceLocation Loc = Base->getExprLoc(); |
| 6736 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6737 | // C++ [over.ref]p1: |
| 6738 | // |
| 6739 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6740 | // for a class object x of type T if T::operator->() exists and if |
| 6741 | // the operator is selected as the best match function by the |
| 6742 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6743 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6744 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6745 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6746 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6747 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 6748 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6749 | << Base->getSourceRange())) |
| 6750 | return ExprError(); |
| 6751 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6752 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6753 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6754 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6755 | |
| 6756 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6757 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6758 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6759 | /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6760 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6761 | |
| 6762 | // Perform overload resolution. |
| 6763 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6764 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6765 | case OR_Success: |
| 6766 | // Overload resolution succeeded; we'll build the call below. |
| 6767 | break; |
| 6768 | |
| 6769 | case OR_No_Viable_Function: |
| 6770 | if (CandidateSet.empty()) |
| 6771 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6772 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6773 | else |
| 6774 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6775 | << "operator->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6776 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6777 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6778 | |
| 6779 | case OR_Ambiguous: |
| 6780 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6781 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6782 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6783 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6784 | |
| 6785 | case OR_Deleted: |
| 6786 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6787 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6788 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6789 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6790 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6791 | } |
| 6792 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6793 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
| 6794 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6795 | // Convert the object parameter. |
| 6796 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6797 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 6798 | Best->FoundDecl, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6799 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6800 | |
| 6801 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6802 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6803 | |
| 6804 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6805 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6806 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6807 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6808 | |
| 6809 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6810 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6811 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6812 | &Base, 1, ResultTy, OpLoc)); |
| 6813 | |
| 6814 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6815 | Method)) |
| 6816 | return ExprError(); |
| 6817 | return move(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6820 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6821 | /// a C++ overloaded function (possibly with some parentheses and |
| 6822 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6823 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6824 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 6825 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6826 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6827 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6828 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 6829 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6830 | if (SubExpr == PE->getSubExpr()) |
| 6831 | return PE->Retain(); |
| 6832 | |
| 6833 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6834 | } |
| 6835 | |
| 6836 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6837 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 6838 | Found, Fn); |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6839 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6840 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6841 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6842 | if (SubExpr == ICE->getSubExpr()) |
| 6843 | return ICE->Retain(); |
| 6844 | |
| 6845 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6846 | ICE->getCastKind(), |
| 6847 | SubExpr, |
| 6848 | ICE->isLvalueCast()); |
| 6849 | } |
| 6850 | |
| 6851 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6852 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6853 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6854 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6855 | if (Method->isStatic()) { |
| 6856 | // Do nothing: static member functions aren't any different |
| 6857 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6858 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6859 | // Fix the sub expression, which really has to be an |
| 6860 | // UnresolvedLookupExpr holding an overloaded member function |
| 6861 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6862 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 6863 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6864 | if (SubExpr == UnOp->getSubExpr()) |
| 6865 | return UnOp->Retain(); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6866 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6867 | assert(isa<DeclRefExpr>(SubExpr) |
| 6868 | && "fixed to something other than a decl ref"); |
| 6869 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6870 | && "fixed to a member ref with no nested name qualifier"); |
| 6871 | |
| 6872 | // We have taken the address of a pointer to member |
| 6873 | // function. Perform the computation here so that we get the |
| 6874 | // appropriate pointer to member type. |
| 6875 | QualType ClassType |
| 6876 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6877 | QualType MemPtrType |
| 6878 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6879 | |
| 6880 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6881 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6882 | } |
| 6883 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6884 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 6885 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6886 | if (SubExpr == UnOp->getSubExpr()) |
| 6887 | return UnOp->Retain(); |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6888 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6889 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6890 | Context.getPointerType(SubExpr->getType()), |
| 6891 | UnOp->getOperatorLoc()); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6892 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6893 | |
| 6894 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6895 | // FIXME: avoid copy. |
| 6896 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6897 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6898 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6899 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6900 | } |
| 6901 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6902 | return DeclRefExpr::Create(Context, |
| 6903 | ULE->getQualifier(), |
| 6904 | ULE->getQualifierRange(), |
| 6905 | Fn, |
| 6906 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6907 | Fn->getType(), |
| 6908 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6909 | } |
| 6910 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6911 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6912 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6913 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6914 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6915 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6916 | TemplateArgs = &TemplateArgsBuffer; |
| 6917 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6918 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6919 | Expr *Base; |
| 6920 | |
| 6921 | // If we're filling in |
| 6922 | if (MemExpr->isImplicitAccess()) { |
| 6923 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6924 | return DeclRefExpr::Create(Context, |
| 6925 | MemExpr->getQualifier(), |
| 6926 | MemExpr->getQualifierRange(), |
| 6927 | Fn, |
| 6928 | MemExpr->getMemberLoc(), |
| 6929 | Fn->getType(), |
| 6930 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6931 | } else { |
| 6932 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6933 | if (MemExpr->getQualifier()) |
| 6934 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6935 | Base = new (Context) CXXThisExpr(Loc, |
| 6936 | MemExpr->getBaseType(), |
| 6937 | /*isImplicit=*/true); |
| 6938 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6939 | } else |
| 6940 | Base = MemExpr->getBase()->Retain(); |
| 6941 | |
| 6942 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6943 | MemExpr->isArrow(), |
| 6944 | MemExpr->getQualifier(), |
| 6945 | MemExpr->getQualifierRange(), |
| 6946 | Fn, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6947 | Found, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6948 | MemExpr->getMemberLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6949 | TemplateArgs, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6950 | Fn->getType()); |
| 6951 | } |
| 6952 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6953 | assert(false && "Invalid reference to overloaded function"); |
| 6954 | return E->Retain(); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6955 | } |
| 6956 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6957 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 6958 | DeclAccessPair Found, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6959 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6960 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6963 | } // end namespace clang |