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; |
| 121 | Deprecated = false; |
| 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, |
| 464 | ForceRValue, UserCast); |
| 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 | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 552 | SCS.Deprecated = 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 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 571 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 572 | // converted to an rvalue. |
| 573 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 575 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 576 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 577 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 578 | |
| 579 | // If T is a non-class type, the type of the rvalue is the |
| 580 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 581 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 582 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 583 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 584 | } else if (FromType->isArrayType()) { |
| 585 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 586 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 587 | |
| 588 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 589 | // bound of T" can be converted to an rvalue of type "pointer to |
| 590 | // T" (C++ 4.2p1). |
| 591 | FromType = Context.getArrayDecayedType(FromType); |
| 592 | |
| 593 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 594 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 595 | SCS.Deprecated = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 596 | |
| 597 | // For the purpose of ranking in overload resolution |
| 598 | // (13.3.3.1.1), this conversion is considered an |
| 599 | // array-to-pointer conversion followed by a qualification |
| 600 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 601 | SCS.Second = ICK_Identity; |
| 602 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 603 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 604 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 605 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 606 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 607 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 608 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 609 | |
| 610 | // An lvalue of function type T can be converted to an rvalue of |
| 611 | // type "pointer to T." The result is a pointer to the |
| 612 | // function. (C++ 4.3p1). |
| 613 | FromType = Context.getPointerType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | } else if (FunctionDecl *Fn |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 615 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 616 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 617 | SCS.First = ICK_Function_To_Pointer; |
| 618 | |
| 619 | // We were able to resolve the address of the overloaded function, |
| 620 | // so we can convert to the type of that function. |
| 621 | FromType = Fn->getType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 622 | if (ToType->isLValueReferenceType()) |
| 623 | FromType = Context.getLValueReferenceType(FromType); |
| 624 | else if (ToType->isRValueReferenceType()) |
| 625 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 626 | else if (ToType->isMemberPointerType()) { |
| 627 | // Resolve address only succeeds if both sides are member pointers, |
| 628 | // but it doesn't have to be the same class. See DR 247. |
| 629 | // Note that this means that the type of &Derived::fn can be |
| 630 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 631 | // base class, even if it was brought into the derived class via a |
| 632 | // using declaration. The standard isn't clear on this issue at all. |
| 633 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 634 | FromType = Context.getMemberPointerType(FromType, |
| 635 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 636 | } else |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 637 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 638 | } else { |
| 639 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 640 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 641 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 642 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 643 | |
| 644 | // The second conversion can be an integral promotion, floating |
| 645 | // point promotion, integral conversion, floating point conversion, |
| 646 | // floating-integral conversion, pointer conversion, |
| 647 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 648 | // For overloading in C, this can also be a "compatible-type" |
| 649 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 650 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 651 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 652 | // The unqualified versions of the types are the same: there's no |
| 653 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 654 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 655 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 657 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 658 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 659 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 660 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 661 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 662 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 663 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 664 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 665 | SCS.Second = ICK_Complex_Promotion; |
| 666 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 667 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 668 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 669 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 670 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 671 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 672 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 673 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 674 | SCS.Second = ICK_Complex_Conversion; |
| 675 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 676 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 677 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 678 | // Complex-real conversions (C99 6.3.1.7) |
| 679 | SCS.Second = ICK_Complex_Real; |
| 680 | FromType = ToType.getUnqualifiedType(); |
| 681 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 682 | // Floating point conversions (C++ 4.8). |
| 683 | SCS.Second = ICK_Floating_Conversion; |
| 684 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 685 | } else if ((FromType->isFloatingType() && |
| 686 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 687 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 688 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 689 | ToType->isFloatingType())) { |
| 690 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 691 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 692 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 693 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 694 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 695 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 696 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 697 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 698 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 699 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 700 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 701 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 702 | } else if (ToType->isBooleanType() && |
| 703 | (FromType->isArithmeticType() || |
| 704 | FromType->isEnumeralType() || |
Fariborz Jahanian | 8811885 | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 705 | FromType->isAnyPointerType() || |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 706 | FromType->isBlockPointerType() || |
| 707 | FromType->isMemberPointerType() || |
| 708 | FromType->isNullPtrType())) { |
| 709 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 710 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 711 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 713 | Context.typesAreCompatible(ToType, FromType)) { |
| 714 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 715 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 716 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 717 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 718 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 719 | } else { |
| 720 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 721 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 722 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 723 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 724 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 725 | QualType CanonFrom; |
| 726 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 727 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 728 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 729 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 730 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 731 | CanonFrom = Context.getCanonicalType(FromType); |
| 732 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 733 | } else { |
| 734 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 735 | SCS.Third = ICK_Identity; |
| 736 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 738 | // [...] Any difference in top-level cv-qualification is |
| 739 | // subsumed by the initialization itself and does not constitute |
| 740 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 741 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 743 | if (CanonFrom.getLocalUnqualifiedType() |
| 744 | == CanonTo.getLocalUnqualifiedType() && |
| 745 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 746 | FromType = ToType; |
| 747 | CanonFrom = CanonTo; |
| 748 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 750 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 751 | |
| 752 | // If we have not converted the argument type to the parameter type, |
| 753 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 754 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 755 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 757 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 761 | /// expression From (whose potentially-adjusted type is FromType) to |
| 762 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 763 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 765 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 766 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 767 | if (!To) { |
| 768 | return false; |
| 769 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 770 | |
| 771 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 772 | // unsigned short int can be converted to an rvalue of type int if |
| 773 | // int can represent all the values of the source type; otherwise, |
| 774 | // the source rvalue can be converted to an rvalue of type unsigned |
| 775 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 776 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 777 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 778 | if (// We can promote any signed, promotable integer type to an int |
| 779 | (FromType->isSignedIntegerType() || |
| 780 | // We can promote any unsigned integer type whose size is |
| 781 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 783 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 784 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 787 | return To->getKind() == BuiltinType::UInt; |
| 788 | } |
| 789 | |
| 790 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 791 | // can be converted to an rvalue of the first of the following types |
| 792 | // that can represent all the values of its underlying type: int, |
| 793 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 794 | |
| 795 | // We pre-calculate the promotion type for enum types. |
| 796 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 797 | if (ToType->isIntegerType()) |
| 798 | return Context.hasSameUnqualifiedType(ToType, |
| 799 | FromEnumType->getDecl()->getPromotionType()); |
| 800 | |
| 801 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 802 | // Determine whether the type we're converting from is signed or |
| 803 | // unsigned. |
| 804 | bool FromIsSigned; |
| 805 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 806 | |
| 807 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 808 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 809 | |
| 810 | // The types we'll try to promote to, in the appropriate |
| 811 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | QualType PromoteTypes[6] = { |
| 813 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 814 | Context.LongTy, Context.UnsignedLongTy , |
| 815 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 816 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 817 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 818 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 819 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 821 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 822 | // We found the type that we can promote to. If this is the |
| 823 | // type we wanted, we have a promotion. Otherwise, no |
| 824 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 825 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 831 | // rvalue of type int if int can represent all the values of the |
| 832 | // bit-field; otherwise, it can be converted to unsigned int if |
| 833 | // unsigned int can represent all the values of the bit-field. If |
| 834 | // the bit-field is larger yet, no integral promotion applies to |
| 835 | // it. If the bit-field has an enumerated type, it is treated as any |
| 836 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 837 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 838 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 839 | using llvm::APSInt; |
| 840 | if (From) |
| 841 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 842 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 843 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 844 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 845 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 846 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 848 | // Are we promoting to an int from a bitfield that fits in an int? |
| 849 | if (BitWidth < ToSize || |
| 850 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 851 | return To->getKind() == BuiltinType::Int; |
| 852 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 854 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 855 | // that fits into an unsigned int? |
| 856 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 857 | return To->getKind() == BuiltinType::UInt; |
| 858 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 860 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 861 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 862 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 864 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 865 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 866 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 867 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 869 | |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 874 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 875 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 877 | /// An rvalue of type float can be converted to an rvalue of type |
| 878 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 879 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 880 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 881 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 882 | ToBuiltin->getKind() == BuiltinType::Double) |
| 883 | return true; |
| 884 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 885 | // C99 6.3.1.5p1: |
| 886 | // When a float is promoted to double or long double, or a |
| 887 | // double is promoted to long double [...]. |
| 888 | if (!getLangOptions().CPlusPlus && |
| 889 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 890 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 891 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 892 | return true; |
| 893 | } |
| 894 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 895 | return false; |
| 896 | } |
| 897 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 898 | /// \brief Determine if a conversion is a complex promotion. |
| 899 | /// |
| 900 | /// A complex promotion is defined as a complex -> complex conversion |
| 901 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 902 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 903 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 904 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 905 | if (!FromComplex) |
| 906 | return false; |
| 907 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 908 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 909 | if (!ToComplex) |
| 910 | return false; |
| 911 | |
| 912 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 913 | ToComplex->getElementType()) || |
| 914 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 915 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 918 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 919 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 920 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 921 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 922 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | static QualType |
| 924 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 925 | QualType ToPointee, QualType ToType, |
| 926 | ASTContext &Context) { |
| 927 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 928 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 929 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
| 931 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 932 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 933 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 934 | if (!ToType.isNull()) |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 935 | return ToType; |
| 936 | |
| 937 | // Build a pointer to ToPointee. It has the right qualifiers |
| 938 | // already. |
| 939 | return Context.getPointerType(ToPointee); |
| 940 | } |
| 941 | |
| 942 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 943 | return Context.getPointerType( |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 944 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 945 | Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 948 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 949 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 950 | /// not have the right set of qualifiers. |
| 951 | static QualType |
| 952 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 953 | QualType ToType, |
| 954 | ASTContext &Context) { |
| 955 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 956 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 957 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 958 | |
| 959 | // Exact qualifier match -> return the pointer type we're converting to. |
| 960 | if (CanonToType.getLocalQualifiers() == Quals) |
| 961 | return ToType; |
| 962 | |
| 963 | // Just build a canonical type that has the right qualifiers. |
| 964 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 965 | } |
| 966 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 968 | bool InOverloadResolution, |
| 969 | ASTContext &Context) { |
| 970 | // Handle value-dependent integral null pointer constants correctly. |
| 971 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 972 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 973 | Expr->getType()->isIntegralType()) |
| 974 | return !InOverloadResolution; |
| 975 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 976 | return Expr->isNullPointerConstant(Context, |
| 977 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 978 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 979 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 981 | /// IsPointerConversion - Determines whether the conversion of the |
| 982 | /// expression From, which has the (possibly adjusted) type FromType, |
| 983 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 984 | /// 4.10). If so, returns true and places the converted type (that |
| 985 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 986 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 987 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 988 | /// This routine also supports conversions to and from block pointers |
| 989 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 990 | /// pointers to interfaces. FIXME: Once we've determined the |
| 991 | /// appropriate overloading rules for Objective-C, we may want to |
| 992 | /// split the Objective-C checks into a different routine; however, |
| 993 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 994 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 995 | /// set if the conversion is an allowed Objective-C conversion that |
| 996 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 997 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 998 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 999 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1001 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1002 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1003 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1004 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1006 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1007 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1008 | ConvertedType = ToType; |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1012 | // Blocks: Block pointers can be converted to void*. |
| 1013 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1014 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1015 | ConvertedType = ToType; |
| 1016 | return true; |
| 1017 | } |
| 1018 | // Blocks: A null pointer constant can be converted to a block |
| 1019 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1021 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1022 | ConvertedType = ToType; |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1026 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1027 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1029 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1030 | ConvertedType = ToType; |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1034 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1035 | if (!ToTypePtr) |
| 1036 | return false; |
| 1037 | |
| 1038 | // 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] | 1039 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1040 | ConvertedType = ToType; |
| 1041 | return true; |
| 1042 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1043 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1044 | // Beyond this point, both types need to be pointers |
| 1045 | // , including objective-c pointers. |
| 1046 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1047 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1048 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1049 | ToType, Context); |
| 1050 | return true; |
| 1051 | |
| 1052 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1053 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1054 | if (!FromTypePtr) |
| 1055 | return false; |
| 1056 | |
| 1057 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1059 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1060 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1061 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1062 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1064 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1065 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
| 1068 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1069 | // When we're overloading in C, we allow a special kind of pointer |
| 1070 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1072 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1074 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
| 1078 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1079 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1081 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1082 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1083 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1084 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1085 | // necessitates this conversion is ill-formed. The result of the |
| 1086 | // conversion is a pointer to the base class sub-object of the |
| 1087 | // derived class object. The null pointer value is converted to |
| 1088 | // the null pointer value of the destination type. |
| 1089 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1090 | // Note that we do not check for ambiguity or inaccessibility |
| 1091 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1092 | if (getLangOptions().CPlusPlus && |
| 1093 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1094 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1095 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1096 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1098 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1099 | ToType, Context); |
| 1100 | return true; |
| 1101 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | /// isObjCPointerConversion - Determines whether this is an |
| 1107 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1108 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1110 | QualType& ConvertedType, |
| 1111 | bool &IncompatibleObjC) { |
| 1112 | if (!getLangOptions().ObjC1) |
| 1113 | return false; |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1114 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1115 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1116 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1118 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1119 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1120 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1121 | // 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] | 1122 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1123 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1124 | ConvertedType = ToType; |
| 1125 | return true; |
| 1126 | } |
| 1127 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1129 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1131 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1132 | ConvertedType = ToType; |
| 1133 | return true; |
| 1134 | } |
| 1135 | // Objective C++: We're able to convert from a pointer to an |
| 1136 | // interface to a pointer to a different interface. |
| 1137 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1138 | ConvertedType = ToType; |
| 1139 | return true; |
| 1140 | } |
| 1141 | |
| 1142 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1143 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1144 | // interfaces, which is permitted. However, we're going to |
| 1145 | // complain about it. |
| 1146 | IncompatibleObjC = true; |
| 1147 | ConvertedType = FromType; |
| 1148 | return true; |
| 1149 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1151 | // 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] | 1152 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1153 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1154 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1155 | else if (const BlockPointerType *ToBlockPtr = |
| 1156 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1157 | // 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] | 1158 | // to a block pointer type. |
| 1159 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1160 | ConvertedType = ToType; |
| 1161 | return true; |
| 1162 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1163 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1164 | } |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1165 | else if (FromType->getAs<BlockPointerType>() && |
| 1166 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1167 | // 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] | 1168 | // pointer to any object. |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1169 | ConvertedType = ToType; |
| 1170 | return true; |
| 1171 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1172 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1173 | return false; |
| 1174 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1175 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1176 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1177 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1178 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1179 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1180 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1181 | return false; |
| 1182 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1183 | // If we have pointers to pointers, recursively check whether this |
| 1184 | // is an Objective-C conversion. |
| 1185 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1186 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1187 | IncompatibleObjC)) { |
| 1188 | // We always complain about this conversion. |
| 1189 | IncompatibleObjC = true; |
| 1190 | ConvertedType = ToType; |
| 1191 | return true; |
| 1192 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1193 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1194 | // as in I* to id. |
| 1195 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1196 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1197 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1198 | IncompatibleObjC)) { |
| 1199 | ConvertedType = ToType; |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1203 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1204 | // differences in the argument and result types are in Objective-C |
| 1205 | // pointer conversions. If so, we permit the conversion (but |
| 1206 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1208 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1209 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1210 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1211 | if (FromFunctionType && ToFunctionType) { |
| 1212 | // If the function types are exactly the same, this isn't an |
| 1213 | // Objective-C pointer conversion. |
| 1214 | if (Context.getCanonicalType(FromPointeeType) |
| 1215 | == Context.getCanonicalType(ToPointeeType)) |
| 1216 | return false; |
| 1217 | |
| 1218 | // Perform the quick checks that will tell us whether these |
| 1219 | // function types are obviously different. |
| 1220 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1221 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1222 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1223 | return false; |
| 1224 | |
| 1225 | bool HasObjCConversion = false; |
| 1226 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1227 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1228 | // Okay, the types match exactly. Nothing to do. |
| 1229 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1230 | ToFunctionType->getResultType(), |
| 1231 | ConvertedType, IncompatibleObjC)) { |
| 1232 | // Okay, we have an Objective-C pointer conversion. |
| 1233 | HasObjCConversion = true; |
| 1234 | } else { |
| 1235 | // Function types are too different. Abort. |
| 1236 | return false; |
| 1237 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1239 | // Check argument types. |
| 1240 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1241 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1242 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1243 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1244 | if (Context.getCanonicalType(FromArgType) |
| 1245 | == Context.getCanonicalType(ToArgType)) { |
| 1246 | // Okay, the types match exactly. Nothing to do. |
| 1247 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1248 | ConvertedType, IncompatibleObjC)) { |
| 1249 | // Okay, we have an Objective-C pointer conversion. |
| 1250 | HasObjCConversion = true; |
| 1251 | } else { |
| 1252 | // Argument types are too different. Abort. |
| 1253 | return false; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | if (HasObjCConversion) { |
| 1258 | // We had an Objective-C conversion. Allow this pointer |
| 1259 | // conversion, but complain about it. |
| 1260 | ConvertedType = ToType; |
| 1261 | IncompatibleObjC = true; |
| 1262 | return true; |
| 1263 | } |
| 1264 | } |
| 1265 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1266 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1269 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1270 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1271 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1272 | /// conversions for which IsPointerConversion has already returned |
| 1273 | /// true. It returns true and produces a diagnostic if there was an |
| 1274 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1275 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1276 | CastExpr::CastKind &Kind, |
| 1277 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1278 | QualType FromType = From->getType(); |
| 1279 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1280 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1281 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1282 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1283 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1284 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1285 | if (FromPointeeType->isRecordType() && |
| 1286 | ToPointeeType->isRecordType()) { |
| 1287 | // We must have a derived-to-base conversion. Check an |
| 1288 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1289 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1290 | From->getExprLoc(), |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1291 | From->getSourceRange(), |
| 1292 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1293 | return true; |
| 1294 | |
| 1295 | // The conversion was successful. |
| 1296 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1300 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1302 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1303 | // Objective-C++ conversions are always okay. |
| 1304 | // FIXME: We should have a different class of conversions for the |
| 1305 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1306 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1307 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1308 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1309 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1310 | return false; |
| 1311 | } |
| 1312 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1313 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1314 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1315 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1316 | /// If so, returns true and places the converted type (that might differ from |
| 1317 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1318 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1319 | QualType ToType, |
| 1320 | bool InOverloadResolution, |
| 1321 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1322 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1323 | if (!ToTypePtr) |
| 1324 | return false; |
| 1325 | |
| 1326 | // 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] | 1327 | if (From->isNullPointerConstant(Context, |
| 1328 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1329 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1330 | ConvertedType = ToType; |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
| 1334 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1335 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1336 | if (!FromTypePtr) |
| 1337 | return false; |
| 1338 | |
| 1339 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1340 | // where D is derived from B (C++ 4.11p2). |
| 1341 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1342 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1343 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1344 | |
| 1345 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1346 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1347 | ToClass.getTypePtr()); |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | return false; |
| 1352 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1353 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1354 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1355 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1356 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1357 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1358 | /// true and produces a diagnostic if there was an error, or returns false |
| 1359 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1361 | CastExpr::CastKind &Kind, |
| 1362 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1363 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1364 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1365 | if (!FromPtrType) { |
| 1366 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1367 | assert(From->isNullPointerConstant(Context, |
| 1368 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1369 | "Expr must be null pointer constant!"); |
| 1370 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1371 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1372 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1373 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1374 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1375 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1376 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1377 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1378 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1379 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1380 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1381 | // FIXME: What about dependent types? |
| 1382 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1383 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1384 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1385 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/ true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1386 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1387 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1388 | assert(DerivationOkay && |
| 1389 | "Should not have been called if derivation isn't OK."); |
| 1390 | (void)DerivationOkay; |
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 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1393 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1394 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1395 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1396 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1397 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1398 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1400 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1401 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1402 | << FromClass << ToClass << QualType(VBase, 0) |
| 1403 | << From->getSourceRange(); |
| 1404 | return true; |
| 1405 | } |
| 1406 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1407 | if (!IgnoreBaseAccess) |
| 1408 | CheckBaseClassAccess(From->getExprLoc(), /*BaseToDerived*/ true, |
| 1409 | FromClass, ToClass, Paths.front()); |
| 1410 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1411 | // Must be a base to derived member conversion. |
| 1412 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1413 | return false; |
| 1414 | } |
| 1415 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1416 | /// IsQualificationConversion - Determines whether the conversion from |
| 1417 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1418 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | bool |
| 1420 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1421 | FromType = Context.getCanonicalType(FromType); |
| 1422 | ToType = Context.getCanonicalType(ToType); |
| 1423 | |
| 1424 | // If FromType and ToType are the same type, this is not a |
| 1425 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1426 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1427 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1428 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1429 | // (C++ 4.4p4): |
| 1430 | // A conversion can add cv-qualifiers at levels other than the first |
| 1431 | // in multi-level pointers, subject to the following rules: [...] |
| 1432 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1433 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1434 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1435 | // Within each iteration of the loop, we check the qualifiers to |
| 1436 | // determine if this still looks like a qualification |
| 1437 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1438 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1439 | // until there are no more pointers or pointers-to-members left to |
| 1440 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1441 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1442 | |
| 1443 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1444 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1445 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1446 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1448 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1449 | // every cv for 0 < k < j. |
| 1450 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1451 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1452 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1454 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1455 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1457 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1458 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1459 | |
| 1460 | // We are left with FromType and ToType being the pointee types |
| 1461 | // after unwrapping the original FromType and ToType the same number |
| 1462 | // of types. If we unwrapped any pointers, and if FromType and |
| 1463 | // ToType have the same unqualified type (since we checked |
| 1464 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1465 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1468 | /// Determines whether there is a user-defined conversion sequence |
| 1469 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1470 | /// ToType. If such a conversion exists, User will contain the |
| 1471 | /// user-defined conversion sequence that performs such a conversion |
| 1472 | /// and this routine will return true. Otherwise, this routine returns |
| 1473 | /// false and User is unspecified. |
| 1474 | /// |
| 1475 | /// \param AllowConversionFunctions true if the conversion should |
| 1476 | /// consider conversion functions at all. If false, only constructors |
| 1477 | /// will be considered. |
| 1478 | /// |
| 1479 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1480 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1481 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1482 | /// |
| 1483 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1484 | /// for overload resolution. |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1485 | /// \param UserCast true if looking for user defined conversion for a static |
| 1486 | /// cast. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1487 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1488 | UserDefinedConversionSequence& User, |
| 1489 | OverloadCandidateSet& CandidateSet, |
| 1490 | bool AllowConversionFunctions, |
| 1491 | bool AllowExplicit, |
| 1492 | bool ForceRValue, |
| 1493 | bool UserCast) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1494 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1495 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1496 | // We're not going to find any constructors. |
| 1497 | } else if (CXXRecordDecl *ToRecordDecl |
| 1498 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1499 | // C++ [over.match.ctor]p1: |
| 1500 | // When objects of class type are direct-initialized (8.5), or |
| 1501 | // copy-initialized from an expression of the same or a |
| 1502 | // derived class type (8.5), overload resolution selects the |
| 1503 | // constructor. [...] For copy-initialization, the candidate |
| 1504 | // functions are all the converting constructors (12.3.1) of |
| 1505 | // that class. The argument list is the expression-list within |
| 1506 | // the parentheses of the initializer. |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1507 | bool SuppressUserConversions = !UserCast; |
| 1508 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1509 | IsDerivedFrom(From->getType(), ToType)) { |
| 1510 | SuppressUserConversions = false; |
| 1511 | AllowConversionFunctions = false; |
| 1512 | } |
| 1513 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1515 | = Context.DeclarationNames.getCXXConstructorName( |
| 1516 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1517 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1519 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1520 | Con != ConEnd; ++Con) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1521 | // Find the constructor (which may be a template). |
| 1522 | CXXConstructorDecl *Constructor = 0; |
| 1523 | FunctionTemplateDecl *ConstructorTmpl |
| 1524 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1525 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1527 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1528 | else |
| 1529 | Constructor = cast<CXXConstructorDecl>(*Con); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1530 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1531 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1532 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1533 | if (ConstructorTmpl) |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1534 | AddTemplateOverloadCandidate(ConstructorTmpl, |
| 1535 | ConstructorTmpl->getAccess(), |
| 1536 | /*ExplicitArgs*/ 0, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1537 | &From, 1, CandidateSet, |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1538 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1539 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1540 | // Allow one user-defined conversion when user specifies a |
| 1541 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1542 | AddOverloadCandidate(Constructor, Constructor->getAccess(), |
| 1543 | &From, 1, CandidateSet, |
Douglas Gregor | 379d84b | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 1544 | SuppressUserConversions, ForceRValue); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1545 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1546 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1550 | if (!AllowConversionFunctions) { |
| 1551 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1553 | PDiag(0) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1554 | << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1555 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | } else if (const RecordType *FromRecordType |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1557 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1559 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1560 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1561 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1562 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1563 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1564 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1565 | NamedDecl *D = *I; |
| 1566 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1567 | if (isa<UsingShadowDecl>(D)) |
| 1568 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1569 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1570 | CXXConversionDecl *Conv; |
| 1571 | FunctionTemplateDecl *ConvTemplate; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1572 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(*I))) |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1573 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1574 | else |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1575 | Conv = dyn_cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1578 | if (ConvTemplate) |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1579 | AddTemplateConversionCandidate(ConvTemplate, I.getAccess(), |
| 1580 | ActingContext, From, ToType, |
| 1581 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1582 | else |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1583 | AddConversionCandidate(Conv, I.getAccess(), ActingContext, |
| 1584 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1588 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1589 | |
| 1590 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1591 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1592 | case OR_Success: |
| 1593 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1595 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1596 | // C++ [over.ics.user]p1: |
| 1597 | // If the user-defined conversion is specified by a |
| 1598 | // constructor (12.3.1), the initial standard conversion |
| 1599 | // sequence converts the source type to the type required by |
| 1600 | // the argument of the constructor. |
| 1601 | // |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1602 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1603 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1604 | User.EllipsisConversion = true; |
| 1605 | else { |
| 1606 | User.Before = Best->Conversions[0].Standard; |
| 1607 | User.EllipsisConversion = false; |
| 1608 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1609 | User.ConversionFunction = Constructor; |
| 1610 | User.After.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1611 | User.After.setFromType( |
| 1612 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1613 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1614 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1615 | } else if (CXXConversionDecl *Conversion |
| 1616 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1617 | // C++ [over.ics.user]p1: |
| 1618 | // |
| 1619 | // [...] If the user-defined conversion is specified by a |
| 1620 | // conversion function (12.3.2), the initial standard |
| 1621 | // conversion sequence converts the source type to the |
| 1622 | // implicit object parameter of the conversion function. |
| 1623 | User.Before = Best->Conversions[0].Standard; |
| 1624 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1625 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
| 1627 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1628 | // The second standard conversion sequence converts the |
| 1629 | // result of the user-defined conversion to the target type |
| 1630 | // for the sequence. Since an implicit conversion sequence |
| 1631 | // is an initialization, the special rules for |
| 1632 | // initialization by user-defined conversion apply when |
| 1633 | // selecting the best user-defined conversion for a |
| 1634 | // user-defined conversion sequence (see 13.3.3 and |
| 1635 | // 13.3.3.1). |
| 1636 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1637 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1638 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1639 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1640 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1641 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1643 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1644 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1645 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1646 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1647 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1648 | |
| 1649 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1650 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1653 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1654 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1655 | |
| 1656 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1657 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1658 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1659 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1660 | OverloadingResult OvResult = |
| 1661 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
| 1662 | CandidateSet, true, false, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1663 | if (OvResult == OR_Ambiguous) |
| 1664 | Diag(From->getSourceRange().getBegin(), |
| 1665 | diag::err_typecheck_ambiguous_condition) |
| 1666 | << From->getType() << ToType << From->getSourceRange(); |
| 1667 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1668 | Diag(From->getSourceRange().getBegin(), |
| 1669 | diag::err_typecheck_nonviable_condition) |
| 1670 | << From->getType() << ToType << From->getSourceRange(); |
| 1671 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1672 | return false; |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1673 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1674 | return true; |
| 1675 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1676 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1677 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1678 | /// conversion sequences to determine whether one is better than the |
| 1679 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1681 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1682 | const ImplicitConversionSequence& ICS2) |
| 1683 | { |
| 1684 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1685 | // conversion sequences (as defined in 13.3.3.1) |
| 1686 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1687 | // conversion sequence than a user-defined conversion sequence or |
| 1688 | // an ellipsis conversion sequence, and |
| 1689 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1690 | // conversion sequence than an ellipsis conversion sequence |
| 1691 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1693 | // C++0x [over.best.ics]p10: |
| 1694 | // For the purpose of ranking implicit conversion sequences as |
| 1695 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1696 | // treated as a user-defined sequence that is indistinguishable |
| 1697 | // from any other user-defined conversion sequence. |
| 1698 | if (ICS1.getKind() < ICS2.getKind()) { |
| 1699 | if (!(ICS1.isUserDefined() && ICS2.isAmbiguous())) |
| 1700 | return ImplicitConversionSequence::Better; |
| 1701 | } else if (ICS2.getKind() < ICS1.getKind()) { |
| 1702 | if (!(ICS2.isUserDefined() && ICS1.isAmbiguous())) |
| 1703 | return ImplicitConversionSequence::Worse; |
| 1704 | } |
| 1705 | |
| 1706 | if (ICS1.isAmbiguous() || ICS2.isAmbiguous()) |
| 1707 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1708 | |
| 1709 | // Two implicit conversion sequences of the same form are |
| 1710 | // indistinguishable conversion sequences unless one of the |
| 1711 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1712 | if (ICS1.isStandard()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1713 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1714 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1715 | // User-defined conversion sequence U1 is a better conversion |
| 1716 | // sequence than another user-defined conversion sequence U2 if |
| 1717 | // they contain the same user-defined conversion function or |
| 1718 | // constructor and if the second standard conversion sequence of |
| 1719 | // U1 is better than the second standard conversion sequence of |
| 1720 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1722 | ICS2.UserDefined.ConversionFunction) |
| 1723 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1724 | ICS2.UserDefined.After); |
| 1725 | } |
| 1726 | |
| 1727 | return ImplicitConversionSequence::Indistinguishable; |
| 1728 | } |
| 1729 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1730 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 1731 | // determine if one is a proper subset of the other. |
| 1732 | static ImplicitConversionSequence::CompareKind |
| 1733 | compareStandardConversionSubsets(ASTContext &Context, |
| 1734 | const StandardConversionSequence& SCS1, |
| 1735 | const StandardConversionSequence& SCS2) { |
| 1736 | ImplicitConversionSequence::CompareKind Result |
| 1737 | = ImplicitConversionSequence::Indistinguishable; |
| 1738 | |
| 1739 | if (SCS1.Second != SCS2.Second) { |
| 1740 | if (SCS1.Second == ICK_Identity) |
| 1741 | Result = ImplicitConversionSequence::Better; |
| 1742 | else if (SCS2.Second == ICK_Identity) |
| 1743 | Result = ImplicitConversionSequence::Worse; |
| 1744 | else |
| 1745 | return ImplicitConversionSequence::Indistinguishable; |
| 1746 | } else if (!Context.hasSameType(SCS1.getToType(1), SCS2.getToType(1))) |
| 1747 | return ImplicitConversionSequence::Indistinguishable; |
| 1748 | |
| 1749 | if (SCS1.Third == SCS2.Third) { |
| 1750 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 1751 | : ImplicitConversionSequence::Indistinguishable; |
| 1752 | } |
| 1753 | |
| 1754 | if (SCS1.Third == ICK_Identity) |
| 1755 | return Result == ImplicitConversionSequence::Worse |
| 1756 | ? ImplicitConversionSequence::Indistinguishable |
| 1757 | : ImplicitConversionSequence::Better; |
| 1758 | |
| 1759 | if (SCS2.Third == ICK_Identity) |
| 1760 | return Result == ImplicitConversionSequence::Better |
| 1761 | ? ImplicitConversionSequence::Indistinguishable |
| 1762 | : ImplicitConversionSequence::Worse; |
| 1763 | |
| 1764 | return ImplicitConversionSequence::Indistinguishable; |
| 1765 | } |
| 1766 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1767 | /// CompareStandardConversionSequences - Compare two standard |
| 1768 | /// conversion sequences to determine whether one is better than the |
| 1769 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1771 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1772 | const StandardConversionSequence& SCS2) |
| 1773 | { |
| 1774 | // Standard conversion sequence S1 is a better conversion sequence |
| 1775 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1776 | |
| 1777 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1778 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1779 | // excluding any Lvalue Transformation; the identity conversion |
| 1780 | // sequence is considered to be a subsequence of any |
| 1781 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1782 | if (ImplicitConversionSequence::CompareKind CK |
| 1783 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 1784 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1785 | |
| 1786 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1787 | // defined below), or, if not that, |
| 1788 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1789 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1790 | if (Rank1 < Rank2) |
| 1791 | return ImplicitConversionSequence::Better; |
| 1792 | else if (Rank2 < Rank1) |
| 1793 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1795 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1796 | // are indistinguishable unless one of the following rules |
| 1797 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1799 | // A conversion that is not a conversion of a pointer, or |
| 1800 | // pointer to member, to bool is better than another conversion |
| 1801 | // that is such a conversion. |
| 1802 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1803 | return SCS2.isPointerConversionToBool() |
| 1804 | ? ImplicitConversionSequence::Better |
| 1805 | : ImplicitConversionSequence::Worse; |
| 1806 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1807 | // C++ [over.ics.rank]p4b2: |
| 1808 | // |
| 1809 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1810 | // conversion of B* to A* is better than conversion of B* to |
| 1811 | // void*, and conversion of A* to void* is better than conversion |
| 1812 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1814 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1816 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1817 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1818 | // Exactly one of the conversion sequences is a conversion to |
| 1819 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1820 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1821 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1822 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1823 | // Neither conversion sequence converts to a void pointer; compare |
| 1824 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1825 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1826 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1827 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1828 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1829 | // Both conversion sequences are conversions to void |
| 1830 | // pointers. Compare the source types to determine if there's an |
| 1831 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1832 | QualType FromType1 = SCS1.getFromType(); |
| 1833 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Adjust the types we're converting from via the array-to-pointer |
| 1836 | // conversion, if we need to. |
| 1837 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1838 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1839 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1840 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1841 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1842 | QualType FromPointee1 |
| 1843 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 1844 | QualType FromPointee2 |
| 1845 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 1847 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1848 | return ImplicitConversionSequence::Better; |
| 1849 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1850 | return ImplicitConversionSequence::Worse; |
| 1851 | |
| 1852 | // Objective-C++: If one interface is more specific than the |
| 1853 | // other, it is the better one. |
| 1854 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 1855 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 1856 | if (FromIface1 && FromIface1) { |
| 1857 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1858 | return ImplicitConversionSequence::Better; |
| 1859 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1860 | return ImplicitConversionSequence::Worse; |
| 1861 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1862 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1863 | |
| 1864 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1865 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1867 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1868 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1870 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1871 | // C++0x [over.ics.rank]p3b4: |
| 1872 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1873 | // implicit object parameter of a non-static member function declared |
| 1874 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1875 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1876 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1877 | // or if the member function in this case has a ref qualifier. |
| 1878 | // (Of course, we don't have ref qualifiers yet.) |
| 1879 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1880 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1881 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1882 | |
| 1883 | // C++ [over.ics.rank]p3b4: |
| 1884 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1885 | // which the references refer are the same type except for |
| 1886 | // top-level cv-qualifiers, and the type to which the reference |
| 1887 | // initialized by S2 refers is more cv-qualified than the type |
| 1888 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1889 | QualType T1 = SCS1.getToType(2); |
| 1890 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1891 | T1 = Context.getCanonicalType(T1); |
| 1892 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1893 | Qualifiers T1Quals, T2Quals; |
| 1894 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1895 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 1896 | if (UnqualT1 == UnqualT2) { |
| 1897 | // If the type is an array type, promote the element qualifiers to the type |
| 1898 | // for comparison. |
| 1899 | if (isa<ArrayType>(T1) && T1Quals) |
| 1900 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1901 | if (isa<ArrayType>(T2) && T2Quals) |
| 1902 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1903 | if (T2.isMoreQualifiedThan(T1)) |
| 1904 | return ImplicitConversionSequence::Better; |
| 1905 | else if (T1.isMoreQualifiedThan(T2)) |
| 1906 | return ImplicitConversionSequence::Worse; |
| 1907 | } |
| 1908 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1909 | |
| 1910 | return ImplicitConversionSequence::Indistinguishable; |
| 1911 | } |
| 1912 | |
| 1913 | /// CompareQualificationConversions - Compares two standard conversion |
| 1914 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1916 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1917 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1919 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1920 | // -- S1 and S2 differ only in their qualification conversion and |
| 1921 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1922 | // cv-qualification signature of type T1 is a proper subset of |
| 1923 | // the cv-qualification signature of type T2, and S1 is not the |
| 1924 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1925 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1926 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1927 | return ImplicitConversionSequence::Indistinguishable; |
| 1928 | |
| 1929 | // FIXME: the example in the standard doesn't use a qualification |
| 1930 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1931 | QualType T1 = SCS1.getToType(2); |
| 1932 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1933 | T1 = Context.getCanonicalType(T1); |
| 1934 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1935 | Qualifiers T1Quals, T2Quals; |
| 1936 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 1937 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1938 | |
| 1939 | // If the types are the same, we won't learn anything by unwrapped |
| 1940 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1941 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1942 | return ImplicitConversionSequence::Indistinguishable; |
| 1943 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 1944 | // If the type is an array type, promote the element qualifiers to the type |
| 1945 | // for comparison. |
| 1946 | if (isa<ArrayType>(T1) && T1Quals) |
| 1947 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 1948 | if (isa<ArrayType>(T2) && T2Quals) |
| 1949 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 1950 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1952 | = ImplicitConversionSequence::Indistinguishable; |
| 1953 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1954 | // Within each iteration of the loop, we check the qualifiers to |
| 1955 | // determine if this still looks like a qualification |
| 1956 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1957 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1958 | // until there are no more pointers or pointers-to-members left |
| 1959 | // to unwrap. This essentially mimics what |
| 1960 | // IsQualificationConversion does, but here we're checking for a |
| 1961 | // strict subset of qualifiers. |
| 1962 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1963 | // The qualifiers are the same, so this doesn't tell us anything |
| 1964 | // about how the sequences rank. |
| 1965 | ; |
| 1966 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1967 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1968 | if (Result == ImplicitConversionSequence::Worse) |
| 1969 | // Neither has qualifiers that are a subset of the other's |
| 1970 | // qualifiers. |
| 1971 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1973 | Result = ImplicitConversionSequence::Better; |
| 1974 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1975 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1976 | if (Result == ImplicitConversionSequence::Better) |
| 1977 | // Neither has qualifiers that are a subset of the other's |
| 1978 | // qualifiers. |
| 1979 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1981 | Result = ImplicitConversionSequence::Worse; |
| 1982 | } else { |
| 1983 | // Qualifiers are disjoint. |
| 1984 | return ImplicitConversionSequence::Indistinguishable; |
| 1985 | } |
| 1986 | |
| 1987 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1988 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1989 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1992 | // Check that the winning standard conversion sequence isn't using |
| 1993 | // the deprecated string literal array to pointer conversion. |
| 1994 | switch (Result) { |
| 1995 | case ImplicitConversionSequence::Better: |
| 1996 | if (SCS1.Deprecated) |
| 1997 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1998 | break; |
| 1999 | |
| 2000 | case ImplicitConversionSequence::Indistinguishable: |
| 2001 | break; |
| 2002 | |
| 2003 | case ImplicitConversionSequence::Worse: |
| 2004 | if (SCS2.Deprecated) |
| 2005 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2006 | break; |
| 2007 | } |
| 2008 | |
| 2009 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2012 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2013 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2014 | /// various kinds of derived-to-base conversions (C++ |
| 2015 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2016 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2017 | ImplicitConversionSequence::CompareKind |
| 2018 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2019 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2020 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2021 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2022 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2023 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Adjust the types we're converting from via the array-to-pointer |
| 2026 | // conversion, if we need to. |
| 2027 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2028 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2029 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2030 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2031 | |
| 2032 | // Canonicalize all of the types. |
| 2033 | FromType1 = Context.getCanonicalType(FromType1); |
| 2034 | ToType1 = Context.getCanonicalType(ToType1); |
| 2035 | FromType2 = Context.getCanonicalType(FromType2); |
| 2036 | ToType2 = Context.getCanonicalType(ToType2); |
| 2037 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2038 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2039 | // |
| 2040 | // If class B is derived directly or indirectly from class A and |
| 2041 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2042 | // |
| 2043 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2044 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2045 | |
| 2046 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2048 | SCS2.Second == ICK_Pointer_Conversion && |
| 2049 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2050 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2051 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2053 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2055 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2056 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2057 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2058 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2059 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2060 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2061 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2062 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2063 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2064 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2065 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2066 | // -- 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] | 2067 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2068 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2069 | return ImplicitConversionSequence::Better; |
| 2070 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2071 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2072 | |
| 2073 | if (ToIface1 && ToIface2) { |
| 2074 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2075 | return ImplicitConversionSequence::Better; |
| 2076 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2077 | return ImplicitConversionSequence::Worse; |
| 2078 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2079 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2080 | |
| 2081 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2082 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2083 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2084 | return ImplicitConversionSequence::Better; |
| 2085 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2086 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2088 | if (FromIface1 && FromIface2) { |
| 2089 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2090 | return ImplicitConversionSequence::Better; |
| 2091 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2092 | return ImplicitConversionSequence::Worse; |
| 2093 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2094 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2097 | // Compare based on reference bindings. |
| 2098 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 2099 | SCS1.Second == ICK_Derived_To_Base) { |
| 2100 | // -- binding of an expression of type C to a reference of type |
| 2101 | // B& is better than binding an expression of type C to a |
| 2102 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2103 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2104 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2105 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2106 | return ImplicitConversionSequence::Better; |
| 2107 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2108 | return ImplicitConversionSequence::Worse; |
| 2109 | } |
| 2110 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2111 | // -- binding of an expression of type B to a reference of type |
| 2112 | // A& is better than binding an expression of type C to a |
| 2113 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2114 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2115 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2116 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2117 | return ImplicitConversionSequence::Better; |
| 2118 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2119 | return ImplicitConversionSequence::Worse; |
| 2120 | } |
| 2121 | } |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2122 | |
| 2123 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2124 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2125 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2126 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2127 | const MemberPointerType * FromMemPointer1 = |
| 2128 | FromType1->getAs<MemberPointerType>(); |
| 2129 | const MemberPointerType * ToMemPointer1 = |
| 2130 | ToType1->getAs<MemberPointerType>(); |
| 2131 | const MemberPointerType * FromMemPointer2 = |
| 2132 | FromType2->getAs<MemberPointerType>(); |
| 2133 | const MemberPointerType * ToMemPointer2 = |
| 2134 | ToType2->getAs<MemberPointerType>(); |
| 2135 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2136 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2137 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2138 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2139 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2140 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2141 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2142 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2143 | // 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] | 2144 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2145 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2146 | return ImplicitConversionSequence::Worse; |
| 2147 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2148 | return ImplicitConversionSequence::Better; |
| 2149 | } |
| 2150 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2151 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2152 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2153 | return ImplicitConversionSequence::Better; |
| 2154 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2155 | return ImplicitConversionSequence::Worse; |
| 2156 | } |
| 2157 | } |
| 2158 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2159 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 2160 | SCS1.Second == ICK_Derived_To_Base) { |
| 2161 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2162 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2163 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2164 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2165 | return ImplicitConversionSequence::Better; |
| 2166 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2167 | return ImplicitConversionSequence::Worse; |
| 2168 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2170 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2171 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2172 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2173 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2174 | return ImplicitConversionSequence::Better; |
| 2175 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2176 | return ImplicitConversionSequence::Worse; |
| 2177 | } |
| 2178 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2179 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2180 | return ImplicitConversionSequence::Indistinguishable; |
| 2181 | } |
| 2182 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2183 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2184 | /// ToType from the expression From. Return the implicit conversion |
| 2185 | /// sequence required to pass this argument, which may be a bad |
| 2186 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2187 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2188 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 2189 | /// 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] | 2190 | ImplicitConversionSequence |
| 2191 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2192 | bool SuppressUserConversions, bool ForceRValue, |
| 2193 | bool InOverloadResolution) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2194 | if (ToType->isReferenceType()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2195 | ImplicitConversionSequence ICS; |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2196 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2198 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2199 | SuppressUserConversions, |
| 2200 | /*AllowExplicit=*/false, |
| 2201 | ForceRValue, |
| 2202 | &ICS); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2203 | return ICS; |
| 2204 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2206 | SuppressUserConversions, |
| 2207 | /*AllowExplicit=*/false, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2208 | ForceRValue, |
| 2209 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2210 | } |
| 2211 | } |
| 2212 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2213 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 2214 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 2215 | /// an error, returns false if the initialization succeeded. Elidable should |
| 2216 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 2217 | /// differently in C++0x for this case. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2219 | AssignmentAction Action, bool Elidable) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2220 | if (!getLangOptions().CPlusPlus) { |
| 2221 | // In C, argument passing is the same as performing an assignment. |
| 2222 | QualType FromType = From->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2224 | AssignConvertType ConvTy = |
| 2225 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2226 | if (ConvTy != Compatible && |
| 2227 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 2228 | ConvTy = Compatible; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2230 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2231 | FromType, From, Action); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2232 | } |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2234 | if (ToType->isReferenceType()) |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2235 | return CheckReferenceInit(From, ToType, |
Douglas Gregor | c809cc2 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2236 | /*FIXME:*/From->getLocStart(), |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2237 | /*SuppressUserConversions=*/false, |
| 2238 | /*AllowExplicit=*/false, |
| 2239 | /*ForceRValue=*/false); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2241 | if (!PerformImplicitConversion(From, ToType, Action, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2242 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2243 | return false; |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2244 | if (!DiagnoseMultipleUserDefinedConversion(From, ToType)) |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2245 | return Diag(From->getSourceRange().getBegin(), |
| 2246 | diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2247 | << ToType << From->getType() << Action << From->getSourceRange(); |
Fariborz Jahanian | 0b51c72 | 2009-09-22 19:53:15 +0000 | [diff] [blame] | 2248 | return true; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2251 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2252 | /// parameter of the given member function (@c Method) from the |
| 2253 | /// expression @p From. |
| 2254 | ImplicitConversionSequence |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2255 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2256 | CXXMethodDecl *Method, |
| 2257 | CXXRecordDecl *ActingContext) { |
| 2258 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2259 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2260 | // const volatile object. |
| 2261 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2262 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2263 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2266 | // to exit early. |
| 2267 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2268 | |
| 2269 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2270 | QualType FromType = OrigFromType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2271 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2272 | FromType = PT->getPointeeType(); |
| 2273 | |
| 2274 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2275 | |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2276 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2277 | // where X is the class of which the function is a member |
| 2278 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2279 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2281 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2282 | // reference binding here, that allows class rvalues to bind to |
| 2283 | // non-constant references. |
| 2284 | |
| 2285 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2286 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2287 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2288 | if (ImplicitParamType.getCVRQualifiers() |
| 2289 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2290 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2291 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2292 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2293 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2294 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2295 | |
| 2296 | // Check that we have either the same type or a derived type. It |
| 2297 | // affects the conversion rank. |
| 2298 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2299 | ImplicitConversionKind SecondKind; |
| 2300 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2301 | SecondKind = ICK_Identity; |
| 2302 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2303 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2304 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2305 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2306 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2307 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2308 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2309 | |
| 2310 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2311 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2312 | ICS.Standard.setAsIdentityConversion(); |
| 2313 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2314 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2315 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2316 | ICS.Standard.ReferenceBinding = true; |
| 2317 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2318 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2319 | return ICS; |
| 2320 | } |
| 2321 | |
| 2322 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2323 | /// the implicit object parameter for the given Method with the given |
| 2324 | /// expression. |
| 2325 | bool |
| 2326 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2327 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2329 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2331 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2332 | FromRecordType = PT->getPointeeType(); |
| 2333 | DestType = Method->getThisType(Context); |
| 2334 | } else { |
| 2335 | FromRecordType = From->getType(); |
| 2336 | DestType = ImplicitParamRecordType; |
| 2337 | } |
| 2338 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2339 | // Note that we always use the true parent context when performing |
| 2340 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | ImplicitConversionSequence ICS |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2342 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2343 | Method->getParent()); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2344 | if (ICS.isBad()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2345 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2346 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2347 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2349 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2350 | CheckDerivedToBaseConversion(FromRecordType, |
| 2351 | ImplicitParamRecordType, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2352 | From->getSourceRange().getBegin(), |
| 2353 | From->getSourceRange())) |
| 2354 | return true; |
| 2355 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 4f4aab2 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2357 | /*isLvalue=*/true); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2358 | return false; |
| 2359 | } |
| 2360 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2361 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2362 | /// expression From to bool (C++0x [conv]p3). |
| 2363 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2365 | // FIXME: Are these flags correct? |
| 2366 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2368 | /*ForceRValue=*/false, |
| 2369 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2373 | /// of the expression From to bool (C++0x [conv]p3). |
| 2374 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2375 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2376 | if (!ICS.isBad()) |
| 2377 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2378 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2379 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2380 | return Diag(From->getSourceRange().getBegin(), |
| 2381 | diag::err_typecheck_bool_condition) |
| 2382 | << From->getType() << From->getSourceRange(); |
| 2383 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2386 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2387 | /// candidate functions, using the given function call arguments. If |
| 2388 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2389 | /// conversions via constructors or conversion operators. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2390 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2391 | /// hacky way to implement the overloading rules for elidable copy |
| 2392 | /// initialization in C++0x (C++0x 12.8p15). |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2393 | /// |
| 2394 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2395 | /// based on an incomplete set of function arguments. This feature is used by |
| 2396 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | void |
| 2398 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2399 | AccessSpecifier Access, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2400 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2401 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2402 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2403 | bool ForceRValue, |
| 2404 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2406 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2407 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2408 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2409 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2411 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2412 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2413 | // If we get here, it's because we're calling a member function |
| 2414 | // that is named without a member access expression (e.g., |
| 2415 | // "this->f") that was either written explicitly or created |
| 2416 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2417 | // function, e.g., X::f(). We use an empty type for the implied |
| 2418 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2419 | // is irrelevant. |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2420 | AddMethodCandidate(Method, Access, Method->getParent(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2421 | QualType(), Args, NumArgs, CandidateSet, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2422 | SuppressUserConversions, ForceRValue); |
| 2423 | return; |
| 2424 | } |
| 2425 | // We treat a constructor like a non-member function, since its object |
| 2426 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2429 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2430 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2431 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2432 | // Overload resolution is always an unevaluated context. |
| 2433 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2434 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2435 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2436 | // C++ [class.copy]p3: |
| 2437 | // A member function template is never instantiated to perform the copy |
| 2438 | // of a class object to an object of its class type. |
| 2439 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2440 | if (NumArgs == 1 && |
| 2441 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 2442 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 2443 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2444 | return; |
| 2445 | } |
| 2446 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2447 | // Add this candidate |
| 2448 | CandidateSet.push_back(OverloadCandidate()); |
| 2449 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2450 | Candidate.Function = Function; |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2451 | Candidate.Access = Access; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2452 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2453 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2454 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2455 | |
| 2456 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2457 | |
| 2458 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2459 | // parameters is viable only if it has an ellipsis in its parameter |
| 2460 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 2461 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 2462 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2463 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2464 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2465 | return; |
| 2466 | } |
| 2467 | |
| 2468 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2469 | // is viable only if the (m+1)st parameter has a default argument |
| 2470 | // (8.3.6). For the purposes of overload resolution, the |
| 2471 | // parameter list is truncated on the right, so that there are |
| 2472 | // exactly m parameters. |
| 2473 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2474 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2475 | // Not enough arguments. |
| 2476 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2477 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2478 | return; |
| 2479 | } |
| 2480 | |
| 2481 | // Determine the implicit conversion sequences for each of the |
| 2482 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2483 | Candidate.Conversions.resize(NumArgs); |
| 2484 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2485 | if (ArgIdx < NumArgsInProto) { |
| 2486 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2487 | // exist for each argument an implicit conversion sequence |
| 2488 | // (13.3.3.1) that converts that argument to the corresponding |
| 2489 | // parameter of F. |
| 2490 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2491 | Candidate.Conversions[ArgIdx] |
| 2492 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2493 | SuppressUserConversions, ForceRValue, |
| 2494 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2495 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 2496 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2497 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2498 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2499 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2500 | } else { |
| 2501 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2502 | // argument for which there is no corresponding parameter is |
| 2503 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2504 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2509 | /// \brief Add all of the function declarations in the given function set to |
| 2510 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2511 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2512 | Expr **Args, unsigned NumArgs, |
| 2513 | OverloadCandidateSet& CandidateSet, |
| 2514 | bool SuppressUserConversions) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2515 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2516 | // FIXME: using declarations |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2517 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) { |
| 2518 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2519 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getAccess(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2520 | cast<CXXMethodDecl>(FD)->getParent(), |
| 2521 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2522 | CandidateSet, SuppressUserConversions); |
| 2523 | else |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2524 | AddOverloadCandidate(FD, AS_none, Args, NumArgs, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2525 | SuppressUserConversions); |
| 2526 | } else { |
| 2527 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*F); |
| 2528 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 2529 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 2530 | AddMethodTemplateCandidate(FunTmpl, F.getAccess(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2531 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2532 | /*FIXME: explicit args */ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2533 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2534 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2535 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2536 | else |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2537 | AddTemplateOverloadCandidate(FunTmpl, AS_none, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2538 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2539 | Args, NumArgs, CandidateSet, |
| 2540 | SuppressUserConversions); |
| 2541 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2542 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2545 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 2546 | /// method) as a method candidate to the given overload set. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2547 | void Sema::AddMethodCandidate(NamedDecl *Decl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2548 | AccessSpecifier Access, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2549 | QualType ObjectType, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2550 | Expr **Args, unsigned NumArgs, |
| 2551 | OverloadCandidateSet& CandidateSet, |
| 2552 | bool SuppressUserConversions, bool ForceRValue) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2553 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2554 | |
| 2555 | if (isa<UsingShadowDecl>(Decl)) |
| 2556 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 2557 | |
| 2558 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 2559 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 2560 | "Expected a member function template"); |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2561 | AddMethodTemplateCandidate(TD, Access, ActingContext, /*ExplicitArgs*/ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2562 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2563 | CandidateSet, |
| 2564 | SuppressUserConversions, |
| 2565 | ForceRValue); |
| 2566 | } else { |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2567 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), Access, ActingContext, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2568 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 2569 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2570 | } |
| 2571 | } |
| 2572 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2573 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2574 | /// of candidate functions, using the given function call arguments |
| 2575 | /// and the object argument (@c Object). For example, in a call |
| 2576 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2577 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2578 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2579 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2580 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2581 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | void |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2583 | Sema::AddMethodCandidate(CXXMethodDecl *Method, AccessSpecifier Access, |
| 2584 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 2585 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2586 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | bool SuppressUserConversions, bool ForceRValue) { |
| 2588 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2589 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2590 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2591 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2592 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2594 | if (!CandidateSet.isNewCandidate(Method)) |
| 2595 | return; |
| 2596 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2597 | // Overload resolution is always an unevaluated context. |
| 2598 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2599 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2600 | // Add this candidate |
| 2601 | CandidateSet.push_back(OverloadCandidate()); |
| 2602 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2603 | Candidate.Function = Method; |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2604 | Candidate.Access = Access; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2605 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2606 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2607 | |
| 2608 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2609 | |
| 2610 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2611 | // parameters is viable only if it has an ellipsis in its parameter |
| 2612 | // list (8.3.5). |
| 2613 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2614 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2615 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2616 | return; |
| 2617 | } |
| 2618 | |
| 2619 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2620 | // is viable only if the (m+1)st parameter has a default argument |
| 2621 | // (8.3.6). For the purposes of overload resolution, the |
| 2622 | // parameter list is truncated on the right, so that there are |
| 2623 | // exactly m parameters. |
| 2624 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2625 | if (NumArgs < MinRequiredArgs) { |
| 2626 | // Not enough arguments. |
| 2627 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2628 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2629 | return; |
| 2630 | } |
| 2631 | |
| 2632 | Candidate.Viable = true; |
| 2633 | Candidate.Conversions.resize(NumArgs + 1); |
| 2634 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2635 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2636 | // The implicit object argument is ignored. |
| 2637 | Candidate.IgnoreObjectArgument = true; |
| 2638 | else { |
| 2639 | // Determine the implicit conversion sequence for the object |
| 2640 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2641 | Candidate.Conversions[0] |
| 2642 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2643 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2644 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2645 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2646 | return; |
| 2647 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | // Determine the implicit conversion sequences for each of the |
| 2651 | // arguments. |
| 2652 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2653 | if (ArgIdx < NumArgsInProto) { |
| 2654 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2655 | // exist for each argument an implicit conversion sequence |
| 2656 | // (13.3.3.1) that converts that argument to the corresponding |
| 2657 | // parameter of F. |
| 2658 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | Candidate.Conversions[ArgIdx + 1] |
| 2660 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2661 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2662 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2663 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2664 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2665 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2666 | break; |
| 2667 | } |
| 2668 | } else { |
| 2669 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2670 | // argument for which there is no corresponding parameter is |
| 2671 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2672 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2677 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2678 | /// set, using template argument deduction to produce an appropriate member |
| 2679 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2681 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2682 | AccessSpecifier Access, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2683 | CXXRecordDecl *ActingContext, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2684 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2685 | QualType ObjectType, |
| 2686 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2687 | OverloadCandidateSet& CandidateSet, |
| 2688 | bool SuppressUserConversions, |
| 2689 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2690 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 2691 | return; |
| 2692 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2693 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2695 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | // 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] | 2697 | // candidate functions in the usual way.113) A given name can refer to one |
| 2698 | // or more function templates and also to a set of overloaded non-template |
| 2699 | // functions. In such a case, the candidate functions generated from each |
| 2700 | // function template are combined with the set of non-template candidate |
| 2701 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2702 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2703 | FunctionDecl *Specialization = 0; |
| 2704 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2705 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2706 | Args, NumArgs, Specialization, Info)) { |
| 2707 | // FIXME: Record what happened with template argument deduction, so |
| 2708 | // that we can give the user a beautiful diagnostic. |
| 2709 | (void)Result; |
| 2710 | return; |
| 2711 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2713 | // Add the function template specialization produced by template argument |
| 2714 | // deduction as a candidate. |
| 2715 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2717 | "Specialization is not a member function?"); |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2718 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Access, |
| 2719 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2720 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2721 | } |
| 2722 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2723 | /// \brief Add a C++ function template specialization as a candidate |
| 2724 | /// in the candidate set, using template argument deduction to produce |
| 2725 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2727 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2728 | AccessSpecifier Access, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2729 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2730 | Expr **Args, unsigned NumArgs, |
| 2731 | OverloadCandidateSet& CandidateSet, |
| 2732 | bool SuppressUserConversions, |
| 2733 | bool ForceRValue) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2734 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2735 | return; |
| 2736 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2737 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2739 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2740 | // 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] | 2741 | // candidate functions in the usual way.113) A given name can refer to one |
| 2742 | // or more function templates and also to a set of overloaded non-template |
| 2743 | // functions. In such a case, the candidate functions generated from each |
| 2744 | // function template are combined with the set of non-template candidate |
| 2745 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2746 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2747 | FunctionDecl *Specialization = 0; |
| 2748 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2749 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2750 | Args, NumArgs, Specialization, Info)) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2751 | CandidateSet.push_back(OverloadCandidate()); |
| 2752 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 2753 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2754 | Candidate.Access = Access; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2755 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2756 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 2757 | Candidate.IsSurrogate = false; |
| 2758 | Candidate.IgnoreObjectArgument = false; |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 2759 | |
| 2760 | // TODO: record more information about failed template arguments |
| 2761 | Candidate.DeductionFailure.Result = Result; |
| 2762 | Candidate.DeductionFailure.TemplateParameter = Info.Param.getOpaqueValue(); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2763 | return; |
| 2764 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2766 | // Add the function template specialization produced by template argument |
| 2767 | // deduction as a candidate. |
| 2768 | assert(Specialization && "Missing function template specialization?"); |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2769 | AddOverloadCandidate(Specialization, Access, Args, NumArgs, CandidateSet, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2770 | SuppressUserConversions, ForceRValue); |
| 2771 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2773 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2774 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2775 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | /// 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] | 2777 | /// (which may or may not be the same type as the type that the |
| 2778 | /// conversion function produces). |
| 2779 | void |
| 2780 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2781 | AccessSpecifier Access, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2782 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2783 | Expr *From, QualType ToType, |
| 2784 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2785 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2786 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2787 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2788 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2789 | return; |
| 2790 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2791 | // Overload resolution is always an unevaluated context. |
| 2792 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2793 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2794 | // Add this candidate |
| 2795 | CandidateSet.push_back(OverloadCandidate()); |
| 2796 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2797 | Candidate.Function = Conversion; |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2798 | Candidate.Access = Access; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2799 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2800 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2801 | Candidate.FinalConversion.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2802 | Candidate.FinalConversion.setFromType(Conversion->getConversionType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2803 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2804 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2805 | // Determine the implicit conversion sequence for the implicit |
| 2806 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2807 | Candidate.Viable = true; |
| 2808 | Candidate.Conversions.resize(1); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2809 | Candidate.Conversions[0] |
| 2810 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 2811 | ActingContext); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2812 | // Conversion functions to a different type in the base class is visible in |
| 2813 | // the derived class. So, a derived to base conversion should not participate |
| 2814 | // in overload resolution. |
| 2815 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 2816 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2817 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2818 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2819 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2820 | return; |
| 2821 | } |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2822 | |
| 2823 | // We won't go through a user-define type conversion function to convert a |
| 2824 | // derived to base as such conversions are given Conversion Rank. They only |
| 2825 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 2826 | QualType FromCanon |
| 2827 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 2828 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 2829 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 2830 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2831 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 2832 | return; |
| 2833 | } |
| 2834 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2835 | |
| 2836 | // To determine what the conversion from the result of calling the |
| 2837 | // conversion function to the type we're eventually trying to |
| 2838 | // convert to (ToType), we need to synthesize a call to the |
| 2839 | // conversion function and attempt copy initialization from it. This |
| 2840 | // makes sure that we get the right semantics with respect to |
| 2841 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2842 | // call on the stack and we don't need its arguments to be |
| 2843 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2845 | From->getLocStart()); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2846 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2847 | CastExpr::CK_FunctionToPointerDecay, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2848 | &ConversionRef, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | |
| 2850 | // 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] | 2851 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2852 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2854 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 2855 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | ImplicitConversionSequence ICS = |
| 2857 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2858 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2859 | /*ForceRValue=*/false, |
| 2860 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2862 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2863 | case ImplicitConversionSequence::StandardConversion: |
| 2864 | Candidate.FinalConversion = ICS.Standard; |
| 2865 | break; |
| 2866 | |
| 2867 | case ImplicitConversionSequence::BadConversion: |
| 2868 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2869 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2870 | break; |
| 2871 | |
| 2872 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2874 | "Can only end up with a standard conversion sequence or failure"); |
| 2875 | } |
| 2876 | } |
| 2877 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2878 | /// \brief Adds a conversion function template specialization |
| 2879 | /// candidate to the overload set, using template argument deduction |
| 2880 | /// to deduce the template arguments of the conversion function |
| 2881 | /// template from the type that we are converting to (C++ |
| 2882 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2884 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2885 | AccessSpecifier Access, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2886 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2887 | Expr *From, QualType ToType, |
| 2888 | OverloadCandidateSet &CandidateSet) { |
| 2889 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2890 | "Only conversion function templates permitted here"); |
| 2891 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2892 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 2893 | return; |
| 2894 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2895 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2896 | CXXConversionDecl *Specialization = 0; |
| 2897 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2899 | Specialization, Info)) { |
| 2900 | // FIXME: Record what happened with template argument deduction, so |
| 2901 | // that we can give the user a beautiful diagnostic. |
| 2902 | (void)Result; |
| 2903 | return; |
| 2904 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2906 | // Add the conversion function template specialization produced by |
| 2907 | // template argument deduction as a candidate. |
| 2908 | assert(Specialization && "Missing function template specialization?"); |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2909 | AddConversionCandidate(Specialization, Access, ActingDC, From, ToType, |
| 2910 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2913 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2914 | /// converts the given @c Object to a function pointer via the |
| 2915 | /// conversion function @c Conversion, and then attempts to call it |
| 2916 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2917 | /// the type of function that we'll eventually be calling. |
| 2918 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2919 | AccessSpecifier Access, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2920 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2921 | const FunctionProtoType *Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2922 | QualType ObjectType, |
| 2923 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2924 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2925 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 2926 | return; |
| 2927 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2928 | // Overload resolution is always an unevaluated context. |
| 2929 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2930 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2931 | CandidateSet.push_back(OverloadCandidate()); |
| 2932 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2933 | Candidate.Function = 0; |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2934 | Candidate.Access = Access; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2935 | Candidate.Surrogate = Conversion; |
| 2936 | Candidate.Viable = true; |
| 2937 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2938 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2939 | Candidate.Conversions.resize(NumArgs + 1); |
| 2940 | |
| 2941 | // Determine the implicit conversion sequence for the implicit |
| 2942 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | ImplicitConversionSequence ObjectInit |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2944 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2945 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2946 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2947 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 2948 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2949 | return; |
| 2950 | } |
| 2951 | |
| 2952 | // The first conversion is actually a user-defined conversion whose |
| 2953 | // first conversion is ObjectInit's standard conversion (which is |
| 2954 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2955 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2956 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2957 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2958 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2959 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2960 | = Candidate.Conversions[0].UserDefined.Before; |
| 2961 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2962 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2964 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2965 | |
| 2966 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2967 | // parameters is viable only if it has an ellipsis in its parameter |
| 2968 | // list (8.3.5). |
| 2969 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2970 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2971 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2972 | return; |
| 2973 | } |
| 2974 | |
| 2975 | // Function types don't have any default arguments, so just check if |
| 2976 | // we have enough arguments. |
| 2977 | if (NumArgs < NumArgsInProto) { |
| 2978 | // Not enough arguments. |
| 2979 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2980 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2981 | return; |
| 2982 | } |
| 2983 | |
| 2984 | // Determine the implicit conversion sequences for each of the |
| 2985 | // arguments. |
| 2986 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2987 | if (ArgIdx < NumArgsInProto) { |
| 2988 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2989 | // exist for each argument an implicit conversion sequence |
| 2990 | // (13.3.3.1) that converts that argument to the corresponding |
| 2991 | // parameter of F. |
| 2992 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | Candidate.Conversions[ArgIdx + 1] |
| 2994 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2995 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2996 | /*ForceRValue=*/false, |
| 2997 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2998 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2999 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3000 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3001 | break; |
| 3002 | } |
| 3003 | } else { |
| 3004 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3005 | // argument for which there is no corresponding parameter is |
| 3006 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3007 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | } |
| 3011 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3012 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 3013 | // operator overloading logic over to the scheme used by binary operators, which |
| 3014 | // works for template instantiation. |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3015 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3016 | SourceLocation OpLoc, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3017 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3018 | OverloadCandidateSet& CandidateSet, |
| 3019 | SourceRange OpRange) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3020 | UnresolvedSet<16> Fns; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3021 | |
| 3022 | QualType T1 = Args[0]->getType(); |
| 3023 | QualType T2; |
| 3024 | if (NumArgs > 1) |
| 3025 | T2 = Args[1]->getType(); |
| 3026 | |
| 3027 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3028 | if (S) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3029 | LookupOverloadedOperatorName(Op, S, T1, T2, Fns); |
| 3030 | AddFunctionCandidates(Fns, Args, NumArgs, CandidateSet, false); |
| 3031 | AddArgumentDependentLookupCandidates(OpName, false, Args, NumArgs, 0, |
| 3032 | CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3033 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3034 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | /// \brief Add overload candidates for overloaded operators that are |
| 3038 | /// member functions. |
| 3039 | /// |
| 3040 | /// Add the overloaded operator candidates that are member functions |
| 3041 | /// for the operator Op that was used in an operator expression such |
| 3042 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3043 | /// CandidateSet will store the added overload candidates. (C++ |
| 3044 | /// [over.match.oper]). |
| 3045 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3046 | SourceLocation OpLoc, |
| 3047 | Expr **Args, unsigned NumArgs, |
| 3048 | OverloadCandidateSet& CandidateSet, |
| 3049 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3050 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3051 | |
| 3052 | // C++ [over.match.oper]p3: |
| 3053 | // For a unary operator @ with an operand of a type whose |
| 3054 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3055 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3056 | // a right operand of a type whose cv-unqualified version is T2, |
| 3057 | // three sets of candidate functions, designated member |
| 3058 | // candidates, non-member candidates and built-in candidates, are |
| 3059 | // constructed as follows: |
| 3060 | QualType T1 = Args[0]->getType(); |
| 3061 | QualType T2; |
| 3062 | if (NumArgs > 1) |
| 3063 | T2 = Args[1]->getType(); |
| 3064 | |
| 3065 | // -- If T1 is a class type, the set of member candidates is the |
| 3066 | // result of the qualified lookup of T1::operator@ |
| 3067 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3068 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3069 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3070 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3071 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3072 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3074 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3075 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3076 | Operators.suppressDiagnostics(); |
| 3077 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3079 | OperEnd = Operators.end(); |
| 3080 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3081 | ++Oper) |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3082 | AddMethodCandidate(*Oper, Oper.getAccess(), Args[0]->getType(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3083 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3084 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3085 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3088 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3089 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3090 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3091 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3092 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3093 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3094 | /// (at the beginning of the argument list) that will be contextually |
| 3095 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3097 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3098 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3099 | bool IsAssignmentOperator, |
| 3100 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3101 | // Overload resolution is always an unevaluated context. |
| 3102 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3103 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3104 | // Add this candidate |
| 3105 | CandidateSet.push_back(OverloadCandidate()); |
| 3106 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 3107 | Candidate.Function = 0; |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3108 | Candidate.Access = AS_none; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3109 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3110 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3111 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3112 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3113 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3114 | |
| 3115 | // Determine the implicit conversion sequences for each of the |
| 3116 | // arguments. |
| 3117 | Candidate.Viable = true; |
| 3118 | Candidate.Conversions.resize(NumArgs); |
| 3119 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3120 | // C++ [over.match.oper]p4: |
| 3121 | // For the built-in assignment operators, conversions of the |
| 3122 | // left operand are restricted as follows: |
| 3123 | // -- no temporaries are introduced to hold the left operand, and |
| 3124 | // -- no user-defined conversions are applied to the left |
| 3125 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3127 | // |
| 3128 | // We block these conversions by turning off user-defined |
| 3129 | // conversions, since that is the only way that initialization of |
| 3130 | // a reference to a non-class type can occur from something that |
| 3131 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3132 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3134 | "Contextual conversion to bool requires bool type"); |
| 3135 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3136 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | Candidate.Conversions[ArgIdx] |
| 3138 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3139 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3140 | /*ForceRValue=*/false, |
| 3141 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3142 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3143 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3144 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3145 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3146 | break; |
| 3147 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3152 | /// candidate operator functions for built-in operators (C++ |
| 3153 | /// [over.built]). The types are separated into pointer types and |
| 3154 | /// enumeration types. |
| 3155 | class BuiltinCandidateTypeSet { |
| 3156 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3157 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3158 | |
| 3159 | /// PointerTypes - The set of pointer types that will be used in the |
| 3160 | /// built-in candidates. |
| 3161 | TypeSet PointerTypes; |
| 3162 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3163 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3164 | /// used in the built-in candidates. |
| 3165 | TypeSet MemberPointerTypes; |
| 3166 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3167 | /// EnumerationTypes - The set of enumeration types that will be |
| 3168 | /// used in the built-in candidates. |
| 3169 | TypeSet EnumerationTypes; |
| 3170 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3171 | /// Sema - The semantic analysis instance where we are building the |
| 3172 | /// candidate type set. |
| 3173 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3175 | /// Context - The AST context in which we will build the type sets. |
| 3176 | ASTContext &Context; |
| 3177 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3178 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3179 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3180 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3181 | |
| 3182 | public: |
| 3183 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3184 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3185 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3187 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3188 | |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3189 | void AddTypesConvertedFrom(QualType Ty, |
| 3190 | SourceLocation Loc, |
| 3191 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3192 | bool AllowExplicitConversions, |
| 3193 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3194 | |
| 3195 | /// pointer_begin - First pointer type found; |
| 3196 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3197 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3198 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3199 | iterator pointer_end() { return PointerTypes.end(); } |
| 3200 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3201 | /// member_pointer_begin - First member pointer type found; |
| 3202 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3203 | |
| 3204 | /// member_pointer_end - Past the last member pointer type found; |
| 3205 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3206 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3207 | /// enumeration_begin - First enumeration type found; |
| 3208 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3209 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3210 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3211 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3212 | }; |
| 3213 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3214 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3215 | /// the set of pointer types along with any more-qualified variants of |
| 3216 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3217 | /// will add "int const *", "int const volatile *", "int const |
| 3218 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3219 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3220 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3221 | /// |
| 3222 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3223 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3224 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3225 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3226 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3227 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3228 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3229 | return false; |
| 3230 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3231 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3232 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3233 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3234 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3235 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3236 | // (the qualifier would sink to the element type), and for another, the |
| 3237 | // only overload situation where it matters is subscript or pointer +- int, |
| 3238 | // and those shouldn't have qualifier variants anyway. |
| 3239 | if (PointeeTy->isArrayType()) |
| 3240 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3241 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3242 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3243 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3244 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3245 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3246 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3247 | // Iterate through all strict supersets of BaseCVR. |
| 3248 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3249 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3250 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3251 | // in the types. |
| 3252 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3253 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3254 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3255 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | return true; |
| 3259 | } |
| 3260 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3261 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3262 | /// to the set of pointer types along with any more-qualified variants of |
| 3263 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3264 | /// will add "int const *", "int const volatile *", "int const |
| 3265 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3266 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3267 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3268 | /// |
| 3269 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3270 | bool |
| 3271 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3272 | QualType Ty) { |
| 3273 | // Insert this type. |
| 3274 | if (!MemberPointerTypes.insert(Ty)) |
| 3275 | return false; |
| 3276 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3277 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3278 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3279 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3280 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3281 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3282 | // (the qualifier would sink to the element type), and for another, the |
| 3283 | // only overload situation where it matters is subscript or pointer +- int, |
| 3284 | // and those shouldn't have qualifier variants anyway. |
| 3285 | if (PointeeTy->isArrayType()) |
| 3286 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3287 | const Type *ClassTy = PointerTy->getClass(); |
| 3288 | |
| 3289 | // Iterate through all strict supersets of the pointee type's CVR |
| 3290 | // qualifiers. |
| 3291 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3292 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3293 | if ((CVR | BaseCVR) != CVR) continue; |
| 3294 | |
| 3295 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3296 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | return true; |
| 3300 | } |
| 3301 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3302 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3303 | /// 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] | 3304 | /// primarily interested in pointer types and enumeration types. We also |
| 3305 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3306 | /// AllowUserConversions is true if we should look at the conversion |
| 3307 | /// functions of a class type, and AllowExplicitConversions if we |
| 3308 | /// should also include the explicit conversion functions of a class |
| 3309 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3311 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3312 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3313 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3314 | bool AllowExplicitConversions, |
| 3315 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3316 | // Only deal with canonical types. |
| 3317 | Ty = Context.getCanonicalType(Ty); |
| 3318 | |
| 3319 | // Look through reference types; they aren't part of the type of an |
| 3320 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3321 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3322 | Ty = RefTy->getPointeeType(); |
| 3323 | |
| 3324 | // We don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3325 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3326 | |
Sebastian Redl | 65ae200 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3327 | // If we're dealing with an array type, decay to the pointer. |
| 3328 | if (Ty->isArrayType()) |
| 3329 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3330 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3331 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3332 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3333 | |
| 3334 | // Insert our type, and its more-qualified variants, into the set |
| 3335 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3336 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3337 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3338 | } else if (Ty->isMemberPointerType()) { |
| 3339 | // Member pointers are far easier, since the pointee can't be converted. |
| 3340 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3341 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3342 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3343 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3344 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3345 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3346 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3347 | // No conversion functions in incomplete types. |
| 3348 | return; |
| 3349 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3350 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3351 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3352 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ae01f78 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3353 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3354 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3355 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3356 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3358 | // about which builtin types we can convert to. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3359 | if (isa<FunctionTemplateDecl>(*I)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3360 | continue; |
| 3361 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3362 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3363 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3364 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3365 | VisibleQuals); |
| 3366 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3372 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3373 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3374 | /// given type to the candidate set. |
| 3375 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3376 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3378 | unsigned NumArgs, |
| 3379 | OverloadCandidateSet &CandidateSet) { |
| 3380 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3382 | // T& operator=(T&, T) |
| 3383 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3384 | ParamTypes[1] = T; |
| 3385 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3386 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3388 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3389 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3390 | ParamTypes[0] |
| 3391 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3392 | ParamTypes[1] = T; |
| 3393 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3398 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3399 | /// 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] | 3400 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3401 | Qualifiers VRQuals; |
| 3402 | const RecordType *TyRec; |
| 3403 | if (const MemberPointerType *RHSMPType = |
| 3404 | ArgExpr->getType()->getAs<MemberPointerType>()) |
| 3405 | TyRec = cast<RecordType>(RHSMPType->getClass()); |
| 3406 | else |
| 3407 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3408 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3409 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3410 | VRQuals.addVolatile(); |
| 3411 | VRQuals.addRestrict(); |
| 3412 | return VRQuals; |
| 3413 | } |
| 3414 | |
| 3415 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3416 | if (!ClassDecl->hasDefinition()) |
| 3417 | return VRQuals; |
| 3418 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3419 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3420 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3421 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3422 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3423 | E = Conversions->end(); I != E; ++I) { |
| 3424 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(*I)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3425 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3426 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3427 | CanTy = ResTypeRef->getPointeeType(); |
| 3428 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3429 | // as see them. |
| 3430 | bool done = false; |
| 3431 | while (!done) { |
| 3432 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3433 | CanTy = ResTypePtr->getPointeeType(); |
| 3434 | else if (const MemberPointerType *ResTypeMPtr = |
| 3435 | CanTy->getAs<MemberPointerType>()) |
| 3436 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3437 | else |
| 3438 | done = true; |
| 3439 | if (CanTy.isVolatileQualified()) |
| 3440 | VRQuals.addVolatile(); |
| 3441 | if (CanTy.isRestrictQualified()) |
| 3442 | VRQuals.addRestrict(); |
| 3443 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3444 | return VRQuals; |
| 3445 | } |
| 3446 | } |
| 3447 | } |
| 3448 | return VRQuals; |
| 3449 | } |
| 3450 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3451 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3452 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3453 | /// on the operator @p Op and the arguments given. For example, if the |
| 3454 | /// operator is a binary '+', this routine might add "int |
| 3455 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3456 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3458 | SourceLocation OpLoc, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3459 | Expr **Args, unsigned NumArgs, |
| 3460 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3461 | // The set of "promoted arithmetic types", which are the arithmetic |
| 3462 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 3463 | // that the first few of these types are the promoted integral |
| 3464 | // types; these types need to be first. |
| 3465 | // FIXME: What about complex? |
| 3466 | const unsigned FirstIntegralType = 0; |
| 3467 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3469 | LastPromotedIntegralType = 13; |
| 3470 | const unsigned FirstPromotedArithmeticType = 7, |
| 3471 | LastPromotedArithmeticType = 16; |
| 3472 | const unsigned NumArithmeticTypes = 16; |
| 3473 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3475 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3476 | Context.SignedCharTy, Context.ShortTy, |
| 3477 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3478 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3479 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3480 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3481 | }; |
Douglas Gregor | b8440a7 | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 3482 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 3483 | "Invalid first promoted integral type"); |
| 3484 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 3485 | == Context.UnsignedLongLongTy && |
| 3486 | "Invalid last promoted integral type"); |
| 3487 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 3488 | "Invalid first promoted arithmetic type"); |
| 3489 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 3490 | == Context.LongDoubleTy && |
| 3491 | "Invalid last promoted arithmetic type"); |
| 3492 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3493 | // Find all of the types that the arguments can convert to, but only |
| 3494 | // if the operator we're looking at has built-in operator candidates |
| 3495 | // that make use of these types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3496 | Qualifiers VisibleTypeConversionsQuals; |
| 3497 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3498 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3499 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 3500 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3501 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3502 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3503 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3504 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3505 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3506 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3507 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3508 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3509 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3510 | OpLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3511 | true, |
| 3512 | (Op == OO_Exclaim || |
| 3513 | Op == OO_AmpAmp || |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3514 | Op == OO_PipePipe), |
| 3515 | VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | bool isComparison = false; |
| 3519 | switch (Op) { |
| 3520 | case OO_None: |
| 3521 | case NUM_OVERLOADED_OPERATORS: |
| 3522 | assert(false && "Expected an overloaded operator"); |
| 3523 | break; |
| 3524 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3525 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3526 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3527 | goto UnaryStar; |
| 3528 | else |
| 3529 | goto BinaryStar; |
| 3530 | break; |
| 3531 | |
| 3532 | case OO_Plus: // '+' is either unary or binary |
| 3533 | if (NumArgs == 1) |
| 3534 | goto UnaryPlus; |
| 3535 | else |
| 3536 | goto BinaryPlus; |
| 3537 | break; |
| 3538 | |
| 3539 | case OO_Minus: // '-' is either unary or binary |
| 3540 | if (NumArgs == 1) |
| 3541 | goto UnaryMinus; |
| 3542 | else |
| 3543 | goto BinaryMinus; |
| 3544 | break; |
| 3545 | |
| 3546 | case OO_Amp: // '&' is either unary or binary |
| 3547 | if (NumArgs == 1) |
| 3548 | goto UnaryAmp; |
| 3549 | else |
| 3550 | goto BinaryAmp; |
| 3551 | |
| 3552 | case OO_PlusPlus: |
| 3553 | case OO_MinusMinus: |
| 3554 | // C++ [over.built]p3: |
| 3555 | // |
| 3556 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3557 | // is either volatile or empty, there exist candidate operator |
| 3558 | // functions of the form |
| 3559 | // |
| 3560 | // VQ T& operator++(VQ T&); |
| 3561 | // T operator++(VQ T&, int); |
| 3562 | // |
| 3563 | // C++ [over.built]p4: |
| 3564 | // |
| 3565 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3566 | // than bool, and VQ is either volatile or empty, there exist |
| 3567 | // candidate operator functions of the form |
| 3568 | // |
| 3569 | // VQ T& operator--(VQ T&); |
| 3570 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3572 | Arith < NumArithmeticTypes; ++Arith) { |
| 3573 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3575 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3576 | |
| 3577 | // Non-volatile version. |
| 3578 | if (NumArgs == 1) |
| 3579 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3580 | else |
| 3581 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3582 | // heuristic to reduce number of builtin candidates in the set. |
| 3583 | // Add volatile version only if there are conversions to a volatile type. |
| 3584 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3585 | // Volatile version |
| 3586 | ParamTypes[0] |
| 3587 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 3588 | if (NumArgs == 1) |
| 3589 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3590 | else |
| 3591 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3592 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | // C++ [over.built]p5: |
| 3596 | // |
| 3597 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3598 | // cv-unqualified object type, and VQ is either volatile or |
| 3599 | // empty, there exist candidate operator functions of the form |
| 3600 | // |
| 3601 | // T*VQ& operator++(T*VQ&); |
| 3602 | // T*VQ& operator--(T*VQ&); |
| 3603 | // T* operator++(T*VQ&, int); |
| 3604 | // T* operator--(T*VQ&, int); |
| 3605 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3606 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3607 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3608 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3609 | continue; |
| 3610 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | QualType ParamTypes[2] = { |
| 3612 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3613 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3615 | // Without volatile |
| 3616 | if (NumArgs == 1) |
| 3617 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3618 | else |
| 3619 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3620 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3621 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3622 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3623 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3624 | ParamTypes[0] |
| 3625 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3626 | if (NumArgs == 1) |
| 3627 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3628 | else |
| 3629 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3630 | } |
| 3631 | } |
| 3632 | break; |
| 3633 | |
| 3634 | UnaryStar: |
| 3635 | // C++ [over.built]p6: |
| 3636 | // For every cv-qualified or cv-unqualified object type T, there |
| 3637 | // exist candidate operator functions of the form |
| 3638 | // |
| 3639 | // T& operator*(T*); |
| 3640 | // |
| 3641 | // C++ [over.built]p7: |
| 3642 | // For every function type T, there exist candidate operator |
| 3643 | // functions of the form |
| 3644 | // T& operator*(T*); |
| 3645 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3646 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3647 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3648 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3650 | &ParamTy, Args, 1, CandidateSet); |
| 3651 | } |
| 3652 | break; |
| 3653 | |
| 3654 | UnaryPlus: |
| 3655 | // C++ [over.built]p8: |
| 3656 | // For every type T, there exist candidate operator functions of |
| 3657 | // the form |
| 3658 | // |
| 3659 | // T* operator+(T*); |
| 3660 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3661 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3662 | QualType ParamTy = *Ptr; |
| 3663 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3664 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3666 | // Fall through |
| 3667 | |
| 3668 | UnaryMinus: |
| 3669 | // C++ [over.built]p9: |
| 3670 | // For every promoted arithmetic type T, there exist candidate |
| 3671 | // operator functions of the form |
| 3672 | // |
| 3673 | // T operator+(T); |
| 3674 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3676 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3677 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3678 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3679 | } |
| 3680 | break; |
| 3681 | |
| 3682 | case OO_Tilde: |
| 3683 | // C++ [over.built]p10: |
| 3684 | // For every promoted integral type T, there exist candidate |
| 3685 | // operator functions of the form |
| 3686 | // |
| 3687 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3688 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3689 | Int < LastPromotedIntegralType; ++Int) { |
| 3690 | QualType IntTy = ArithmeticTypes[Int]; |
| 3691 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3692 | } |
| 3693 | break; |
| 3694 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3695 | case OO_New: |
| 3696 | case OO_Delete: |
| 3697 | case OO_Array_New: |
| 3698 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3699 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3700 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | |
| 3703 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3704 | UnaryAmp: |
| 3705 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3706 | // C++ [over.match.oper]p3: |
| 3707 | // -- For the operator ',', the unary operator '&', or the |
| 3708 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3711 | case OO_EqualEqual: |
| 3712 | case OO_ExclaimEqual: |
| 3713 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | // For every pointer to member type T, there exist candidate operator |
| 3715 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3716 | // |
| 3717 | // bool operator==(T,T); |
| 3718 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3720 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3721 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3722 | MemPtr != MemPtrEnd; |
| 3723 | ++MemPtr) { |
| 3724 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3725 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3726 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3728 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3730 | case OO_Less: |
| 3731 | case OO_Greater: |
| 3732 | case OO_LessEqual: |
| 3733 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3734 | // C++ [over.built]p15: |
| 3735 | // |
| 3736 | // For every pointer or enumeration type T, there exist |
| 3737 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3739 | // bool operator<(T, T); |
| 3740 | // bool operator>(T, T); |
| 3741 | // bool operator<=(T, T); |
| 3742 | // bool operator>=(T, T); |
| 3743 | // bool operator==(T, T); |
| 3744 | // bool operator!=(T, T); |
| 3745 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3746 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3747 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3748 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3749 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3751 | = CandidateTypes.enumeration_begin(); |
| 3752 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3753 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3754 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3755 | } |
| 3756 | |
| 3757 | // Fall through. |
| 3758 | isComparison = true; |
| 3759 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3760 | BinaryPlus: |
| 3761 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3762 | if (!isComparison) { |
| 3763 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3764 | |
| 3765 | // C++ [over.built]p13: |
| 3766 | // |
| 3767 | // For every cv-qualified or cv-unqualified object type T |
| 3768 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3770 | // T* operator+(T*, ptrdiff_t); |
| 3771 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3772 | // T* operator-(T*, ptrdiff_t); |
| 3773 | // T* operator+(ptrdiff_t, T*); |
| 3774 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3775 | // |
| 3776 | // C++ [over.built]p14: |
| 3777 | // |
| 3778 | // For every T, where T is a pointer to object type, there |
| 3779 | // exist candidate operator functions of the form |
| 3780 | // |
| 3781 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3783 | = CandidateTypes.pointer_begin(); |
| 3784 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3785 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3786 | |
| 3787 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3788 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3789 | |
| 3790 | if (Op == OO_Plus) { |
| 3791 | // T* operator+(ptrdiff_t, T*); |
| 3792 | ParamTypes[0] = ParamTypes[1]; |
| 3793 | ParamTypes[1] = *Ptr; |
| 3794 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3795 | } else { |
| 3796 | // ptrdiff_t operator-(T, T); |
| 3797 | ParamTypes[1] = *Ptr; |
| 3798 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3799 | Args, 2, CandidateSet); |
| 3800 | } |
| 3801 | } |
| 3802 | } |
| 3803 | // Fall through |
| 3804 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3805 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3806 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3807 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3808 | // C++ [over.built]p12: |
| 3809 | // |
| 3810 | // For every pair of promoted arithmetic types L and R, there |
| 3811 | // exist candidate operator functions of the form |
| 3812 | // |
| 3813 | // LR operator*(L, R); |
| 3814 | // LR operator/(L, R); |
| 3815 | // LR operator+(L, R); |
| 3816 | // LR operator-(L, R); |
| 3817 | // bool operator<(L, R); |
| 3818 | // bool operator>(L, R); |
| 3819 | // bool operator<=(L, R); |
| 3820 | // bool operator>=(L, R); |
| 3821 | // bool operator==(L, R); |
| 3822 | // bool operator!=(L, R); |
| 3823 | // |
| 3824 | // where LR is the result of the usual arithmetic conversions |
| 3825 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3826 | // |
| 3827 | // C++ [over.built]p24: |
| 3828 | // |
| 3829 | // For every pair of promoted arithmetic types L and R, there exist |
| 3830 | // candidate operator functions of the form |
| 3831 | // |
| 3832 | // LR operator?(bool, L, R); |
| 3833 | // |
| 3834 | // where LR is the result of the usual arithmetic conversions |
| 3835 | // between types L and R. |
| 3836 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3838 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3840 | Right < LastPromotedArithmeticType; ++Right) { |
| 3841 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3842 | QualType Result |
| 3843 | = isComparison |
| 3844 | ? Context.BoolTy |
| 3845 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3846 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3847 | } |
| 3848 | } |
| 3849 | break; |
| 3850 | |
| 3851 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3852 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3853 | case OO_Caret: |
| 3854 | case OO_Pipe: |
| 3855 | case OO_LessLess: |
| 3856 | case OO_GreaterGreater: |
| 3857 | // C++ [over.built]p17: |
| 3858 | // |
| 3859 | // For every pair of promoted integral types L and R, there |
| 3860 | // exist candidate operator functions of the form |
| 3861 | // |
| 3862 | // LR operator%(L, R); |
| 3863 | // LR operator&(L, R); |
| 3864 | // LR operator^(L, R); |
| 3865 | // LR operator|(L, R); |
| 3866 | // L operator<<(L, R); |
| 3867 | // L operator>>(L, R); |
| 3868 | // |
| 3869 | // where LR is the result of the usual arithmetic conversions |
| 3870 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3872 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3874 | Right < LastPromotedIntegralType; ++Right) { |
| 3875 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3876 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3877 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3878 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3879 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3880 | } |
| 3881 | } |
| 3882 | break; |
| 3883 | |
| 3884 | case OO_Equal: |
| 3885 | // C++ [over.built]p20: |
| 3886 | // |
| 3887 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3888 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3889 | // empty, there exist candidate operator functions of the form |
| 3890 | // |
| 3891 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3892 | for (BuiltinCandidateTypeSet::iterator |
| 3893 | Enum = CandidateTypes.enumeration_begin(), |
| 3894 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3895 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3897 | CandidateSet); |
| 3898 | for (BuiltinCandidateTypeSet::iterator |
| 3899 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3900 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3901 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3902 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3903 | CandidateSet); |
| 3904 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3905 | |
| 3906 | case OO_PlusEqual: |
| 3907 | case OO_MinusEqual: |
| 3908 | // C++ [over.built]p19: |
| 3909 | // |
| 3910 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3911 | // volatile or empty, there exist candidate operator functions |
| 3912 | // of the form |
| 3913 | // |
| 3914 | // T*VQ& operator=(T*VQ&, T*); |
| 3915 | // |
| 3916 | // C++ [over.built]p21: |
| 3917 | // |
| 3918 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3919 | // cv-unqualified object type and VQ is either volatile or |
| 3920 | // empty, there exist candidate operator functions of the form |
| 3921 | // |
| 3922 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3923 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3924 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3925 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3926 | QualType ParamTypes[2]; |
| 3927 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3928 | |
| 3929 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3930 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3931 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3932 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3933 | |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3934 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 3935 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3936 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3937 | ParamTypes[0] |
| 3938 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3939 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3940 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3941 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3942 | } |
| 3943 | // Fall through. |
| 3944 | |
| 3945 | case OO_StarEqual: |
| 3946 | case OO_SlashEqual: |
| 3947 | // C++ [over.built]p18: |
| 3948 | // |
| 3949 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3950 | // VQ is either volatile or empty, and R is a promoted |
| 3951 | // arithmetic type, there exist candidate operator functions of |
| 3952 | // the form |
| 3953 | // |
| 3954 | // VQ L& operator=(VQ L&, R); |
| 3955 | // VQ L& operator*=(VQ L&, R); |
| 3956 | // VQ L& operator/=(VQ L&, R); |
| 3957 | // VQ L& operator+=(VQ L&, R); |
| 3958 | // VQ L& operator-=(VQ L&, R); |
| 3959 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3961 | Right < LastPromotedArithmeticType; ++Right) { |
| 3962 | QualType ParamTypes[2]; |
| 3963 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3964 | |
| 3965 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3966 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3967 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3968 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3969 | |
| 3970 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 3971 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 3972 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 3973 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 3974 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3975 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 3976 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3977 | } |
| 3978 | } |
| 3979 | break; |
| 3980 | |
| 3981 | case OO_PercentEqual: |
| 3982 | case OO_LessLessEqual: |
| 3983 | case OO_GreaterGreaterEqual: |
| 3984 | case OO_AmpEqual: |
| 3985 | case OO_CaretEqual: |
| 3986 | case OO_PipeEqual: |
| 3987 | // C++ [over.built]p22: |
| 3988 | // |
| 3989 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3990 | // is either volatile or empty, and R is a promoted integral |
| 3991 | // type, there exist candidate operator functions of the form |
| 3992 | // |
| 3993 | // VQ L& operator%=(VQ L&, R); |
| 3994 | // VQ L& operator<<=(VQ L&, R); |
| 3995 | // VQ L& operator>>=(VQ L&, R); |
| 3996 | // VQ L& operator&=(VQ L&, R); |
| 3997 | // VQ L& operator^=(VQ L&, R); |
| 3998 | // VQ L& operator|=(VQ L&, R); |
| 3999 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4001 | Right < LastPromotedIntegralType; ++Right) { |
| 4002 | QualType ParamTypes[2]; |
| 4003 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4004 | |
| 4005 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4006 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4007 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a4a9334 | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4008 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4009 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4010 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4011 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4012 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4013 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4014 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | break; |
| 4018 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4019 | case OO_Exclaim: { |
| 4020 | // C++ [over.operator]p23: |
| 4021 | // |
| 4022 | // There also exist candidate operator functions of the form |
| 4023 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4024 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4025 | // bool operator&&(bool, bool); [BELOW] |
| 4026 | // bool operator||(bool, bool); [BELOW] |
| 4027 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4028 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4029 | /*IsAssignmentOperator=*/false, |
| 4030 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4031 | break; |
| 4032 | } |
| 4033 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4034 | case OO_AmpAmp: |
| 4035 | case OO_PipePipe: { |
| 4036 | // C++ [over.operator]p23: |
| 4037 | // |
| 4038 | // There also exist candidate operator functions of the form |
| 4039 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4040 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4041 | // bool operator&&(bool, bool); |
| 4042 | // bool operator||(bool, bool); |
| 4043 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4044 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4045 | /*IsAssignmentOperator=*/false, |
| 4046 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4047 | break; |
| 4048 | } |
| 4049 | |
| 4050 | case OO_Subscript: |
| 4051 | // C++ [over.built]p13: |
| 4052 | // |
| 4053 | // For every cv-qualified or cv-unqualified object type T there |
| 4054 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4056 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4057 | // T& operator[](T*, ptrdiff_t); |
| 4058 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4059 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4060 | // T& operator[](ptrdiff_t, T*); |
| 4061 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4062 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4063 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4064 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4065 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4066 | |
| 4067 | // T& operator[](T*, ptrdiff_t) |
| 4068 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4069 | |
| 4070 | // T& operator[](ptrdiff_t, T*); |
| 4071 | ParamTypes[0] = ParamTypes[1]; |
| 4072 | ParamTypes[1] = *Ptr; |
| 4073 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4074 | } |
| 4075 | break; |
| 4076 | |
| 4077 | case OO_ArrowStar: |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4078 | // C++ [over.built]p11: |
| 4079 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4080 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4081 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4082 | // there exist candidate operator functions of the form |
| 4083 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4084 | // where CV12 is the union of CV1 and CV2. |
| 4085 | { |
| 4086 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4087 | CandidateTypes.pointer_begin(); |
| 4088 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4089 | QualType C1Ty = (*Ptr); |
| 4090 | QualType C1; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4091 | QualifierCollector Q1; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4092 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4093 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4094 | if (!isa<RecordType>(C1)) |
| 4095 | continue; |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4096 | // heuristic to reduce number of builtin candidates in the set. |
| 4097 | // Add volatile/restrict version only if there are conversions to a |
| 4098 | // volatile/restrict type. |
| 4099 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4100 | continue; |
| 4101 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4102 | continue; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4103 | } |
| 4104 | for (BuiltinCandidateTypeSet::iterator |
| 4105 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4106 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4107 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4108 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4109 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 12df37c | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4110 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4111 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4112 | break; |
| 4113 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4114 | // build CV12 T& |
| 4115 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4116 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4117 | T.isVolatileQualified()) |
| 4118 | continue; |
| 4119 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4120 | T.isRestrictQualified()) |
| 4121 | continue; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4122 | T = Q1.apply(T); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4123 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4124 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4125 | } |
| 4126 | } |
| 4127 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4128 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4129 | |
| 4130 | case OO_Conditional: |
| 4131 | // Note that we don't consider the first argument, since it has been |
| 4132 | // contextually converted to bool long ago. The candidates below are |
| 4133 | // therefore added as binary. |
| 4134 | // |
| 4135 | // C++ [over.built]p24: |
| 4136 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4137 | // there exist candidate operator functions of the form |
| 4138 | // |
| 4139 | // T operator?(bool, T, T); |
| 4140 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4141 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4142 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4143 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4144 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4145 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4146 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4147 | CandidateTypes.member_pointer_begin(), |
| 4148 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4149 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4150 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4151 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4152 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4156 | /// \brief Add function candidates found via argument-dependent lookup |
| 4157 | /// to the set of overloading candidates. |
| 4158 | /// |
| 4159 | /// This routine performs argument-dependent name lookup based on the |
| 4160 | /// given function name (which may also be an operator name) and adds |
| 4161 | /// all of the overload candidates found by ADL to the overload |
| 4162 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4164 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4165 | bool Operator, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4166 | Expr **Args, unsigned NumArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4167 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4168 | OverloadCandidateSet& CandidateSet, |
| 4169 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4170 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4171 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 4172 | // FIXME: This approach for uniquing ADL results (and removing |
| 4173 | // redundant candidates from the set) relies on pointer-equality, |
| 4174 | // which means we need to key off the canonical decl. However, |
| 4175 | // always going back to the canonical decl might not get us the |
| 4176 | // right set of default arguments. What default arguments are |
| 4177 | // we supposed to consider on ADL candidates, anyway? |
| 4178 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4179 | // FIXME: Pass in the explicit template arguments? |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4180 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4181 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4182 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4183 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4184 | CandEnd = CandidateSet.end(); |
| 4185 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4186 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4187 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4188 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4189 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4190 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4191 | |
| 4192 | // For each of the ADL candidates we found, add it to the overload |
| 4193 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4194 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4195 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4196 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4197 | continue; |
| 4198 | |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4199 | AddOverloadCandidate(FD, AS_none, Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4200 | false, false, PartialOverloading); |
| 4201 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4202 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4203 | AS_none, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4204 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4205 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4208 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4209 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4211 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4212 | const OverloadCandidate& Cand2, |
| 4213 | SourceLocation Loc) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4214 | // Define viable functions to be better candidates than non-viable |
| 4215 | // functions. |
| 4216 | if (!Cand2.Viable) |
| 4217 | return Cand1.Viable; |
| 4218 | else if (!Cand1.Viable) |
| 4219 | return false; |
| 4220 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4221 | // C++ [over.match.best]p1: |
| 4222 | // |
| 4223 | // -- if F is a static member function, ICS1(F) is defined such |
| 4224 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4225 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4226 | // better nor worse than ICS1(F). |
| 4227 | unsigned StartArg = 0; |
| 4228 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4229 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4231 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4232 | // A viable function F1 is defined to be a better function than another |
| 4233 | // 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] | 4234 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4235 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4236 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4237 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4238 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4239 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4240 | Cand2.Conversions[ArgIdx])) { |
| 4241 | case ImplicitConversionSequence::Better: |
| 4242 | // Cand1 has a better conversion sequence. |
| 4243 | HasBetterConversion = true; |
| 4244 | break; |
| 4245 | |
| 4246 | case ImplicitConversionSequence::Worse: |
| 4247 | // Cand1 can't be better than Cand2. |
| 4248 | return false; |
| 4249 | |
| 4250 | case ImplicitConversionSequence::Indistinguishable: |
| 4251 | // Do nothing. |
| 4252 | break; |
| 4253 | } |
| 4254 | } |
| 4255 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4257 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4258 | if (HasBetterConversion) |
| 4259 | return true; |
| 4260 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4262 | // specialization, or, if not that, |
| 4263 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4264 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4265 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4266 | |
| 4267 | // -- F1 and F2 are function template specializations, and the function |
| 4268 | // template for F1 is more specialized than the template for F2 |
| 4269 | // 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] | 4270 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4271 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4272 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4273 | if (FunctionTemplateDecl *BetterTemplate |
| 4274 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4275 | Cand2.Function->getPrimaryTemplate(), |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4276 | Loc, |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4277 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4278 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4279 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4281 | // -- the context is an initialization by user-defined conversion |
| 4282 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4283 | // from the return type of F1 to the destination type (i.e., |
| 4284 | // the type of the entity being initialized) is a better |
| 4285 | // conversion sequence than the standard conversion sequence |
| 4286 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | if (Cand1.Function && Cand2.Function && |
| 4288 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4289 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4290 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4291 | Cand2.FinalConversion)) { |
| 4292 | case ImplicitConversionSequence::Better: |
| 4293 | // Cand1 has a better conversion sequence. |
| 4294 | return true; |
| 4295 | |
| 4296 | case ImplicitConversionSequence::Worse: |
| 4297 | // Cand1 can't be better than Cand2. |
| 4298 | return false; |
| 4299 | |
| 4300 | case ImplicitConversionSequence::Indistinguishable: |
| 4301 | // Do nothing |
| 4302 | break; |
| 4303 | } |
| 4304 | } |
| 4305 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4306 | return false; |
| 4307 | } |
| 4308 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4310 | /// within an overload candidate set. |
| 4311 | /// |
| 4312 | /// \param CandidateSet the set of candidate functions. |
| 4313 | /// |
| 4314 | /// \param Loc the location of the function name (or operator symbol) for |
| 4315 | /// which overload resolution occurs. |
| 4316 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4318 | /// function, Best points to the candidate function found. |
| 4319 | /// |
| 4320 | /// \returns The result of overload resolution. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4321 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4322 | SourceLocation Loc, |
| 4323 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4324 | // Find the best viable function. |
| 4325 | Best = CandidateSet.end(); |
| 4326 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4327 | Cand != CandidateSet.end(); ++Cand) { |
| 4328 | if (Cand->Viable) { |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4329 | if (Best == CandidateSet.end() || |
| 4330 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4331 | Best = Cand; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | // If we didn't find any viable functions, abort. |
| 4336 | if (Best == CandidateSet.end()) |
| 4337 | return OR_No_Viable_Function; |
| 4338 | |
| 4339 | // Make sure that this function is better than every other viable |
| 4340 | // function. If not, we have an ambiguity. |
| 4341 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4342 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4343 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4344 | Cand != Best && |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4345 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4346 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4347 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4348 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4349 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4351 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4352 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4353 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4354 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4355 | return OR_Deleted; |
| 4356 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4357 | // C++ [basic.def.odr]p2: |
| 4358 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4360 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4361 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4362 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4363 | if (Best->Function) |
| 4364 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4365 | return OR_Success; |
| 4366 | } |
| 4367 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4368 | namespace { |
| 4369 | |
| 4370 | enum OverloadCandidateKind { |
| 4371 | oc_function, |
| 4372 | oc_method, |
| 4373 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4374 | oc_function_template, |
| 4375 | oc_method_template, |
| 4376 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4377 | oc_implicit_default_constructor, |
| 4378 | oc_implicit_copy_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4379 | oc_implicit_copy_assignment |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4380 | }; |
| 4381 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4382 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4383 | FunctionDecl *Fn, |
| 4384 | std::string &Description) { |
| 4385 | bool isTemplate = false; |
| 4386 | |
| 4387 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4388 | isTemplate = true; |
| 4389 | Description = S.getTemplateArgumentBindingsText( |
| 4390 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4391 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4392 | |
| 4393 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4394 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4395 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4396 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4397 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4398 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4402 | // This actually gets spelled 'candidate function' for now, but |
| 4403 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4404 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4405 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4406 | |
| 4407 | assert(Meth->isCopyAssignment() |
| 4408 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4409 | return oc_implicit_copy_assignment; |
| 4410 | } |
| 4411 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4412 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | } // end anonymous namespace |
| 4416 | |
| 4417 | // Notes the location of an overload candidate. |
| 4418 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4419 | std::string FnDesc; |
| 4420 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4421 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4422 | << (unsigned) K << FnDesc; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4425 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4426 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4427 | /// target types of the conversion. |
| 4428 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4429 | SourceLocation CaretLoc, |
| 4430 | const PartialDiagnostic &PDiag) { |
| 4431 | Diag(CaretLoc, PDiag) |
| 4432 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4433 | for (AmbiguousConversionSequence::const_iterator |
| 4434 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4435 | NoteOverloadCandidate(*I); |
| 4436 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4439 | namespace { |
| 4440 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4441 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4442 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4443 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4444 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4445 | FunctionDecl *Fn = Cand->Function; |
| 4446 | |
| 4447 | // There's a conversion slot for the object argument if this is a |
| 4448 | // non-constructor method. Note that 'I' corresponds the |
| 4449 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4450 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4451 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4452 | if (I == 0) |
| 4453 | isObjectArgument = true; |
| 4454 | else |
| 4455 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4458 | std::string FnDesc; |
| 4459 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 4460 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4461 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 4462 | QualType FromTy = Conv.Bad.getFromType(); |
| 4463 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4464 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4465 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4466 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4467 | Expr *E = FromExpr->IgnoreParens(); |
| 4468 | if (isa<UnaryOperator>(E)) |
| 4469 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4470 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 4471 | |
| 4472 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 4473 | << (unsigned) FnKind << FnDesc |
| 4474 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4475 | << ToTy << Name << I+1; |
| 4476 | return; |
| 4477 | } |
| 4478 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4479 | // Do some hand-waving analysis to see if the non-viability is due |
| 4480 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4481 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 4482 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 4483 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 4484 | CToTy = RT->getPointeeType(); |
| 4485 | else { |
| 4486 | // TODO: detect and diagnose the full richness of const mismatches. |
| 4487 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 4488 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 4489 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 4490 | } |
| 4491 | |
| 4492 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 4493 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 4494 | // It is dumb that we have to do this here. |
| 4495 | while (isa<ArrayType>(CFromTy)) |
| 4496 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4497 | while (isa<ArrayType>(CToTy)) |
| 4498 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 4499 | |
| 4500 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 4501 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 4502 | |
| 4503 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 4504 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 4505 | << (unsigned) FnKind << FnDesc |
| 4506 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4507 | << FromTy |
| 4508 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 4509 | << (unsigned) isObjectArgument << I+1; |
| 4510 | return; |
| 4511 | } |
| 4512 | |
| 4513 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 4514 | assert(CVR && "unexpected qualifiers mismatch"); |
| 4515 | |
| 4516 | if (isObjectArgument) { |
| 4517 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 4518 | << (unsigned) FnKind << FnDesc |
| 4519 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4520 | << FromTy << (CVR - 1); |
| 4521 | } else { |
| 4522 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 4523 | << (unsigned) FnKind << FnDesc |
| 4524 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4525 | << FromTy << (CVR - 1) << I+1; |
| 4526 | } |
| 4527 | return; |
| 4528 | } |
| 4529 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 4530 | // Diagnose references or pointers to incomplete types differently, |
| 4531 | // since it's far from impossible that the incompleteness triggered |
| 4532 | // the failure. |
| 4533 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 4534 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 4535 | TempFromTy = PTy->getPointeeType(); |
| 4536 | if (TempFromTy->isIncompleteType()) { |
| 4537 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 4538 | << (unsigned) FnKind << FnDesc |
| 4539 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 4540 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 4541 | return; |
| 4542 | } |
| 4543 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 4544 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4545 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 4546 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4547 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 4548 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 4552 | unsigned NumFormalArgs) { |
| 4553 | // TODO: treat calls to a missing default constructor as a special case |
| 4554 | |
| 4555 | FunctionDecl *Fn = Cand->Function; |
| 4556 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 4557 | |
| 4558 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 4559 | |
| 4560 | // at least / at most / exactly |
| 4561 | unsigned mode, modeCount; |
| 4562 | if (NumFormalArgs < MinParams) { |
| 4563 | assert(Cand->FailureKind == ovl_fail_too_few_arguments); |
| 4564 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 4565 | mode = 0; // "at least" |
| 4566 | else |
| 4567 | mode = 2; // "exactly" |
| 4568 | modeCount = MinParams; |
| 4569 | } else { |
| 4570 | assert(Cand->FailureKind == ovl_fail_too_many_arguments); |
| 4571 | if (MinParams != FnTy->getNumArgs()) |
| 4572 | mode = 1; // "at most" |
| 4573 | else |
| 4574 | mode = 2; // "exactly" |
| 4575 | modeCount = FnTy->getNumArgs(); |
| 4576 | } |
| 4577 | |
| 4578 | std::string Description; |
| 4579 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 4580 | |
| 4581 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
| 4582 | << (unsigned) FnKind << Description << mode << modeCount << NumFormalArgs; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4585 | /// Diagnose a failed template-argument deduction. |
| 4586 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 4587 | Expr **Args, unsigned NumArgs) { |
| 4588 | FunctionDecl *Fn = Cand->Function; // pattern |
| 4589 | |
| 4590 | TemplateParameter Param = TemplateParameter::getFromOpaqueValue( |
| 4591 | Cand->DeductionFailure.TemplateParameter); |
| 4592 | |
| 4593 | switch (Cand->DeductionFailure.Result) { |
| 4594 | case Sema::TDK_Success: |
| 4595 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 4596 | |
| 4597 | case Sema::TDK_Incomplete: { |
| 4598 | NamedDecl *ParamD; |
| 4599 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 4600 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 4601 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
| 4602 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 4603 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 4604 | << ParamD->getDeclName(); |
| 4605 | return; |
| 4606 | } |
| 4607 | |
| 4608 | // TODO: diagnose these individually, then kill off |
| 4609 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
| 4610 | case Sema::TDK_InstantiationDepth: |
| 4611 | case Sema::TDK_Inconsistent: |
| 4612 | case Sema::TDK_InconsistentQuals: |
| 4613 | case Sema::TDK_SubstitutionFailure: |
| 4614 | case Sema::TDK_NonDeducedMismatch: |
| 4615 | case Sema::TDK_TooManyArguments: |
| 4616 | case Sema::TDK_TooFewArguments: |
| 4617 | case Sema::TDK_InvalidExplicitArguments: |
| 4618 | case Sema::TDK_FailedOverloadResolution: |
| 4619 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 4620 | return; |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 4625 | /// already generated a primary error at the call site. |
| 4626 | /// |
| 4627 | /// It really does need to be a single diagnostic with its caret |
| 4628 | /// pointed at the candidate declaration. Yes, this creates some |
| 4629 | /// major challenges of technical writing. Yes, this makes pointing |
| 4630 | /// out problems with specific arguments quite awkward. It's still |
| 4631 | /// better than generating twenty screens of text for every failed |
| 4632 | /// overload. |
| 4633 | /// |
| 4634 | /// It would be great to be able to express per-candidate problems |
| 4635 | /// more richly for those diagnostic clients that cared, but we'd |
| 4636 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4637 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 4638 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4639 | FunctionDecl *Fn = Cand->Function; |
| 4640 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4641 | // Note deleted candidates, but only if they're viable. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4642 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4643 | std::string FnDesc; |
| 4644 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4645 | |
| 4646 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4647 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4648 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4651 | // We don't really have anything else to say about viable candidates. |
| 4652 | if (Cand->Viable) { |
| 4653 | S.NoteOverloadCandidate(Fn); |
| 4654 | return; |
| 4655 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4656 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4657 | switch (Cand->FailureKind) { |
| 4658 | case ovl_fail_too_many_arguments: |
| 4659 | case ovl_fail_too_few_arguments: |
| 4660 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4661 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4662 | case ovl_fail_bad_deduction: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 4663 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 4664 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4665 | case ovl_fail_trivial_conversion: |
| 4666 | case ovl_fail_bad_final_conversion: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4667 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4668 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4669 | case ovl_fail_bad_conversion: { |
| 4670 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 4671 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4672 | if (Cand->Conversions[I].isBad()) |
| 4673 | return DiagnoseBadConversion(S, Cand, I); |
| 4674 | |
| 4675 | // FIXME: this currently happens when we're called from SemaInit |
| 4676 | // when user-conversion overload fails. Figure out how to handle |
| 4677 | // those conditions and diagnose them well. |
| 4678 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4679 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4680 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 4684 | // Desugar the type of the surrogate down to a function type, |
| 4685 | // retaining as many typedefs as possible while still showing |
| 4686 | // the function type (and, therefore, its parameter types). |
| 4687 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 4688 | bool isLValueReference = false; |
| 4689 | bool isRValueReference = false; |
| 4690 | bool isPointer = false; |
| 4691 | if (const LValueReferenceType *FnTypeRef = |
| 4692 | FnType->getAs<LValueReferenceType>()) { |
| 4693 | FnType = FnTypeRef->getPointeeType(); |
| 4694 | isLValueReference = true; |
| 4695 | } else if (const RValueReferenceType *FnTypeRef = |
| 4696 | FnType->getAs<RValueReferenceType>()) { |
| 4697 | FnType = FnTypeRef->getPointeeType(); |
| 4698 | isRValueReference = true; |
| 4699 | } |
| 4700 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 4701 | FnType = FnTypePtr->getPointeeType(); |
| 4702 | isPointer = true; |
| 4703 | } |
| 4704 | // Desugar down to a function type. |
| 4705 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 4706 | // Reconstruct the pointer/reference as appropriate. |
| 4707 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 4708 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 4709 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 4710 | |
| 4711 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 4712 | << FnType; |
| 4713 | } |
| 4714 | |
| 4715 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 4716 | const char *Opc, |
| 4717 | SourceLocation OpLoc, |
| 4718 | OverloadCandidate *Cand) { |
| 4719 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 4720 | std::string TypeStr("operator"); |
| 4721 | TypeStr += Opc; |
| 4722 | TypeStr += "("; |
| 4723 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 4724 | if (Cand->Conversions.size() == 1) { |
| 4725 | TypeStr += ")"; |
| 4726 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 4727 | } else { |
| 4728 | TypeStr += ", "; |
| 4729 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 4730 | TypeStr += ")"; |
| 4731 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 4736 | OverloadCandidate *Cand) { |
| 4737 | unsigned NoOperands = Cand->Conversions.size(); |
| 4738 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 4739 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4740 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 4741 | if (!ICS.isAmbiguous()) continue; |
| 4742 | |
| 4743 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
| 4744 | PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4745 | } |
| 4746 | } |
| 4747 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4748 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 4749 | if (Cand->Function) |
| 4750 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 4751 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4752 | return Cand->Surrogate->getLocation(); |
| 4753 | return SourceLocation(); |
| 4754 | } |
| 4755 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4756 | struct CompareOverloadCandidatesForDisplay { |
| 4757 | Sema &S; |
| 4758 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4759 | |
| 4760 | bool operator()(const OverloadCandidate *L, |
| 4761 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 4762 | // Fast-path this check. |
| 4763 | if (L == R) return false; |
| 4764 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4765 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4766 | if (L->Viable) { |
| 4767 | if (!R->Viable) return true; |
| 4768 | |
| 4769 | // TODO: introduce a tri-valued comparison for overload |
| 4770 | // candidates. Would be more worthwhile if we had a sort |
| 4771 | // that could exploit it. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4772 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 4773 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4774 | } else if (R->Viable) |
| 4775 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4776 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4777 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4778 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4779 | // Criteria by which we can sort non-viable candidates: |
| 4780 | if (!L->Viable) { |
| 4781 | // 1. Arity mismatches come after other candidates. |
| 4782 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 4783 | L->FailureKind == ovl_fail_too_few_arguments) |
| 4784 | return false; |
| 4785 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 4786 | R->FailureKind == ovl_fail_too_few_arguments) |
| 4787 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4788 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4789 | // 2. Bad conversions come first and are ordered by the number |
| 4790 | // of bad conversions and quality of good conversions. |
| 4791 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 4792 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 4793 | return true; |
| 4794 | |
| 4795 | // If there's any ordering between the defined conversions... |
| 4796 | // FIXME: this might not be transitive. |
| 4797 | assert(L->Conversions.size() == R->Conversions.size()); |
| 4798 | |
| 4799 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame^] | 4800 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 4801 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4802 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 4803 | R->Conversions[I])) { |
| 4804 | case ImplicitConversionSequence::Better: |
| 4805 | leftBetter++; |
| 4806 | break; |
| 4807 | |
| 4808 | case ImplicitConversionSequence::Worse: |
| 4809 | leftBetter--; |
| 4810 | break; |
| 4811 | |
| 4812 | case ImplicitConversionSequence::Indistinguishable: |
| 4813 | break; |
| 4814 | } |
| 4815 | } |
| 4816 | if (leftBetter > 0) return true; |
| 4817 | if (leftBetter < 0) return false; |
| 4818 | |
| 4819 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 4820 | return false; |
| 4821 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 4822 | // TODO: others? |
| 4823 | } |
| 4824 | |
| 4825 | // Sort everything else by location. |
| 4826 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 4827 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 4828 | |
| 4829 | // Put candidates without locations (e.g. builtins) at the end. |
| 4830 | if (LLoc.isInvalid()) return false; |
| 4831 | if (RLoc.isInvalid()) return true; |
| 4832 | |
| 4833 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4834 | } |
| 4835 | }; |
| 4836 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4837 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 4838 | /// computes up to the first |
| 4839 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 4840 | Expr **Args, unsigned NumArgs) { |
| 4841 | assert(!Cand->Viable); |
| 4842 | |
| 4843 | // Don't do anything on failures other than bad conversion. |
| 4844 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 4845 | |
| 4846 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4847 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4848 | unsigned ConvCount = Cand->Conversions.size(); |
| 4849 | while (true) { |
| 4850 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 4851 | ConvIdx++; |
| 4852 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 4853 | break; |
| 4854 | } |
| 4855 | |
| 4856 | if (ConvIdx == ConvCount) |
| 4857 | return; |
| 4858 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 4859 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 4860 | "remaining conversion is initialized?"); |
| 4861 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4862 | // FIXME: these should probably be preserved from the overload |
| 4863 | // operation somehow. |
| 4864 | bool SuppressUserConversions = false; |
| 4865 | bool ForceRValue = false; |
| 4866 | |
| 4867 | const FunctionProtoType* Proto; |
| 4868 | unsigned ArgIdx = ConvIdx; |
| 4869 | |
| 4870 | if (Cand->IsSurrogate) { |
| 4871 | QualType ConvType |
| 4872 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 4873 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4874 | ConvType = ConvPtrType->getPointeeType(); |
| 4875 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 4876 | ArgIdx--; |
| 4877 | } else if (Cand->Function) { |
| 4878 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 4879 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 4880 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 4881 | ArgIdx--; |
| 4882 | } else { |
| 4883 | // Builtin binary operator with a bad first conversion. |
| 4884 | assert(ConvCount <= 3); |
| 4885 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 4886 | Cand->Conversions[ConvIdx] |
| 4887 | = S.TryCopyInitialization(Args[ConvIdx], |
| 4888 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 4889 | SuppressUserConversions, ForceRValue, |
| 4890 | /*InOverloadResolution*/ true); |
| 4891 | return; |
| 4892 | } |
| 4893 | |
| 4894 | // Fill in the rest of the conversions. |
| 4895 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4896 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 4897 | if (ArgIdx < NumArgsInProto) |
| 4898 | Cand->Conversions[ConvIdx] |
| 4899 | = S.TryCopyInitialization(Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 4900 | SuppressUserConversions, ForceRValue, |
| 4901 | /*InOverloadResolution=*/true); |
| 4902 | else |
| 4903 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 4904 | } |
| 4905 | } |
| 4906 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4907 | } // end anonymous namespace |
| 4908 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4909 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 4910 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4911 | /// set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4912 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4913 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4914 | OverloadCandidateDisplayKind OCD, |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 4915 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 4916 | const char *Opc, |
Fariborz Jahanian | 29f9d39 | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 4917 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4918 | // Sort the candidates by viability and position. Sorting directly would |
| 4919 | // be prohibitive, so we make a set of pointers and sort those. |
| 4920 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 4921 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 4922 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4923 | LastCand = CandidateSet.end(); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4924 | Cand != LastCand; ++Cand) { |
| 4925 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4926 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4927 | else if (OCD == OCD_AllCandidates) { |
| 4928 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 4929 | Cands.push_back(Cand); |
| 4930 | } |
| 4931 | } |
| 4932 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 4933 | std::sort(Cands.begin(), Cands.end(), |
| 4934 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4935 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4936 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4937 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4938 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 4939 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 4940 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 4941 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4942 | if (Cand->Function) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4943 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4944 | else if (Cand->IsSurrogate) |
| 4945 | NoteSurrogateCandidate(*this, Cand); |
| 4946 | |
| 4947 | // This a builtin candidate. We do not, in general, want to list |
| 4948 | // every possible builtin candidate. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4949 | else if (Cand->Viable) { |
| 4950 | // Generally we only see ambiguities including viable builtin |
| 4951 | // operators if overload resolution got screwed up by an |
| 4952 | // ambiguous user-defined conversion. |
| 4953 | // |
| 4954 | // FIXME: It's quite possible for different conversions to see |
| 4955 | // different ambiguities, though. |
| 4956 | if (!ReportedAmbiguousConversions) { |
| 4957 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 4958 | ReportedAmbiguousConversions = true; |
| 4959 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4960 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4961 | // If this is a viable builtin, print it. |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 4962 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4963 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4964 | } |
| 4965 | } |
| 4966 | |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 4967 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, NamedDecl *D, |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4968 | AccessSpecifier AS) { |
| 4969 | if (isa<UnresolvedLookupExpr>(E)) |
| 4970 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D, AS); |
| 4971 | |
| 4972 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D, AS); |
| 4973 | } |
| 4974 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4975 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 4976 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 4977 | /// expression with overloaded function type and @p ToType is the type |
| 4978 | /// we're trying to resolve to. For example: |
| 4979 | /// |
| 4980 | /// @code |
| 4981 | /// int f(double); |
| 4982 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4983 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4984 | /// int (*pfd)(double) = f; // selects f(double) |
| 4985 | /// @endcode |
| 4986 | /// |
| 4987 | /// This routine returns the resulting FunctionDecl if it could be |
| 4988 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 4989 | /// routine will emit diagnostics if there is an error. |
| 4990 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4991 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4992 | bool Complain) { |
| 4993 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4994 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4995 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4996 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4997 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 4998 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4999 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5000 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5001 | FunctionType = MemTypePtr->getPointeeType(); |
| 5002 | IsMember = true; |
| 5003 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5004 | |
| 5005 | // We only look at pointers or references to functions. |
Douglas Gregor | 6b6ba8b | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 5006 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5007 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5008 | return 0; |
| 5009 | |
| 5010 | // Find the actual overloaded function declaration. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5011 | if (From->getType() != Context.OverloadTy) |
| 5012 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5013 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5014 | // C++ [over.over]p1: |
| 5015 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5016 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5017 | // C++ [over.over]p1: |
| 5018 | // [...] The overloaded function name can be preceded by the & |
| 5019 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5020 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5021 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 5022 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 5023 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 5024 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5027 | // Look through all of the overloaded functions, searching for one |
| 5028 | // whose type matches exactly. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5029 | UnresolvedSet<4> Matches; // contains only FunctionDecls |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5030 | bool FoundNonTemplateFunction = false; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5031 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5032 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5033 | // Look through any using declarations to find the underlying function. |
| 5034 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5035 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5036 | // C++ [over.over]p3: |
| 5037 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 5038 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5039 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5040 | // type "pointer-to-member-function." |
| 5041 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5042 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5043 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5044 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5045 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5046 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5048 | // static when converting to member pointer. |
| 5049 | if (Method->isStatic() == IsMember) |
| 5050 | continue; |
| 5051 | } else if (IsMember) |
| 5052 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5053 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5054 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5055 | // If the name is a function template, template argument deduction is |
| 5056 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5057 | // resulting template argument list is used to generate a single |
| 5058 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5059 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5060 | // 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] | 5061 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5062 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5063 | if (TemplateDeductionResult Result |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5064 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5065 | FunctionType, Specialization, Info)) { |
| 5066 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5067 | (void)Result; |
| 5068 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5069 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5070 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5072 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5073 | Matches.addDecl(cast<FunctionDecl>(Specialization->getCanonicalDecl()), |
| 5074 | I.getAccess()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5075 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5076 | |
| 5077 | continue; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5078 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5079 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5080 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5081 | // Skip non-static functions when converting to pointer, and static |
| 5082 | // when converting to member pointer. |
| 5083 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5084 | continue; |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5085 | |
| 5086 | // If we have explicit template arguments, skip non-templates. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5087 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5088 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5089 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5090 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5091 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5092 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 5093 | QualType ResultTy; |
| 5094 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5095 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5096 | ResultTy)) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5097 | Matches.addDecl(cast<FunctionDecl>(FunDecl->getCanonicalDecl()), |
| 5098 | I.getAccess()); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5099 | FoundNonTemplateFunction = true; |
| 5100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5101 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5104 | // If there were 0 or 1 matches, we're done. |
| 5105 | if (Matches.empty()) |
| 5106 | return 0; |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5107 | else if (Matches.size() == 1) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5108 | FunctionDecl *Result = cast<FunctionDecl>(*Matches.begin()); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5109 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5110 | if (Complain) |
| 5111 | CheckUnresolvedAccess(*this, OvlExpr, Result, Matches.begin().getAccess()); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5112 | return Result; |
| 5113 | } |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5114 | |
| 5115 | // C++ [over.over]p4: |
| 5116 | // If more than one function is selected, [...] |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5117 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5118 | // [...] and any given function template specialization F1 is |
| 5119 | // eliminated if the set contains a second function template |
| 5120 | // specialization whose function template is more specialized |
| 5121 | // than the function template of F1 according to the partial |
| 5122 | // ordering rules of 14.5.5.2. |
| 5123 | |
| 5124 | // The algorithm specified above is quadratic. We instead use a |
| 5125 | // two-pass algorithm (similar to the one used to identify the |
| 5126 | // best viable function in an overload set) that identifies the |
| 5127 | // best function template (if it exists). |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5128 | |
| 5129 | UnresolvedSetIterator Result = |
| 5130 | getMostSpecialized(Matches.begin(), Matches.end(), |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5131 | TPOC_Other, From->getLocStart(), |
| 5132 | PDiag(), |
| 5133 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5134 | << Matches[0]->getDeclName(), |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5135 | PDiag(diag::note_ovl_candidate) |
| 5136 | << (unsigned) oc_function_template); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5137 | assert(Result != Matches.end() && "no most-specialized template"); |
| 5138 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
| 5139 | if (Complain) |
| 5140 | CheckUnresolvedAccess(*this, OvlExpr, *Result, Result.getAccess()); |
| 5141 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5142 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5143 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5144 | // [...] any function template specializations in the set are |
| 5145 | // eliminated if the set also contains a non-template function, [...] |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5146 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 5147 | if (cast<FunctionDecl>(Matches[I].getDecl())->getPrimaryTemplate() == 0) |
| 5148 | ++I; |
| 5149 | else { |
| 5150 | Matches.erase(I); |
| 5151 | --N; |
| 5152 | } |
| 5153 | } |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5154 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5155 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5156 | // selected function. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5157 | if (Matches.size() == 1) { |
| 5158 | UnresolvedSetIterator Match = Matches.begin(); |
| 5159 | MarkDeclarationReferenced(From->getLocStart(), *Match); |
| 5160 | if (Complain) |
| 5161 | CheckUnresolvedAccess(*this, OvlExpr, *Match, Match.getAccess()); |
| 5162 | return cast<FunctionDecl>(*Match); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5165 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5166 | // returns (even if we issue the diagnostics here). |
| 5167 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5168 | << Matches[0]->getDeclName(); |
| 5169 | for (UnresolvedSetIterator I = Matches.begin(), |
| 5170 | E = Matches.end(); I != E; ++I) |
| 5171 | NoteOverloadCandidate(cast<FunctionDecl>(*I)); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5172 | return 0; |
| 5173 | } |
| 5174 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5175 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5176 | /// resolve that overloaded function expression down to a single function. |
| 5177 | /// |
| 5178 | /// This routine can only resolve template-ids that refer to a single function |
| 5179 | /// template, where that template-id refers to a single template whose template |
| 5180 | /// arguments are either provided by the template-id or have defaults, |
| 5181 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5182 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5183 | // C++ [over.over]p1: |
| 5184 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5185 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5186 | // C++ [over.over]p1: |
| 5187 | // [...] The overloaded function name can be preceded by the & |
| 5188 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5189 | |
| 5190 | if (From->getType() != Context.OverloadTy) |
| 5191 | return 0; |
| 5192 | |
| 5193 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5194 | |
| 5195 | // If we didn't actually find any template-ids, we're done. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5196 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5197 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5198 | |
| 5199 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5200 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5201 | |
| 5202 | // Look through all of the overloaded functions, searching for one |
| 5203 | // whose type matches exactly. |
| 5204 | FunctionDecl *Matched = 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5205 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5206 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5207 | // C++0x [temp.arg.explicit]p3: |
| 5208 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5209 | // where deduction is not done, if a template argument list is |
| 5210 | // specified and it, along with any default template arguments, |
| 5211 | // identifies a single function template specialization, then the |
| 5212 | // template-id is an lvalue for the function template specialization. |
| 5213 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5214 | |
| 5215 | // C++ [over.over]p2: |
| 5216 | // If the name is a function template, template argument deduction is |
| 5217 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5218 | // resulting template argument list is used to generate a single |
| 5219 | // function template specialization, which is added to the set of |
| 5220 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5221 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5222 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5223 | if (TemplateDeductionResult Result |
| 5224 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5225 | Specialization, Info)) { |
| 5226 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5227 | (void)Result; |
| 5228 | continue; |
| 5229 | } |
| 5230 | |
| 5231 | // Multiple matches; we can't resolve to a single declaration. |
| 5232 | if (Matched) |
| 5233 | return 0; |
| 5234 | |
| 5235 | Matched = Specialization; |
| 5236 | } |
| 5237 | |
| 5238 | return Matched; |
| 5239 | } |
| 5240 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5241 | /// \brief Add a single candidate to the overload set. |
| 5242 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5243 | NamedDecl *Callee, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5244 | AccessSpecifier Access, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5245 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5246 | Expr **Args, unsigned NumArgs, |
| 5247 | OverloadCandidateSet &CandidateSet, |
| 5248 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5249 | if (isa<UsingShadowDecl>(Callee)) |
| 5250 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5251 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5252 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5253 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5254 | S.AddOverloadCandidate(Func, Access, Args, NumArgs, CandidateSet, |
| 5255 | false, false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5256 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | if (FunctionTemplateDecl *FuncTemplate |
| 5260 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5261 | S.AddTemplateOverloadCandidate(FuncTemplate, Access, ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5262 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5263 | return; |
| 5264 | } |
| 5265 | |
| 5266 | assert(false && "unhandled case in overloaded call candidate"); |
| 5267 | |
| 5268 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5272 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5273 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5274 | Expr **Args, unsigned NumArgs, |
| 5275 | OverloadCandidateSet &CandidateSet, |
| 5276 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5277 | |
| 5278 | #ifndef NDEBUG |
| 5279 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5280 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5281 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5282 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5283 | // and let Y be the lookup set produced by argument dependent |
| 5284 | // lookup (defined as follows). If X contains |
| 5285 | // |
| 5286 | // -- a declaration of a class member, or |
| 5287 | // |
| 5288 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5289 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5290 | // |
| 5291 | // -- a declaration that is neither a function or a function |
| 5292 | // template |
| 5293 | // |
| 5294 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5295 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5296 | if (ULE->requiresADL()) { |
| 5297 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5298 | E = ULE->decls_end(); I != E; ++I) { |
| 5299 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5300 | assert(isa<UsingShadowDecl>(*I) || |
| 5301 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5302 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5303 | } |
| 5304 | } |
| 5305 | #endif |
| 5306 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5307 | // It would be nice to avoid this copy. |
| 5308 | TemplateArgumentListInfo TABuffer; |
| 5309 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5310 | if (ULE->hasExplicitTemplateArgs()) { |
| 5311 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5312 | ExplicitTemplateArgs = &TABuffer; |
| 5313 | } |
| 5314 | |
| 5315 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5316 | E = ULE->decls_end(); I != E; ++I) |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 5317 | AddOverloadedCallCandidate(*this, *I, I.getAccess(), ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5318 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5319 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5320 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5321 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5322 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 5323 | Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5324 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5325 | CandidateSet, |
| 5326 | PartialOverloading); |
| 5327 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5328 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5329 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5330 | Expr **Args, unsigned NumArgs) { |
| 5331 | Fn->Destroy(SemaRef.Context); |
| 5332 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5333 | Args[Arg]->Destroy(SemaRef.Context); |
| 5334 | return SemaRef.ExprError(); |
| 5335 | } |
| 5336 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5337 | /// Attempts to recover from a call where no functions were found. |
| 5338 | /// |
| 5339 | /// Returns true if new candidates were found. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5340 | static Sema::OwningExprResult |
| 5341 | BuildRecoveryCallExpr(Sema &SemaRef, Expr *Fn, |
| 5342 | UnresolvedLookupExpr *ULE, |
| 5343 | SourceLocation LParenLoc, |
| 5344 | Expr **Args, unsigned NumArgs, |
| 5345 | SourceLocation *CommaLocs, |
| 5346 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5347 | |
| 5348 | CXXScopeSpec SS; |
| 5349 | if (ULE->getQualifier()) { |
| 5350 | SS.setScopeRep(ULE->getQualifier()); |
| 5351 | SS.setRange(ULE->getQualifierRange()); |
| 5352 | } |
| 5353 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5354 | TemplateArgumentListInfo TABuffer; |
| 5355 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5356 | if (ULE->hasExplicitTemplateArgs()) { |
| 5357 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5358 | ExplicitTemplateArgs = &TABuffer; |
| 5359 | } |
| 5360 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5361 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5362 | Sema::LookupOrdinaryName); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 5363 | if (SemaRef.DiagnoseEmptyLookup(/*Scope=*/0, SS, R)) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5364 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5365 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5366 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5367 | |
| 5368 | // Build an implicit member call if appropriate. Just drop the |
| 5369 | // casts and such from the call, we don't really care. |
| 5370 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5371 | if ((*R.begin())->isCXXClassMember()) |
| 5372 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5373 | else if (ExplicitTemplateArgs) |
| 5374 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5375 | else |
| 5376 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 5377 | |
| 5378 | if (NewFn.isInvalid()) |
| 5379 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 5380 | |
| 5381 | Fn->Destroy(SemaRef.Context); |
| 5382 | |
| 5383 | // This shouldn't cause an infinite loop because we're giving it |
| 5384 | // an expression with non-empty lookup results, which should never |
| 5385 | // end up here. |
| 5386 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 5387 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 5388 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5389 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5390 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5391 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5392 | /// (which eventually refers to the declaration Func) and the call |
| 5393 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 5394 | /// to a specific function. If overload resolution succeeds, returns |
| 5395 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 5396 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5397 | /// arguments and Fn, and returns NULL. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5398 | Sema::OwningExprResult |
| 5399 | Sema::BuildOverloadedCallExpr(Expr *Fn, UnresolvedLookupExpr *ULE, |
| 5400 | SourceLocation LParenLoc, |
| 5401 | Expr **Args, unsigned NumArgs, |
| 5402 | SourceLocation *CommaLocs, |
| 5403 | SourceLocation RParenLoc) { |
| 5404 | #ifndef NDEBUG |
| 5405 | if (ULE->requiresADL()) { |
| 5406 | // To do ADL, we must have found an unqualified name. |
| 5407 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 5408 | |
| 5409 | // We don't perform ADL for implicit declarations of builtins. |
| 5410 | // Verify that this was correctly set up. |
| 5411 | FunctionDecl *F; |
| 5412 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 5413 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 5414 | F->getBuiltinID() && F->isImplicit()) |
| 5415 | assert(0 && "performing ADL for builtin"); |
| 5416 | |
| 5417 | // We don't perform ADL in C. |
| 5418 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 5419 | } |
| 5420 | #endif |
| 5421 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5422 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 5423 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5424 | // Add the functions denoted by the callee to the set of candidate |
| 5425 | // functions, including those from argument-dependent lookup. |
| 5426 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5427 | |
| 5428 | // If we found nothing, try to recover. |
| 5429 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 5430 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5431 | if (CandidateSet.empty()) |
| 5432 | return BuildRecoveryCallExpr(*this, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5433 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5434 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5435 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5436 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5437 | case OR_Success: { |
| 5438 | FunctionDecl *FDecl = Best->Function; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5439 | CheckUnresolvedLookupAccess(ULE, FDecl, Best->getAccess()); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5440 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 5441 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 5442 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5443 | |
| 5444 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 5445 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5446 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5447 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5448 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5449 | break; |
| 5450 | |
| 5451 | case OR_Ambiguous: |
| 5452 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5453 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5454 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5455 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5456 | |
| 5457 | case OR_Deleted: |
| 5458 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 5459 | << Best->Function->isDeleted() |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5460 | << ULE->getName() |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5461 | << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5462 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 5463 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | // Overload resolution failed. Destroy all of the subexpressions and |
| 5467 | // return NULL. |
| 5468 | Fn->Destroy(Context); |
| 5469 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5470 | Args[Arg]->Destroy(Context); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5471 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5474 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5475 | return Functions.size() > 1 || |
| 5476 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 5477 | } |
| 5478 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5479 | /// \brief Create a unary operation that may resolve to an overloaded |
| 5480 | /// operator. |
| 5481 | /// |
| 5482 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 5483 | /// |
| 5484 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 5485 | /// operator. |
| 5486 | /// |
| 5487 | /// \param Functions The set of non-member functions that will be |
| 5488 | /// considered by overload resolution. The caller needs to build this |
| 5489 | /// set based on the context using, e.g., |
| 5490 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5491 | /// set should not contain any member functions; those will be added |
| 5492 | /// by CreateOverloadedUnaryOp(). |
| 5493 | /// |
| 5494 | /// \param input The input argument. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5495 | Sema::OwningExprResult |
| 5496 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 5497 | const UnresolvedSetImpl &Fns, |
| 5498 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5499 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 5500 | Expr *Input = (Expr *)input.get(); |
| 5501 | |
| 5502 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 5503 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 5504 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5505 | |
| 5506 | Expr *Args[2] = { Input, 0 }; |
| 5507 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5508 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5509 | // For post-increment and post-decrement, add the implicit '0' as |
| 5510 | // the second argument, so that we know this is a post-increment or |
| 5511 | // post-decrement. |
| 5512 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 5513 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5514 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5515 | SourceLocation()); |
| 5516 | NumArgs = 2; |
| 5517 | } |
| 5518 | |
| 5519 | if (Input->isTypeDependent()) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5520 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5521 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5522 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5523 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5524 | /*ADL*/ true, IsOverloaded(Fns)); |
| 5525 | Fn->addDecls(Fns.begin(), Fns.end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5527 | input.release(); |
| 5528 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 5529 | &Args[0], NumArgs, |
| 5530 | Context.DependentTy, |
| 5531 | OpLoc)); |
| 5532 | } |
| 5533 | |
| 5534 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5535 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5536 | |
| 5537 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5538 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5539 | |
| 5540 | // Add operator candidates that are member functions. |
| 5541 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 5542 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5543 | // Add candidates from ADL. |
| 5544 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | 6ec89d4 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 5545 | Args, NumArgs, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5546 | /*ExplicitTemplateArgs*/ 0, |
| 5547 | CandidateSet); |
| 5548 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5549 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5550 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5551 | |
| 5552 | // Perform overload resolution. |
| 5553 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5554 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5555 | case OR_Success: { |
| 5556 | // We found a built-in operator or an overloaded operator. |
| 5557 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5558 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5559 | if (FnDecl) { |
| 5560 | // We matched an overloaded operator. Build a call to that |
| 5561 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5563 | // Convert the arguments. |
| 5564 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5565 | CheckMemberOperatorAccess(OpLoc, Args[0], Method, Best->getAccess()); |
| 5566 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5567 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 5568 | return ExprError(); |
| 5569 | } else { |
| 5570 | // Convert the arguments. |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5571 | OwningExprResult InputInit |
| 5572 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5573 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5574 | SourceLocation(), |
| 5575 | move(input)); |
| 5576 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5577 | return ExprError(); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5578 | |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 5579 | input = move(InputInit); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 5580 | Input = (Expr *)input.get(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | // Determine the result type |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5584 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5585 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5586 | // Build the actual expression node. |
| 5587 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5588 | SourceLocation()); |
| 5589 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5590 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5591 | input.release(); |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5592 | Args[0] = Input; |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5593 | ExprOwningPtr<CallExpr> TheCall(this, |
| 5594 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 5595 | Args, NumArgs, ResultTy, OpLoc)); |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 5596 | |
| 5597 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5598 | FnDecl)) |
| 5599 | return ExprError(); |
| 5600 | |
| 5601 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5602 | } else { |
| 5603 | // We matched a built-in operator. Convert the arguments, then |
| 5604 | // break out so that we will build the appropriate built-in |
| 5605 | // operator node. |
| 5606 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5607 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5608 | return ExprError(); |
| 5609 | |
| 5610 | break; |
| 5611 | } |
| 5612 | } |
| 5613 | |
| 5614 | case OR_No_Viable_Function: |
| 5615 | // No viable function; fall through to handling this as a |
| 5616 | // built-in operator, which will produce an error message for us. |
| 5617 | break; |
| 5618 | |
| 5619 | case OR_Ambiguous: |
| 5620 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5621 | << UnaryOperator::getOpcodeStr(Opc) |
| 5622 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5623 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5624 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5625 | return ExprError(); |
| 5626 | |
| 5627 | case OR_Deleted: |
| 5628 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5629 | << Best->Function->isDeleted() |
| 5630 | << UnaryOperator::getOpcodeStr(Opc) |
| 5631 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5632 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5633 | return ExprError(); |
| 5634 | } |
| 5635 | |
| 5636 | // Either we found no viable overloaded operator or we matched a |
| 5637 | // built-in operator. In either case, fall through to trying to |
| 5638 | // build a built-in operation. |
| 5639 | input.release(); |
| 5640 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 5641 | } |
| 5642 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5643 | /// \brief Create a binary operation that may resolve to an overloaded |
| 5644 | /// operator. |
| 5645 | /// |
| 5646 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 5647 | /// |
| 5648 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 5649 | /// operator. |
| 5650 | /// |
| 5651 | /// \param Functions The set of non-member functions that will be |
| 5652 | /// considered by overload resolution. The caller needs to build this |
| 5653 | /// set based on the context using, e.g., |
| 5654 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 5655 | /// set should not contain any member functions; those will be added |
| 5656 | /// by CreateOverloadedBinOp(). |
| 5657 | /// |
| 5658 | /// \param LHS Left-hand argument. |
| 5659 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5660 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5661 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5662 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5663 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5664 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5665 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5666 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5667 | |
| 5668 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 5669 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 5670 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 5671 | |
| 5672 | // If either side is type-dependent, create an appropriate dependent |
| 5673 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5674 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5675 | if (Fns.empty()) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5676 | // If there are no functions to store, just build a dependent |
| 5677 | // BinaryOperator or CompoundAssignment. |
| 5678 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 5679 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 5680 | Context.DependentTy, OpLoc)); |
| 5681 | |
| 5682 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 5683 | Context.DependentTy, |
| 5684 | Context.DependentTy, |
| 5685 | Context.DependentTy, |
| 5686 | OpLoc)); |
| 5687 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5688 | |
| 5689 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5690 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5691 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5692 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5693 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5694 | /*ADL*/ true, IsOverloaded(Fns)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5695 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5696 | Fn->addDecls(Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5697 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5698 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5699 | Context.DependentTy, |
| 5700 | OpLoc)); |
| 5701 | } |
| 5702 | |
| 5703 | // If this is the .* operator, which is not overloadable, just |
| 5704 | // create a built-in binary operator. |
| 5705 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5706 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5707 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 5708 | // If this is the assignment operator, we only perform overload resolution |
| 5709 | // if the left-hand side is a class or enumeration type. This is actually |
| 5710 | // a hack. The standard requires that we do overload resolution between the |
| 5711 | // various built-in candidates, but as DR507 points out, this can lead to |
| 5712 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 5713 | // Note that we go the traditional code path for compound assignment forms. |
| 5714 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5715 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5716 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5717 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5718 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5719 | |
| 5720 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5721 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5722 | |
| 5723 | // Add operator candidates that are member functions. |
| 5724 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 5725 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5726 | // Add candidates from ADL. |
| 5727 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 5728 | Args, 2, |
| 5729 | /*ExplicitTemplateArgs*/ 0, |
| 5730 | CandidateSet); |
| 5731 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5732 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 5733 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5734 | |
| 5735 | // Perform overload resolution. |
| 5736 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5737 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5738 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5739 | // We found a built-in operator or an overloaded operator. |
| 5740 | FunctionDecl *FnDecl = Best->Function; |
| 5741 | |
| 5742 | if (FnDecl) { |
| 5743 | // We matched an overloaded operator. Build a call to that |
| 5744 | // operator. |
| 5745 | |
| 5746 | // Convert the arguments. |
| 5747 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5748 | // Best->Access is only meaningful for class members. |
| 5749 | CheckMemberOperatorAccess(OpLoc, Args[0], Method, Best->getAccess()); |
| 5750 | |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5751 | OwningExprResult Arg1 |
| 5752 | = PerformCopyInitialization( |
| 5753 | InitializedEntity::InitializeParameter( |
| 5754 | FnDecl->getParamDecl(0)), |
| 5755 | SourceLocation(), |
| 5756 | Owned(Args[1])); |
| 5757 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5758 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5759 | |
| 5760 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
| 5761 | return ExprError(); |
| 5762 | |
| 5763 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5764 | } else { |
| 5765 | // Convert the arguments. |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5766 | OwningExprResult Arg0 |
| 5767 | = PerformCopyInitialization( |
| 5768 | InitializedEntity::InitializeParameter( |
| 5769 | FnDecl->getParamDecl(0)), |
| 5770 | SourceLocation(), |
| 5771 | Owned(Args[0])); |
| 5772 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5773 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 5774 | |
| 5775 | OwningExprResult Arg1 |
| 5776 | = PerformCopyInitialization( |
| 5777 | InitializedEntity::InitializeParameter( |
| 5778 | FnDecl->getParamDecl(1)), |
| 5779 | SourceLocation(), |
| 5780 | Owned(Args[1])); |
| 5781 | if (Arg1.isInvalid()) |
| 5782 | return ExprError(); |
| 5783 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 5784 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | // Determine the result type |
| 5788 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5789 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5790 | ResultTy = ResultTy.getNonReferenceType(); |
| 5791 | |
| 5792 | // Build the actual expression node. |
| 5793 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 5794 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5795 | UsualUnaryConversions(FnExpr); |
| 5796 | |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 5797 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5798 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 5799 | Args, 2, ResultTy, |
| 5800 | OpLoc)); |
| 5801 | |
| 5802 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 5803 | FnDecl)) |
| 5804 | return ExprError(); |
| 5805 | |
| 5806 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5807 | } else { |
| 5808 | // We matched a built-in operator. Convert the arguments, then |
| 5809 | // break out so that we will build the appropriate built-in |
| 5810 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5811 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5812 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5813 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5814 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5815 | return ExprError(); |
| 5816 | |
| 5817 | break; |
| 5818 | } |
| 5819 | } |
| 5820 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5821 | case OR_No_Viable_Function: { |
| 5822 | // C++ [over.match.oper]p9: |
| 5823 | // If the operator is the operator , [...] and there are no |
| 5824 | // viable functions, then the operator is assumed to be the |
| 5825 | // built-in operator and interpreted according to clause 5. |
| 5826 | if (Opc == BinaryOperator::Comma) |
| 5827 | break; |
| 5828 | |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5829 | // For class as left operand for assignment or compound assigment operator |
| 5830 | // do not fall through to handling in built-in, but report that no overloaded |
| 5831 | // assignment operator found |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5832 | OwningExprResult Result = ExprError(); |
| 5833 | if (Args[0]->getType()->isRecordType() && |
| 5834 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5835 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 5836 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5837 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5838 | } else { |
| 5839 | // No viable function; try to create a built-in operation, which will |
| 5840 | // produce an error. Then, show the non-viable candidates. |
| 5841 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 5842 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5843 | assert(Result.isInvalid() && |
| 5844 | "C++ binary operator overloading is missing candidates!"); |
| 5845 | if (Result.isInvalid()) |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5846 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5847 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5848 | return move(Result); |
| 5849 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5850 | |
| 5851 | case OR_Ambiguous: |
| 5852 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 5853 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5854 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5855 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5856 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5857 | return ExprError(); |
| 5858 | |
| 5859 | case OR_Deleted: |
| 5860 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 5861 | << Best->Function->isDeleted() |
| 5862 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5863 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5864 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5865 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5866 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5867 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 5868 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 5869 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5870 | } |
| 5871 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5872 | Action::OwningExprResult |
| 5873 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 5874 | SourceLocation RLoc, |
| 5875 | ExprArg Base, ExprArg Idx) { |
| 5876 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 5877 | static_cast<Expr*>(Idx.get()) }; |
| 5878 | DeclarationName OpName = |
| 5879 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 5880 | |
| 5881 | // If either side is type-dependent, create an appropriate dependent |
| 5882 | // expression. |
| 5883 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 5884 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5885 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5886 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5887 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5888 | 0, SourceRange(), OpName, LLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 5889 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 5890 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5891 | |
| 5892 | Base.release(); |
| 5893 | Idx.release(); |
| 5894 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 5895 | Args, 2, |
| 5896 | Context.DependentTy, |
| 5897 | RLoc)); |
| 5898 | } |
| 5899 | |
| 5900 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5901 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5902 | |
| 5903 | // Subscript can only be overloaded as a member function. |
| 5904 | |
| 5905 | // Add operator candidates that are member functions. |
| 5906 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5907 | |
| 5908 | // Add builtin operator candidates. |
| 5909 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 5910 | |
| 5911 | // Perform overload resolution. |
| 5912 | OverloadCandidateSet::iterator Best; |
| 5913 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 5914 | case OR_Success: { |
| 5915 | // We found a built-in operator or an overloaded operator. |
| 5916 | FunctionDecl *FnDecl = Best->Function; |
| 5917 | |
| 5918 | if (FnDecl) { |
| 5919 | // We matched an overloaded operator. Build a call to that |
| 5920 | // operator. |
| 5921 | |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 5922 | CheckMemberOperatorAccess(LLoc, Args[0], FnDecl, Best->getAccess()); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5923 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5924 | // Convert the arguments. |
| 5925 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 5926 | if (PerformObjectArgumentInitialization(Args[0], Method)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5927 | return ExprError(); |
| 5928 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 5929 | // Convert the arguments. |
| 5930 | OwningExprResult InputInit |
| 5931 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 5932 | FnDecl->getParamDecl(0)), |
| 5933 | SourceLocation(), |
| 5934 | Owned(Args[1])); |
| 5935 | if (InputInit.isInvalid()) |
| 5936 | return ExprError(); |
| 5937 | |
| 5938 | Args[1] = InputInit.takeAs<Expr>(); |
| 5939 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5940 | // Determine the result type |
| 5941 | QualType ResultTy |
| 5942 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5943 | ResultTy = ResultTy.getNonReferenceType(); |
| 5944 | |
| 5945 | // Build the actual expression node. |
| 5946 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 5947 | LLoc); |
| 5948 | UsualUnaryConversions(FnExpr); |
| 5949 | |
| 5950 | Base.release(); |
| 5951 | Idx.release(); |
| 5952 | ExprOwningPtr<CXXOperatorCallExpr> |
| 5953 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 5954 | FnExpr, Args, 2, |
| 5955 | ResultTy, RLoc)); |
| 5956 | |
| 5957 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 5958 | FnDecl)) |
| 5959 | return ExprError(); |
| 5960 | |
| 5961 | return MaybeBindToTemporary(TheCall.release()); |
| 5962 | } else { |
| 5963 | // We matched a built-in operator. Convert the arguments, then |
| 5964 | // break out so that we will build the appropriate built-in |
| 5965 | // operator node. |
| 5966 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5967 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5968 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5969 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5970 | return ExprError(); |
| 5971 | |
| 5972 | break; |
| 5973 | } |
| 5974 | } |
| 5975 | |
| 5976 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5977 | if (CandidateSet.empty()) |
| 5978 | Diag(LLoc, diag::err_ovl_no_oper) |
| 5979 | << Args[0]->getType() << /*subscript*/ 0 |
| 5980 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 5981 | else |
| 5982 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 5983 | << Args[0]->getType() |
| 5984 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5985 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 5986 | "[]", LLoc); |
| 5987 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | case OR_Ambiguous: |
| 5991 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 5992 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5993 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 5994 | "[]", LLoc); |
| 5995 | return ExprError(); |
| 5996 | |
| 5997 | case OR_Deleted: |
| 5998 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 5999 | << Best->Function->isDeleted() << "[]" |
| 6000 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6001 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6002 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6003 | return ExprError(); |
| 6004 | } |
| 6005 | |
| 6006 | // We matched a built-in operator; build it. |
| 6007 | Base.release(); |
| 6008 | Idx.release(); |
| 6009 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 6010 | Owned(Args[1]), RLoc); |
| 6011 | } |
| 6012 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6013 | /// BuildCallToMemberFunction - Build a call to a member |
| 6014 | /// function. MemExpr is the expression that refers to the member |
| 6015 | /// function (and includes the object parameter), Args/NumArgs are the |
| 6016 | /// arguments to the function call (not including the object |
| 6017 | /// parameter). The caller needs to validate that the member |
| 6018 | /// expression refers to a member function or an overloaded member |
| 6019 | /// function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6020 | Sema::OwningExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6021 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 6022 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6023 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 6024 | SourceLocation RParenLoc) { |
| 6025 | // Dig out the member expression. This holds both the object |
| 6026 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6027 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6028 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6029 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6030 | CXXMethodDecl *Method = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6031 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6032 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6033 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 6034 | } else { |
| 6035 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6036 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6037 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6039 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6040 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6041 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6042 | // FIXME: avoid copy. |
| 6043 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6044 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6045 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6046 | TemplateArgs = &TemplateArgsBuffer; |
| 6047 | } |
| 6048 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6049 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6050 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6051 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6052 | NamedDecl *Func = *I; |
| 6053 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6054 | if (isa<UsingShadowDecl>(Func)) |
| 6055 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6056 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6057 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6058 | // If explicit template arguments were provided, we can't call a |
| 6059 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6060 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6061 | continue; |
| 6062 | |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6063 | AddMethodCandidate(Method, I.getAccess(), ActingDC, ObjectType, |
| 6064 | Args, NumArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6065 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6066 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6067 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6068 | I.getAccess(), ActingDC, TemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6069 | ObjectType, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6070 | CandidateSet, |
| 6071 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6072 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6073 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6074 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6075 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6076 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6077 | OverloadCandidateSet::iterator Best; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6078 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6079 | case OR_Success: |
| 6080 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6081 | CheckUnresolvedMemberAccess(UnresExpr, Method, Best->getAccess()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6082 | break; |
| 6083 | |
| 6084 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6085 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6086 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6087 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6088 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6089 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6090 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6091 | |
| 6092 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6093 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6094 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6095 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6096 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6097 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6098 | |
| 6099 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6100 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6101 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6102 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6103 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6104 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6105 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6108 | MemExprE = FixOverloadedFunctionReference(MemExprE, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6109 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6110 | // If overload resolution picked a static member, build a |
| 6111 | // non-member call based on that function. |
| 6112 | if (Method->isStatic()) { |
| 6113 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6114 | Args, NumArgs, RParenLoc); |
| 6115 | } |
| 6116 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6117 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6118 | } |
| 6119 | |
| 6120 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6121 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6122 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6123 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6124 | Method->getResultType().getNonReferenceType(), |
| 6125 | RParenLoc)); |
| 6126 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6127 | // Check for a valid return type. |
| 6128 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6129 | TheCall.get(), Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6130 | return ExprError(); |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6131 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6132 | // Convert the object argument (for a non-static member function call). |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6133 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6134 | if (!Method->isStatic() && |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6135 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6136 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6137 | MemExpr->setBase(ObjectArg); |
| 6138 | |
| 6139 | // Convert the rest of the arguments |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6140 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6142 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6143 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6144 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6145 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6146 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 6147 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6148 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6151 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6152 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6153 | /// overloaded function call operator (@c operator()) or performing a |
| 6154 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6155 | Sema::ExprResult |
| 6156 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 6157 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6158 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6160 | SourceLocation RParenLoc) { |
| 6161 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6162 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6163 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6164 | // C++ [over.call.object]p1: |
| 6165 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6166 | // 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] | 6167 | // candidate functions includes at least the function call |
| 6168 | // operators of T. The function call operators of T are obtained by |
| 6169 | // ordinary lookup of the name operator() in the context of |
| 6170 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6171 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6172 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6173 | |
| 6174 | if (RequireCompleteType(LParenLoc, Object->getType(), |
| 6175 | PartialDiagnostic(diag::err_incomplete_object_call) |
| 6176 | << Object->getSourceRange())) |
| 6177 | return true; |
| 6178 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6179 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6180 | LookupQualifiedName(R, Record->getDecl()); |
| 6181 | R.suppressDiagnostics(); |
| 6182 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6183 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6184 | Oper != OperEnd; ++Oper) { |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6185 | AddMethodCandidate(*Oper, Oper.getAccess(), Object->getType(), |
| 6186 | Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6187 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6188 | } |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6190 | // C++ [over.call.object]p2: |
| 6191 | // In addition, for each conversion function declared in T of the |
| 6192 | // form |
| 6193 | // |
| 6194 | // operator conversion-type-id () cv-qualifier; |
| 6195 | // |
| 6196 | // where cv-qualifier is the same cv-qualification as, or a |
| 6197 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 6198 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6199 | // R", or the type "reference to pointer to function of |
| 6200 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6201 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6202 | // is also considered as a candidate function. Similarly, |
| 6203 | // surrogate call functions are added to the set of candidate |
| 6204 | // functions for each conversion function declared in an |
| 6205 | // accessible base class provided the function is not hidden |
| 6206 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6207 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 6208 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6209 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6210 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6211 | NamedDecl *D = *I; |
| 6212 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6213 | if (isa<UsingShadowDecl>(D)) |
| 6214 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6215 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6216 | // Skip over templated conversion functions; they aren't |
| 6217 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6218 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6219 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6220 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6221 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6222 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6223 | // Strip the reference type (if any) and then the pointer type (if |
| 6224 | // any) to get down to what might be a function type. |
| 6225 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6226 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6227 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6228 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6229 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6230 | AddSurrogateCandidate(Conv, I.getAccess(), ActingContext, Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6231 | Object->getType(), Args, NumArgs, |
| 6232 | CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6233 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6234 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6235 | // Perform overload resolution. |
| 6236 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6237 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6238 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6239 | // Overload resolution succeeded; we'll build the appropriate call |
| 6240 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6241 | break; |
| 6242 | |
| 6243 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6244 | if (CandidateSet.empty()) |
| 6245 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6246 | << Object->getType() << /*call*/ 1 |
| 6247 | << Object->getSourceRange(); |
| 6248 | else |
| 6249 | Diag(Object->getSourceRange().getBegin(), |
| 6250 | diag::err_ovl_no_viable_object_call) |
| 6251 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6252 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6253 | break; |
| 6254 | |
| 6255 | case OR_Ambiguous: |
| 6256 | Diag(Object->getSourceRange().getBegin(), |
| 6257 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6258 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6259 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6260 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6261 | |
| 6262 | case OR_Deleted: |
| 6263 | Diag(Object->getSourceRange().getBegin(), |
| 6264 | diag::err_ovl_deleted_object_call) |
| 6265 | << Best->Function->isDeleted() |
| 6266 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6267 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6268 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6270 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6271 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6272 | // We had an error; delete all of the subexpressions and return |
| 6273 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6274 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6275 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6276 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6277 | return true; |
| 6278 | } |
| 6279 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6280 | if (Best->Function == 0) { |
| 6281 | // Since there is no function declaration, this is one of the |
| 6282 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6284 | = cast<CXXConversionDecl>( |
| 6285 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6286 | |
John McCall | 2cb9416 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 6287 | CheckMemberOperatorAccess(LParenLoc, Object, Conv, Best->getAccess()); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6288 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6289 | // We selected one of the surrogate functions that converts the |
| 6290 | // object parameter to a function pointer. Perform the conversion |
| 6291 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6292 | |
| 6293 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6294 | // and then call it. |
Eli Friedman | a958a01 | 2009-12-09 04:52:43 +0000 | [diff] [blame] | 6295 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Conv); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6296 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6297 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6298 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 6299 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
John McCall | 2cb9416 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 6302 | CheckMemberOperatorAccess(LParenLoc, Object, |
| 6303 | Best->Function, Best->getAccess()); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6304 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6305 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6306 | // that calls this method, using Object for the implicit object |
| 6307 | // parameter and passing along the remaining arguments. |
| 6308 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6309 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6310 | |
| 6311 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6312 | unsigned NumArgsToCheck = NumArgs; |
| 6313 | |
| 6314 | // Build the full argument list for the method call (the |
| 6315 | // implicit object parameter is placed at the beginning of the |
| 6316 | // list). |
| 6317 | Expr **MethodArgs; |
| 6318 | if (NumArgs < NumArgsInProto) { |
| 6319 | NumArgsToCheck = NumArgsInProto; |
| 6320 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6321 | } else { |
| 6322 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6323 | } |
| 6324 | MethodArgs[0] = Object; |
| 6325 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6326 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6327 | |
| 6328 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6329 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6330 | UsualUnaryConversions(NewFn); |
| 6331 | |
| 6332 | // Once we've built TheCall, all of the expressions are properly |
| 6333 | // owned. |
| 6334 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6335 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6336 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6337 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6338 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6339 | delete [] MethodArgs; |
| 6340 | |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6341 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6342 | Method)) |
| 6343 | return true; |
| 6344 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6345 | // We may have default arguments. If so, we need to allocate more |
| 6346 | // slots in the call for them. |
| 6347 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6348 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6349 | else if (NumArgs > NumArgsInProto) |
| 6350 | NumArgsToCheck = NumArgsInProto; |
| 6351 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6352 | bool IsError = false; |
| 6353 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6354 | // Initialize the implicit object parameter. |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6355 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6356 | TheCall->setArg(0, Object); |
| 6357 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6358 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6359 | // Check the argument types. |
| 6360 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6361 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6362 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6363 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6364 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6365 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 6366 | |
| 6367 | OwningExprResult InputInit |
| 6368 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6369 | Method->getParamDecl(i)), |
| 6370 | SourceLocation(), Owned(Arg)); |
| 6371 | |
| 6372 | IsError |= InputInit.isInvalid(); |
| 6373 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6374 | } else { |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 6375 | OwningExprResult DefArg |
| 6376 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 6377 | if (DefArg.isInvalid()) { |
| 6378 | IsError = true; |
| 6379 | break; |
| 6380 | } |
| 6381 | |
| 6382 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6383 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6384 | |
| 6385 | TheCall->setArg(i + 1, Arg); |
| 6386 | } |
| 6387 | |
| 6388 | // If this is a variadic call, handle args passed through "...". |
| 6389 | if (Proto->isVariadic()) { |
| 6390 | // Promote the arguments (C99 6.5.2.2p7). |
| 6391 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 6392 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6393 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6394 | TheCall->setArg(i + 1, Arg); |
| 6395 | } |
| 6396 | } |
| 6397 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6398 | if (IsError) return true; |
| 6399 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6400 | if (CheckFunctionCall(Method, TheCall.get())) |
| 6401 | return true; |
| 6402 | |
Anders Carlsson | 1c83deb | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 6403 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6406 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6407 | /// (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] | 6408 | /// @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] | 6409 | Sema::OwningExprResult |
| 6410 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 6411 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6412 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6413 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6414 | SourceLocation Loc = Base->getExprLoc(); |
| 6415 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6416 | // C++ [over.ref]p1: |
| 6417 | // |
| 6418 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 6419 | // for a class object x of type T if T::operator->() exists and if |
| 6420 | // the operator is selected as the best match function by the |
| 6421 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6422 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6423 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6424 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6425 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6426 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 6427 | PDiag(diag::err_typecheck_incomplete_tag) |
| 6428 | << Base->getSourceRange())) |
| 6429 | return ExprError(); |
| 6430 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6431 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 6432 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 6433 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6434 | |
| 6435 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6436 | Oper != OperEnd; ++Oper) { |
| 6437 | NamedDecl *D = *Oper; |
| 6438 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6439 | if (isa<UsingShadowDecl>(D)) |
| 6440 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6441 | |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6442 | AddMethodCandidate(cast<CXXMethodDecl>(D), Oper.getAccess(), ActingContext, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6443 | Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6444 | /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6445 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6446 | |
| 6447 | // Perform overload resolution. |
| 6448 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6449 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6450 | case OR_Success: |
| 6451 | // Overload resolution succeeded; we'll build the call below. |
| 6452 | break; |
| 6453 | |
| 6454 | case OR_No_Viable_Function: |
| 6455 | if (CandidateSet.empty()) |
| 6456 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6457 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6458 | else |
| 6459 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6460 | << "operator->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6461 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6462 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6463 | |
| 6464 | case OR_Ambiguous: |
| 6465 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6466 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6467 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6468 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6469 | |
| 6470 | case OR_Deleted: |
| 6471 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6472 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 6473 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6474 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6475 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | // Convert the object parameter. |
| 6479 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6480 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6481 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 6482 | |
| 6483 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 6484 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6485 | |
| 6486 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6487 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 6488 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6489 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6490 | |
| 6491 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 6492 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6493 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 6494 | &Base, 1, ResultTy, OpLoc)); |
| 6495 | |
| 6496 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 6497 | Method)) |
| 6498 | return ExprError(); |
| 6499 | return move(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6502 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 6503 | /// a C++ overloaded function (possibly with some parentheses and |
| 6504 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 6505 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6506 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
| 6507 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6508 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6509 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 6510 | if (SubExpr == PE->getSubExpr()) |
| 6511 | return PE->Retain(); |
| 6512 | |
| 6513 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 6514 | } |
| 6515 | |
| 6516 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 6517 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), Fn); |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6518 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6519 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 6520 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6521 | if (SubExpr == ICE->getSubExpr()) |
| 6522 | return ICE->Retain(); |
| 6523 | |
| 6524 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 6525 | ICE->getCastKind(), |
| 6526 | SubExpr, |
| 6527 | ICE->isLvalueCast()); |
| 6528 | } |
| 6529 | |
| 6530 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6531 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6532 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6533 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6534 | if (Method->isStatic()) { |
| 6535 | // Do nothing: static member functions aren't any different |
| 6536 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6537 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6538 | // Fix the sub expression, which really has to be an |
| 6539 | // UnresolvedLookupExpr holding an overloaded member function |
| 6540 | // or template. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6541 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6542 | if (SubExpr == UnOp->getSubExpr()) |
| 6543 | return UnOp->Retain(); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6544 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6545 | assert(isa<DeclRefExpr>(SubExpr) |
| 6546 | && "fixed to something other than a decl ref"); |
| 6547 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 6548 | && "fixed to a member ref with no nested name qualifier"); |
| 6549 | |
| 6550 | // We have taken the address of a pointer to member |
| 6551 | // function. Perform the computation here so that we get the |
| 6552 | // appropriate pointer to member type. |
| 6553 | QualType ClassType |
| 6554 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 6555 | QualType MemPtrType |
| 6556 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 6557 | |
| 6558 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6559 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 6560 | } |
| 6561 | } |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6562 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
| 6563 | if (SubExpr == UnOp->getSubExpr()) |
| 6564 | return UnOp->Retain(); |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 6565 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6566 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 6567 | Context.getPointerType(SubExpr->getType()), |
| 6568 | UnOp->getOperatorLoc()); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6569 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6570 | |
| 6571 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6572 | // FIXME: avoid copy. |
| 6573 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6574 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6575 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6576 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6579 | return DeclRefExpr::Create(Context, |
| 6580 | ULE->getQualifier(), |
| 6581 | ULE->getQualifierRange(), |
| 6582 | Fn, |
| 6583 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6584 | Fn->getType(), |
| 6585 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6588 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6589 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6590 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6591 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 6592 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6593 | TemplateArgs = &TemplateArgsBuffer; |
| 6594 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6595 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6596 | Expr *Base; |
| 6597 | |
| 6598 | // If we're filling in |
| 6599 | if (MemExpr->isImplicitAccess()) { |
| 6600 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 6601 | return DeclRefExpr::Create(Context, |
| 6602 | MemExpr->getQualifier(), |
| 6603 | MemExpr->getQualifierRange(), |
| 6604 | Fn, |
| 6605 | MemExpr->getMemberLoc(), |
| 6606 | Fn->getType(), |
| 6607 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 6608 | } else { |
| 6609 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 6610 | if (MemExpr->getQualifier()) |
| 6611 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 6612 | Base = new (Context) CXXThisExpr(Loc, |
| 6613 | MemExpr->getBaseType(), |
| 6614 | /*isImplicit=*/true); |
| 6615 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6616 | } else |
| 6617 | Base = MemExpr->getBase()->Retain(); |
| 6618 | |
| 6619 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6620 | MemExpr->isArrow(), |
| 6621 | MemExpr->getQualifier(), |
| 6622 | MemExpr->getQualifierRange(), |
| 6623 | Fn, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6624 | MemExpr->getMemberLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6625 | TemplateArgs, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6626 | Fn->getType()); |
| 6627 | } |
| 6628 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 6629 | assert(false && "Invalid reference to overloaded function"); |
| 6630 | return E->Retain(); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6633 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
| 6634 | FunctionDecl *Fn) { |
| 6635 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Fn)); |
| 6636 | } |
| 6637 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6638 | } // end namespace clang |