Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 16 | #include "SemaInit.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | /// GetConversionCategory - Retrieve the implicit conversion |
| 32 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 35 | static const ImplicitConversionCategory |
| 36 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 37 | ICC_Identity, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 41 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 42 | ICC_Qualification_Adjustment, |
| 43 | ICC_Promotion, |
| 44 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 45 | ICC_Promotion, |
| 46 | ICC_Conversion, |
| 47 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
| 52 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 54 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 55 | ICC_Conversion |
| 56 | }; |
| 57 | return Category[(int)Kind]; |
| 58 | } |
| 59 | |
| 60 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 61 | /// corresponding to the given implicit conversion kind. |
| 62 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 63 | static const ImplicitConversionRank |
| 64 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 70 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 73 | ICR_Promotion, |
| 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
| 79 | ICR_Conversion, |
| 80 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 81 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 82 | ICR_Conversion, |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 83 | ICR_Complex_Real_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 84 | }; |
| 85 | return Rank[(int)Kind]; |
| 86 | } |
| 87 | |
| 88 | /// GetImplicitConversionName - Return the name of this kind of |
| 89 | /// implicit conversion. |
| 90 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 91 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | "No conversion", |
| 93 | "Lvalue-to-rvalue", |
| 94 | "Array-to-pointer", |
| 95 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 96 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 97 | "Qualification", |
| 98 | "Integral promotion", |
| 99 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Integral conversion", |
| 102 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 103 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 104 | "Floating-integral conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 105 | "Complex-real conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 106 | "Pointer conversion", |
| 107 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 108 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 109 | "Compatible-types conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 110 | "Derived-to-base conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 111 | }; |
| 112 | return Name[Kind]; |
| 113 | } |
| 114 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 115 | /// StandardConversionSequence - Set the standard conversion |
| 116 | /// sequence to the identity conversion. |
| 117 | void StandardConversionSequence::setAsIdentityConversion() { |
| 118 | First = ICK_Identity; |
| 119 | Second = ICK_Identity; |
| 120 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 121 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 122 | ReferenceBinding = false; |
| 123 | DirectBinding = false; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 124 | RRefBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 125 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 128 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 129 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 130 | /// implicit conversions. |
| 131 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 132 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 133 | if (GetConversionRank(First) > Rank) |
| 134 | Rank = GetConversionRank(First); |
| 135 | if (GetConversionRank(Second) > Rank) |
| 136 | Rank = GetConversionRank(Second); |
| 137 | if (GetConversionRank(Third) > Rank) |
| 138 | Rank = GetConversionRank(Third); |
| 139 | return Rank; |
| 140 | } |
| 141 | |
| 142 | /// isPointerConversionToBool - Determines whether this conversion is |
| 143 | /// a conversion of a pointer or pointer-to-member to bool. This is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 145 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 151 | if (getToType(1)->isBooleanType() && |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 152 | (getFromType()->isPointerType() || getFromType()->isBlockPointerType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 166 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 167 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 175 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 185 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | bool PrintedSomething = false; |
| 187 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 188 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 189 | PrintedSomething = true; |
| 190 | } |
| 191 | |
| 192 | if (Second != ICK_Identity) { |
| 193 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 194 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 195 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 196 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 197 | |
| 198 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 199 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 200 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 201 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 202 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 203 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 204 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 205 | PrintedSomething = true; |
| 206 | } |
| 207 | |
| 208 | if (Third != ICK_Identity) { |
| 209 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 210 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 211 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 212 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 213 | PrintedSomething = true; |
| 214 | } |
| 215 | |
| 216 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 217 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 222 | /// error. Useful for debugging overloading issues. |
| 223 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 224 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 225 | if (Before.First || Before.Second || Before.Third) { |
| 226 | Before.DebugPrint(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 227 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | } |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 229 | OS << '\'' << ConversionFunction << '\''; |
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 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 278 | namespace { |
| 279 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 280 | // template parameter and template argument information. |
| 281 | struct DFIParamWithArguments { |
| 282 | TemplateParameter Param; |
| 283 | TemplateArgument FirstArg; |
| 284 | TemplateArgument SecondArg; |
| 285 | }; |
| 286 | } |
| 287 | |
| 288 | /// \brief Convert from Sema's representation of template deduction information |
| 289 | /// to the form used in overload-candidate information. |
| 290 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 291 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 292 | Sema::TemplateDeductionResult TDK, |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 293 | Sema::TemplateDeductionInfo &Info) { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 294 | OverloadCandidate::DeductionFailureInfo Result; |
| 295 | Result.Result = static_cast<unsigned>(TDK); |
| 296 | Result.Data = 0; |
| 297 | switch (TDK) { |
| 298 | case Sema::TDK_Success: |
| 299 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 300 | case Sema::TDK_TooManyArguments: |
| 301 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 302 | break; |
| 303 | |
| 304 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 305 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 306 | Result.Data = Info.Param.getOpaqueValue(); |
| 307 | break; |
| 308 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 309 | case Sema::TDK_Inconsistent: |
| 310 | case Sema::TDK_InconsistentQuals: { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 311 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 312 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 313 | Saved->Param = Info.Param; |
| 314 | Saved->FirstArg = Info.FirstArg; |
| 315 | Saved->SecondArg = Info.SecondArg; |
| 316 | Result.Data = Saved; |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 321 | Result.Data = Info.take(); |
| 322 | break; |
| 323 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 324 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 325 | case Sema::TDK_FailedOverloadResolution: |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | return Result; |
| 330 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 332 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 333 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 334 | case Sema::TDK_Success: |
| 335 | case Sema::TDK_InstantiationDepth: |
| 336 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 337 | case Sema::TDK_TooManyArguments: |
| 338 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 339 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 340 | break; |
| 341 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 342 | case Sema::TDK_Inconsistent: |
| 343 | case Sema::TDK_InconsistentQuals: |
Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame^] | 344 | // FIXME: Destroy the data? |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 345 | Data = 0; |
| 346 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 347 | |
| 348 | case Sema::TDK_SubstitutionFailure: |
| 349 | // FIXME: Destroy the template arugment list? |
| 350 | Data = 0; |
| 351 | break; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 353 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 354 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 355 | case Sema::TDK_FailedOverloadResolution: |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | TemplateParameter |
| 361 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 362 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 363 | case Sema::TDK_Success: |
| 364 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 365 | case Sema::TDK_TooManyArguments: |
| 366 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 367 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 368 | return TemplateParameter(); |
| 369 | |
| 370 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 371 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 372 | return TemplateParameter::getFromOpaqueValue(Data); |
| 373 | |
| 374 | case Sema::TDK_Inconsistent: |
| 375 | case Sema::TDK_InconsistentQuals: |
| 376 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
| 377 | |
| 378 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 379 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 380 | case Sema::TDK_FailedOverloadResolution: |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | return TemplateParameter(); |
| 385 | } |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 386 | |
| 387 | TemplateArgumentList * |
| 388 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 389 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 390 | case Sema::TDK_Success: |
| 391 | case Sema::TDK_InstantiationDepth: |
| 392 | case Sema::TDK_TooManyArguments: |
| 393 | case Sema::TDK_TooFewArguments: |
| 394 | case Sema::TDK_Incomplete: |
| 395 | case Sema::TDK_InvalidExplicitArguments: |
| 396 | case Sema::TDK_Inconsistent: |
| 397 | case Sema::TDK_InconsistentQuals: |
| 398 | return 0; |
| 399 | |
| 400 | case Sema::TDK_SubstitutionFailure: |
| 401 | return static_cast<TemplateArgumentList*>(Data); |
| 402 | |
| 403 | // Unhandled |
| 404 | case Sema::TDK_NonDeducedMismatch: |
| 405 | case Sema::TDK_FailedOverloadResolution: |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 412 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 413 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 414 | case Sema::TDK_Success: |
| 415 | case Sema::TDK_InstantiationDepth: |
| 416 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 417 | case Sema::TDK_TooManyArguments: |
| 418 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 419 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 420 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 423 | case Sema::TDK_Inconsistent: |
| 424 | case Sema::TDK_InconsistentQuals: |
| 425 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
| 426 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 427 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 428 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 429 | case Sema::TDK_FailedOverloadResolution: |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | const TemplateArgument * |
| 437 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 438 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 439 | case Sema::TDK_Success: |
| 440 | case Sema::TDK_InstantiationDepth: |
| 441 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 442 | case Sema::TDK_TooManyArguments: |
| 443 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 444 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 445 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 446 | return 0; |
| 447 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 448 | case Sema::TDK_Inconsistent: |
| 449 | case Sema::TDK_InconsistentQuals: |
| 450 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 451 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 452 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 453 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 454 | case Sema::TDK_FailedOverloadResolution: |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | void OverloadCandidateSet::clear() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 462 | inherited::clear(); |
| 463 | Functions.clear(); |
| 464 | } |
| 465 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 466 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 467 | // overload of the declarations in Old. This routine returns false if |
| 468 | // New and Old cannot be overloaded, e.g., if New has the same |
| 469 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 470 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 471 | // it does return false, MatchedDecl will point to the decl that New |
| 472 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 473 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 474 | // |
| 475 | // Example: Given the following input: |
| 476 | // |
| 477 | // void f(int, float); // #1 |
| 478 | // void f(int, int); // #2 |
| 479 | // int f(int, int); // #3 |
| 480 | // |
| 481 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 483 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 484 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 485 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 486 | // (since they have different signatures), so this routine returns |
| 487 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 488 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 489 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 490 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 491 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 492 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 493 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 494 | // point to the FunctionDecl for #2. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 495 | Sema::OverloadKind |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 496 | Sema::CheckOverload(FunctionDecl *New, const LookupResult &Old, |
| 497 | NamedDecl *&Match) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 498 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 499 | I != E; ++I) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 500 | NamedDecl *OldD = (*I)->getUnderlyingDecl(); |
| 501 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 502 | if (!IsOverload(New, OldT->getTemplatedDecl())) { |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 503 | Match = *I; |
| 504 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 505 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 506 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 507 | if (!IsOverload(New, OldF)) { |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 508 | Match = *I; |
| 509 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 510 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 511 | } else if (isa<UsingDecl>(OldD) || isa<TagDecl>(OldD)) { |
| 512 | // We can overload with these, which can show up when doing |
| 513 | // redeclaration checks for UsingDecls. |
| 514 | assert(Old.getLookupKind() == LookupUsingDeclName); |
| 515 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 516 | // Optimistically assume that an unresolved using decl will |
| 517 | // overload; if it doesn't, we'll have to diagnose during |
| 518 | // template instantiation. |
| 519 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 520 | // (C++ 13p1): |
| 521 | // Only function declarations can be overloaded; object and type |
| 522 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 523 | Match = *I; |
| 524 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 525 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 526 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 527 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 528 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old) { |
| 532 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 533 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 534 | |
| 535 | // C++ [temp.fct]p2: |
| 536 | // A function template can be overloaded with other function templates |
| 537 | // and with normal (non-template) functions. |
| 538 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 539 | return true; |
| 540 | |
| 541 | // Is the function New an overload of the function Old? |
| 542 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 543 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 544 | |
| 545 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 546 | // determine whether they are overloads. If we find any mismatch |
| 547 | // in the signature, they are overloads. |
| 548 | |
| 549 | // If either of these functions is a K&R-style function (no |
| 550 | // prototype), then we consider them to have matching signatures. |
| 551 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 552 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 553 | return false; |
| 554 | |
| 555 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 556 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 557 | |
| 558 | // The signature of a function includes the types of its |
| 559 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 560 | // of the ellipsis; see C++ DR 357). |
| 561 | if (OldQType != NewQType && |
| 562 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 563 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 564 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 565 | return true; |
| 566 | |
| 567 | // C++ [temp.over.link]p4: |
| 568 | // The signature of a function template consists of its function |
| 569 | // signature, its return type and its template parameter list. The names |
| 570 | // of the template parameters are significant only for establishing the |
| 571 | // relationship between the template parameters and the rest of the |
| 572 | // signature. |
| 573 | // |
| 574 | // We check the return type and template parameter lists for function |
| 575 | // templates first; the remaining checks follow. |
| 576 | if (NewTemplate && |
| 577 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 578 | OldTemplate->getTemplateParameters(), |
| 579 | false, TPL_TemplateMatch) || |
| 580 | OldType->getResultType() != NewType->getResultType())) |
| 581 | return true; |
| 582 | |
| 583 | // If the function is a class member, its signature includes the |
| 584 | // cv-qualifiers (if any) on the function itself. |
| 585 | // |
| 586 | // As part of this, also check whether one of the member functions |
| 587 | // is static, in which case they are not overloads (C++ |
| 588 | // 13.1p2). While not part of the definition of the signature, |
| 589 | // this check is important to determine whether these functions |
| 590 | // can be overloaded. |
| 591 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 592 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 593 | if (OldMethod && NewMethod && |
| 594 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 595 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 596 | return true; |
| 597 | |
| 598 | // The signatures match; this is not an overload. |
| 599 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 602 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 603 | /// from the given expression (Expr) to the given type (ToType). This |
| 604 | /// function returns an implicit conversion sequence that can be used |
| 605 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 606 | /// |
| 607 | /// void f(float f); |
| 608 | /// void g(int i) { f(i); } |
| 609 | /// |
| 610 | /// this routine would produce an implicit conversion sequence to |
| 611 | /// describe the initialization of f from i, which will be a standard |
| 612 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 613 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 614 | // |
| 615 | /// Note that this routine only determines how the conversion can be |
| 616 | /// performed; it does not actually perform the conversion. As such, |
| 617 | /// it will not produce any diagnostics if no conversion is available, |
| 618 | /// but will instead return an implicit conversion sequence of kind |
| 619 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 620 | /// |
| 621 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 622 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 623 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 624 | /// permitted. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 625 | ImplicitConversionSequence |
Anders Carlsson | 5ec4abf | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 626 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 627 | bool SuppressUserConversions, |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 628 | bool AllowExplicit, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 629 | bool InOverloadResolution) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 630 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 631 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 632 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 633 | return ICS; |
| 634 | } |
| 635 | |
| 636 | if (!getLangOptions().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 637 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 638 | return ICS; |
| 639 | } |
| 640 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 641 | if (SuppressUserConversions) { |
| 642 | // C++ [over.ics.user]p4: |
| 643 | // A conversion of an expression of class type to the same class |
| 644 | // type is given Exact Match rank, and a conversion of an |
| 645 | // expression of class type to a base class of that type is |
| 646 | // given Conversion rank, in spite of the fact that a copy/move |
| 647 | // constructor (i.e., a user-defined conversion function) is |
| 648 | // called for those cases. |
| 649 | QualType FromType = From->getType(); |
| 650 | if (!ToType->getAs<RecordType>() || !FromType->getAs<RecordType>() || |
| 651 | !(Context.hasSameUnqualifiedType(FromType, ToType) || |
| 652 | IsDerivedFrom(FromType, ToType))) { |
| 653 | // We're not in the case above, so there is no conversion that |
| 654 | // we can perform. |
| 655 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
| 656 | return ICS; |
| 657 | } |
| 658 | |
| 659 | ICS.setStandard(); |
| 660 | ICS.Standard.setAsIdentityConversion(); |
| 661 | ICS.Standard.setFromType(FromType); |
| 662 | ICS.Standard.setAllToTypes(ToType); |
| 663 | |
| 664 | // We don't actually check at this point whether there is a valid |
| 665 | // copy/move constructor, since overloading just assumes that it |
| 666 | // exists. When we actually perform initialization, we'll find the |
| 667 | // appropriate constructor to copy the returned object, if needed. |
| 668 | ICS.Standard.CopyConstructor = 0; |
| 669 | |
| 670 | // Determine whether this is considered a derived-to-base conversion. |
| 671 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 672 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 673 | |
| 674 | return ICS; |
| 675 | } |
| 676 | |
| 677 | // Attempt user-defined conversion. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 678 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 679 | OverloadingResult UserDefResult |
| 680 | = IsUserDefinedConversion(From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 681 | AllowExplicit); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 682 | |
| 683 | if (UserDefResult == OR_Success) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 684 | ICS.setUserDefined(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 685 | // C++ [over.ics.user]p4: |
| 686 | // A conversion of an expression of class type to the same class |
| 687 | // type is given Exact Match rank, and a conversion of an |
| 688 | // expression of class type to a base class of that type is |
| 689 | // given Conversion rank, in spite of the fact that a copy |
| 690 | // constructor (i.e., a user-defined conversion function) is |
| 691 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 693 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | QualType FromCanon |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 695 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 696 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 697 | if (Constructor->isCopyConstructor() && |
Douglas Gregor | 4141d5b | 2009-12-22 00:21:20 +0000 | [diff] [blame] | 698 | (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 699 | // Turn this into a "standard" conversion sequence, so that it |
| 700 | // gets ranked with standard conversion sequences. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 701 | ICS.setStandard(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 702 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 703 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 704 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 705 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 706 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 707 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 708 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 709 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 710 | |
| 711 | // C++ [over.best.ics]p4: |
| 712 | // However, when considering the argument of a user-defined |
| 713 | // conversion function that is a candidate by 13.3.1.3 when |
| 714 | // invoked for the copying of the temporary in the second step |
| 715 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 716 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 717 | // ellipsis conversion sequences are allowed. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 718 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 719 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 720 | } |
John McCall | e8c8cd2 | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 721 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 722 | ICS.setAmbiguous(); |
| 723 | ICS.Ambiguous.setFromType(From->getType()); |
| 724 | ICS.Ambiguous.setToType(ToType); |
| 725 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 726 | Cand != Conversions.end(); ++Cand) |
| 727 | if (Cand->Viable) |
| 728 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 729 | } else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 730 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 731 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 732 | |
| 733 | return ICS; |
| 734 | } |
| 735 | |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 736 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 737 | /// expression From to the type ToType. Returns true if there was an |
| 738 | /// error, false otherwise. The expression From is replaced with the |
| 739 | /// converted expression. Flavor is the kind of conversion we're |
| 740 | /// performing, used in the error message. If @p AllowExplicit, |
| 741 | /// explicit user-defined conversions are permitted. |
| 742 | bool |
| 743 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 744 | AssignmentAction Action, bool AllowExplicit) { |
| 745 | ImplicitConversionSequence ICS; |
| 746 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 747 | } |
| 748 | |
| 749 | bool |
| 750 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 751 | AssignmentAction Action, bool AllowExplicit, |
| 752 | ImplicitConversionSequence& ICS) { |
| 753 | ICS = TryImplicitConversion(From, ToType, |
| 754 | /*SuppressUserConversions=*/false, |
| 755 | AllowExplicit, |
| 756 | /*InOverloadResolution=*/false); |
| 757 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 758 | } |
| 759 | |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 760 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 761 | /// conversion that strips "noreturn" off the nested function type. |
| 762 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
| 763 | QualType ToType, QualType &ResultTy) { |
| 764 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 765 | return false; |
| 766 | |
| 767 | // Strip the noreturn off the type we're converting from; noreturn can |
| 768 | // safely be removed. |
| 769 | FromType = Context.getNoReturnType(FromType, false); |
| 770 | if (!Context.hasSameUnqualifiedType(FromType, ToType)) |
| 771 | return false; |
| 772 | |
| 773 | ResultTy = FromType; |
| 774 | return true; |
| 775 | } |
| 776 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 777 | /// IsStandardConversion - Determines whether there is a standard |
| 778 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 779 | /// expression From to the type ToType. Standard conversion sequences |
| 780 | /// only consider non-class types; for conversions that involve class |
| 781 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 782 | /// contain the standard conversion sequence required to perform this |
| 783 | /// conversion and this routine will return true. Otherwise, this |
| 784 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | bool |
| 786 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 787 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 789 | QualType FromType = From->getType(); |
| 790 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 791 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 792 | SCS.setAsIdentityConversion(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 793 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 794 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 795 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 796 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 798 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 800 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 801 | if (getLangOptions().CPlusPlus) |
| 802 | return false; |
| 803 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 807 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 808 | // array-to-pointer conversion, or function-to-pointer conversion |
| 809 | // (C++ 4p1). |
| 810 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 811 | if (FromType == Context.OverloadTy) { |
| 812 | DeclAccessPair AccessPair; |
| 813 | if (FunctionDecl *Fn |
| 814 | = ResolveAddressOfOverloadedFunction(From, ToType, false, |
| 815 | AccessPair)) { |
| 816 | // We were able to resolve the address of the overloaded function, |
| 817 | // so we can convert to the type of that function. |
| 818 | FromType = Fn->getType(); |
| 819 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 820 | if (!Method->isStatic()) { |
| 821 | Type *ClassType |
| 822 | = Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 823 | FromType = Context.getMemberPointerType(FromType, ClassType); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | // If the "from" expression takes the address of the overloaded |
| 828 | // function, update the type of the resulting expression accordingly. |
| 829 | if (FromType->getAs<FunctionType>()) |
| 830 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(From->IgnoreParens())) |
| 831 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 832 | FromType = Context.getPointerType(FromType); |
| 833 | |
| 834 | // Check that we've computed the proper type after overload resolution. |
| 835 | assert(Context.hasSameType(FromType, |
| 836 | FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
| 837 | } else { |
| 838 | return false; |
| 839 | } |
| 840 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 842 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 843 | // converted to an rvalue. |
| 844 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 846 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 847 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 848 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 849 | |
| 850 | // If T is a non-class type, the type of the rvalue is the |
| 851 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 852 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 853 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 854 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 855 | } else if (FromType->isArrayType()) { |
| 856 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 857 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 858 | |
| 859 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 860 | // bound of T" can be converted to an rvalue of type "pointer to |
| 861 | // T" (C++ 4.2p1). |
| 862 | FromType = Context.getArrayDecayedType(FromType); |
| 863 | |
| 864 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 865 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 866 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 867 | |
| 868 | // For the purpose of ranking in overload resolution |
| 869 | // (13.3.3.1.1), this conversion is considered an |
| 870 | // array-to-pointer conversion followed by a qualification |
| 871 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 872 | SCS.Second = ICK_Identity; |
| 873 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 874 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 875 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 876 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 877 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 878 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 879 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 880 | |
| 881 | // An lvalue of function type T can be converted to an rvalue of |
| 882 | // type "pointer to T." The result is a pointer to the |
| 883 | // function. (C++ 4.3p1). |
| 884 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 885 | } else { |
| 886 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 887 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 888 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 889 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 890 | |
| 891 | // The second conversion can be an integral promotion, floating |
| 892 | // point promotion, integral conversion, floating point conversion, |
| 893 | // floating-integral conversion, pointer conversion, |
| 894 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 895 | // For overloading in C, this can also be a "compatible-type" |
| 896 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 897 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 898 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 899 | // The unqualified versions of the types are the same: there's no |
| 900 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 901 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 902 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 904 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 905 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 906 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 907 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 908 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 909 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 910 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 911 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 912 | SCS.Second = ICK_Complex_Promotion; |
| 913 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 914 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 915 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 916 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 917 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 918 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 919 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 920 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 921 | SCS.Second = ICK_Complex_Conversion; |
| 922 | FromType = ToType.getUnqualifiedType(); |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 923 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 924 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 925 | // Complex-real conversions (C99 6.3.1.7) |
| 926 | SCS.Second = ICK_Complex_Real; |
| 927 | FromType = ToType.getUnqualifiedType(); |
| 928 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 929 | // Floating point conversions (C++ 4.8). |
| 930 | SCS.Second = ICK_Floating_Conversion; |
| 931 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 932 | } else if ((FromType->isFloatingType() && |
| 933 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 934 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 936 | ToType->isFloatingType())) { |
| 937 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 938 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 939 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 940 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 941 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 942 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 943 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 944 | SCS.IncompatibleObjC = IncompatibleObjC; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 945 | } else if (IsMemberPointerConversion(From, FromType, ToType, |
| 946 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 947 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 948 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 949 | } else if (ToType->isBooleanType() && |
| 950 | (FromType->isArithmeticType() || |
| 951 | FromType->isEnumeralType() || |
Fariborz Jahanian | 8811885 | 2009-12-11 21:23:13 +0000 | [diff] [blame] | 952 | FromType->isAnyPointerType() || |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 953 | FromType->isBlockPointerType() || |
| 954 | FromType->isMemberPointerType() || |
| 955 | FromType->isNullPtrType())) { |
| 956 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 957 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 958 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 960 | Context.typesAreCompatible(ToType, FromType)) { |
| 961 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 962 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 963 | } else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) { |
| 964 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 965 | SCS.Second = ICK_NoReturn_Adjustment; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 966 | } else { |
| 967 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 968 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 969 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 970 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 971 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 972 | QualType CanonFrom; |
| 973 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 974 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 975 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 976 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 977 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 978 | CanonFrom = Context.getCanonicalType(FromType); |
| 979 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 980 | } else { |
| 981 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 982 | SCS.Third = ICK_Identity; |
| 983 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 985 | // [...] Any difference in top-level cv-qualification is |
| 986 | // subsumed by the initialization itself and does not constitute |
| 987 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 988 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 990 | if (CanonFrom.getLocalUnqualifiedType() |
| 991 | == CanonTo.getLocalUnqualifiedType() && |
| 992 | CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 993 | FromType = ToType; |
| 994 | CanonFrom = CanonTo; |
| 995 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 996 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 997 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 998 | |
| 999 | // If we have not converted the argument type to the parameter type, |
| 1000 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1001 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1002 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1004 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1008 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1009 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1010 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1012 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1013 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1014 | if (!To) { |
| 1015 | return false; |
| 1016 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1017 | |
| 1018 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1019 | // unsigned short int can be converted to an rvalue of type int if |
| 1020 | // int can represent all the values of the source type; otherwise, |
| 1021 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1022 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1023 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1024 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1025 | if (// We can promote any signed, promotable integer type to an int |
| 1026 | (FromType->isSignedIntegerType() || |
| 1027 | // We can promote any unsigned integer type whose size is |
| 1028 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1030 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1031 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1034 | return To->getKind() == BuiltinType::UInt; |
| 1035 | } |
| 1036 | |
| 1037 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 1038 | // can be converted to an rvalue of the first of the following types |
| 1039 | // that can represent all the values of its underlying type: int, |
| 1040 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1041 | |
| 1042 | // We pre-calculate the promotion type for enum types. |
| 1043 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) |
| 1044 | if (ToType->isIntegerType()) |
| 1045 | return Context.hasSameUnqualifiedType(ToType, |
| 1046 | FromEnumType->getDecl()->getPromotionType()); |
| 1047 | |
| 1048 | if (FromType->isWideCharType() && ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1049 | // Determine whether the type we're converting from is signed or |
| 1050 | // unsigned. |
| 1051 | bool FromIsSigned; |
| 1052 | uint64_t FromSize = Context.getTypeSize(FromType); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1053 | |
| 1054 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1055 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1056 | |
| 1057 | // The types we'll try to promote to, in the appropriate |
| 1058 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | QualType PromoteTypes[6] = { |
| 1060 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1061 | Context.LongTy, Context.UnsignedLongTy , |
| 1062 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1063 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1064 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1065 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1066 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1068 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1069 | // We found the type that we can promote to. If this is the |
| 1070 | // type we wanted, we have a promotion. Otherwise, no |
| 1071 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1072 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1078 | // rvalue of type int if int can represent all the values of the |
| 1079 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1080 | // unsigned int can represent all the values of the bit-field. If |
| 1081 | // the bit-field is larger yet, no integral promotion applies to |
| 1082 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1083 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1084 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1085 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1086 | using llvm::APSInt; |
| 1087 | if (From) |
| 1088 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1089 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1090 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 1091 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1092 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1093 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1095 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1096 | if (BitWidth < ToSize || |
| 1097 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1098 | return To->getKind() == BuiltinType::Int; |
| 1099 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1101 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1102 | // that fits into an unsigned int? |
| 1103 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1104 | return To->getKind() == BuiltinType::UInt; |
| 1105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1107 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1108 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1111 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1112 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1113 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1114 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1115 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1116 | |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| 1120 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1121 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1122 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1124 | /// An rvalue of type float can be converted to an rvalue of type |
| 1125 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1126 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1127 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1128 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1129 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1130 | return true; |
| 1131 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1132 | // C99 6.3.1.5p1: |
| 1133 | // When a float is promoted to double or long double, or a |
| 1134 | // double is promoted to long double [...]. |
| 1135 | if (!getLangOptions().CPlusPlus && |
| 1136 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1137 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1138 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1139 | return true; |
| 1140 | } |
| 1141 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
| 1144 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1145 | /// \brief Determine if a conversion is a complex promotion. |
| 1146 | /// |
| 1147 | /// A complex promotion is defined as a complex -> complex conversion |
| 1148 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1149 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1150 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1151 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1152 | if (!FromComplex) |
| 1153 | return false; |
| 1154 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1155 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1156 | if (!ToComplex) |
| 1157 | return false; |
| 1158 | |
| 1159 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1160 | ToComplex->getElementType()) || |
| 1161 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1162 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1165 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1166 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1167 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1168 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1169 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | static QualType |
| 1171 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1172 | QualType ToPointee, QualType ToType, |
| 1173 | ASTContext &Context) { |
| 1174 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 1175 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1176 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1179 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1180 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1181 | if (!ToType.isNull()) |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1182 | return ToType; |
| 1183 | |
| 1184 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1185 | // already. |
| 1186 | return Context.getPointerType(ToPointee); |
| 1187 | } |
| 1188 | |
| 1189 | // Just build a canonical type that has the right qualifiers. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1190 | return Context.getPointerType( |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1191 | Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), |
| 1192 | Quals)); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1195 | /// BuildSimilarlyQualifiedObjCObjectPointerType - In a pointer conversion from |
| 1196 | /// the FromType, which is an objective-c pointer, to ToType, which may or may |
| 1197 | /// not have the right set of qualifiers. |
| 1198 | static QualType |
| 1199 | BuildSimilarlyQualifiedObjCObjectPointerType(QualType FromType, |
| 1200 | QualType ToType, |
| 1201 | ASTContext &Context) { |
| 1202 | QualType CanonFromType = Context.getCanonicalType(FromType); |
| 1203 | QualType CanonToType = Context.getCanonicalType(ToType); |
| 1204 | Qualifiers Quals = CanonFromType.getQualifiers(); |
| 1205 | |
| 1206 | // Exact qualifier match -> return the pointer type we're converting to. |
| 1207 | if (CanonToType.getLocalQualifiers() == Quals) |
| 1208 | return ToType; |
| 1209 | |
| 1210 | // Just build a canonical type that has the right qualifiers. |
| 1211 | return Context.getQualifiedType(CanonToType.getLocalUnqualifiedType(), Quals); |
| 1212 | } |
| 1213 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1215 | bool InOverloadResolution, |
| 1216 | ASTContext &Context) { |
| 1217 | // Handle value-dependent integral null pointer constants correctly. |
| 1218 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1219 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 1220 | Expr->getType()->isIntegralType()) |
| 1221 | return !InOverloadResolution; |
| 1222 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1223 | return Expr->isNullPointerConstant(Context, |
| 1224 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1225 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1226 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1228 | /// IsPointerConversion - Determines whether the conversion of the |
| 1229 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1230 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1231 | /// 4.10). If so, returns true and places the converted type (that |
| 1232 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1233 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1234 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1235 | /// This routine also supports conversions to and from block pointers |
| 1236 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1237 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1238 | /// appropriate overloading rules for Objective-C, we may want to |
| 1239 | /// split the Objective-C checks into a different routine; however, |
| 1240 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1241 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1242 | /// set if the conversion is an allowed Objective-C conversion that |
| 1243 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1244 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1245 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1246 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1248 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1249 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 1250 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1251 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1253 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1254 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1255 | ConvertedType = ToType; |
| 1256 | return true; |
| 1257 | } |
| 1258 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1259 | // Blocks: Block pointers can be converted to void*. |
| 1260 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1261 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1262 | ConvertedType = ToType; |
| 1263 | return true; |
| 1264 | } |
| 1265 | // Blocks: A null pointer constant can be converted to a block |
| 1266 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1268 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1269 | ConvertedType = ToType; |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1273 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1274 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1276 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1277 | ConvertedType = ToType; |
| 1278 | return true; |
| 1279 | } |
| 1280 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1281 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1282 | if (!ToTypePtr) |
| 1283 | return false; |
| 1284 | |
| 1285 | // 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] | 1286 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1287 | ConvertedType = ToType; |
| 1288 | return true; |
| 1289 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1290 | |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1291 | // Beyond this point, both types need to be pointers |
| 1292 | // , including objective-c pointers. |
| 1293 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1294 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
| 1295 | ConvertedType = BuildSimilarlyQualifiedObjCObjectPointerType(FromType, |
| 1296 | ToType, Context); |
| 1297 | return true; |
| 1298 | |
| 1299 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1300 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1301 | if (!FromTypePtr) |
| 1302 | return false; |
| 1303 | |
| 1304 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1306 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1307 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1308 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1309 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1311 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1312 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1313 | return true; |
| 1314 | } |
| 1315 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1316 | // When we're overloading in C, we allow a special kind of pointer |
| 1317 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1319 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1321 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1323 | return true; |
| 1324 | } |
| 1325 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1326 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1328 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1329 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1330 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1331 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1332 | // necessitates this conversion is ill-formed. The result of the |
| 1333 | // conversion is a pointer to the base class sub-object of the |
| 1334 | // derived class object. The null pointer value is converted to |
| 1335 | // the null pointer value of the destination type. |
| 1336 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1337 | // Note that we do not check for ambiguity or inaccessibility |
| 1338 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1339 | if (getLangOptions().CPlusPlus && |
| 1340 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1341 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1342 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1343 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1345 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1346 | ToType, Context); |
| 1347 | return true; |
| 1348 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | /// isObjCPointerConversion - Determines whether this is an |
| 1354 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1355 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1357 | QualType& ConvertedType, |
| 1358 | bool &IncompatibleObjC) { |
| 1359 | if (!getLangOptions().ObjC1) |
| 1360 | return false; |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1361 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1362 | // First, we handle all conversions on ObjC object pointer types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1363 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1365 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1366 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1367 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1368 | // 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] | 1369 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1370 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1371 | ConvertedType = ToType; |
| 1372 | return true; |
| 1373 | } |
| 1374 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1376 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1378 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1379 | ConvertedType = ToType; |
| 1380 | return true; |
| 1381 | } |
| 1382 | // Objective C++: We're able to convert from a pointer to an |
| 1383 | // interface to a pointer to a different interface. |
| 1384 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1385 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1386 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1387 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1388 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1389 | FromObjCPtr->getPointeeType())) |
| 1390 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1391 | ConvertedType = ToType; |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
| 1395 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1396 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1397 | // interfaces, which is permitted. However, we're going to |
| 1398 | // complain about it. |
| 1399 | IncompatibleObjC = true; |
| 1400 | ConvertedType = FromType; |
| 1401 | return true; |
| 1402 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1404 | // 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] | 1405 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1406 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1407 | ToPointeeType = ToCPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1408 | else if (const BlockPointerType *ToBlockPtr = |
| 1409 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1410 | // 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] | 1411 | // to a block pointer type. |
| 1412 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1413 | ConvertedType = ToType; |
| 1414 | return true; |
| 1415 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1416 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1417 | } |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1418 | else if (FromType->getAs<BlockPointerType>() && |
| 1419 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
| 1420 | // 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] | 1421 | // pointer to any object. |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1422 | ConvertedType = ToType; |
| 1423 | return true; |
| 1424 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1425 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1426 | return false; |
| 1427 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1428 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1429 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1430 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1431 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1432 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1433 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1434 | return false; |
| 1435 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1436 | // If we have pointers to pointers, recursively check whether this |
| 1437 | // is an Objective-C conversion. |
| 1438 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1439 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1440 | IncompatibleObjC)) { |
| 1441 | // We always complain about this conversion. |
| 1442 | IncompatibleObjC = true; |
| 1443 | ConvertedType = ToType; |
| 1444 | return true; |
| 1445 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1446 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1447 | // as in I* to id. |
| 1448 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1449 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1450 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1451 | IncompatibleObjC)) { |
| 1452 | ConvertedType = ToType; |
| 1453 | return true; |
| 1454 | } |
| 1455 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1456 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1457 | // differences in the argument and result types are in Objective-C |
| 1458 | // pointer conversions. If so, we permit the conversion (but |
| 1459 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1461 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1462 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1463 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1464 | if (FromFunctionType && ToFunctionType) { |
| 1465 | // If the function types are exactly the same, this isn't an |
| 1466 | // Objective-C pointer conversion. |
| 1467 | if (Context.getCanonicalType(FromPointeeType) |
| 1468 | == Context.getCanonicalType(ToPointeeType)) |
| 1469 | return false; |
| 1470 | |
| 1471 | // Perform the quick checks that will tell us whether these |
| 1472 | // function types are obviously different. |
| 1473 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1474 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1475 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1476 | return false; |
| 1477 | |
| 1478 | bool HasObjCConversion = false; |
| 1479 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1480 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1481 | // Okay, the types match exactly. Nothing to do. |
| 1482 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1483 | ToFunctionType->getResultType(), |
| 1484 | ConvertedType, IncompatibleObjC)) { |
| 1485 | // Okay, we have an Objective-C pointer conversion. |
| 1486 | HasObjCConversion = true; |
| 1487 | } else { |
| 1488 | // Function types are too different. Abort. |
| 1489 | return false; |
| 1490 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1492 | // Check argument types. |
| 1493 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1494 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1495 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1496 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1497 | if (Context.getCanonicalType(FromArgType) |
| 1498 | == Context.getCanonicalType(ToArgType)) { |
| 1499 | // Okay, the types match exactly. Nothing to do. |
| 1500 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1501 | ConvertedType, IncompatibleObjC)) { |
| 1502 | // Okay, we have an Objective-C pointer conversion. |
| 1503 | HasObjCConversion = true; |
| 1504 | } else { |
| 1505 | // Argument types are too different. Abort. |
| 1506 | return false; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | if (HasObjCConversion) { |
| 1511 | // We had an Objective-C conversion. Allow this pointer |
| 1512 | // conversion, but complain about it. |
| 1513 | ConvertedType = ToType; |
| 1514 | IncompatibleObjC = true; |
| 1515 | return true; |
| 1516 | } |
| 1517 | } |
| 1518 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1519 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1520 | } |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1521 | |
| 1522 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1523 | /// for equlity of their argument types. Caller has already checked that |
| 1524 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1525 | /// pointer types which only differ in their protocol qualifiers are equal. |
| 1526 | bool Sema::FunctionArgTypesAreEqual(FunctionProtoType* OldType, |
| 1527 | FunctionProtoType* NewType){ |
| 1528 | if (!getLangOptions().ObjC1) |
| 1529 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1530 | NewType->arg_type_begin()); |
| 1531 | |
| 1532 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1533 | N = NewType->arg_type_begin(), |
| 1534 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1535 | QualType ToType = (*O); |
| 1536 | QualType FromType = (*N); |
| 1537 | if (ToType != FromType) { |
| 1538 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1539 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 27c9fe9 | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1540 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1541 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1542 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1543 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1544 | continue; |
| 1545 | } |
| 1546 | else if (ToType->isObjCObjectPointerType() && |
| 1547 | FromType->isObjCObjectPointerType()) { |
| 1548 | QualType ToInterfaceTy = ToType->getPointeeType(); |
| 1549 | QualType FromInterfaceTy = FromType->getPointeeType(); |
| 1550 | if (const ObjCInterfaceType *OITTo = |
| 1551 | ToInterfaceTy->getAs<ObjCInterfaceType>()) |
| 1552 | if (const ObjCInterfaceType *OITFr = |
| 1553 | FromInterfaceTy->getAs<ObjCInterfaceType>()) |
| 1554 | if (OITTo->getDecl() == OITFr->getDecl()) |
| 1555 | continue; |
| 1556 | } |
| 1557 | return false; |
| 1558 | } |
| 1559 | } |
| 1560 | return true; |
| 1561 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1562 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1563 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1564 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1565 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1566 | /// conversions for which IsPointerConversion has already returned |
| 1567 | /// true. It returns true and produces a diagnostic if there was an |
| 1568 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1569 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1570 | CastExpr::CastKind &Kind, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1571 | CXXBaseSpecifierArray& BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1572 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1573 | QualType FromType = From->getType(); |
| 1574 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1575 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1576 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1577 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1578 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1580 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1581 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1582 | // We must have a derived-to-base conversion. Check an |
| 1583 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1584 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1585 | From->getExprLoc(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1586 | From->getSourceRange(), &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1587 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1588 | return true; |
| 1589 | |
| 1590 | // The conversion was successful. |
| 1591 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1595 | FromType->getAs<ObjCObjectPointerType>()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1597 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1598 | // Objective-C++ conversions are always okay. |
| 1599 | // FIXME: We should have a different class of conversions for the |
| 1600 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1601 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1602 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1603 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1604 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1605 | return false; |
| 1606 | } |
| 1607 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1608 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1609 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1610 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1611 | /// If so, returns true and places the converted type (that might differ from |
| 1612 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1613 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1614 | QualType ToType, |
| 1615 | bool InOverloadResolution, |
| 1616 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1617 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1618 | if (!ToTypePtr) |
| 1619 | return false; |
| 1620 | |
| 1621 | // 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] | 1622 | if (From->isNullPointerConstant(Context, |
| 1623 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1624 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1625 | ConvertedType = ToType; |
| 1626 | return true; |
| 1627 | } |
| 1628 | |
| 1629 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1630 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1631 | if (!FromTypePtr) |
| 1632 | return false; |
| 1633 | |
| 1634 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1635 | // where D is derived from B (C++ 4.11p2). |
| 1636 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1637 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1638 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1639 | |
| 1640 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1641 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1642 | ToClass.getTypePtr()); |
| 1643 | return true; |
| 1644 | } |
| 1645 | |
| 1646 | return false; |
| 1647 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1648 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1649 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1650 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1651 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1652 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1653 | /// true and produces a diagnostic if there was an error, or returns false |
| 1654 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1656 | CastExpr::CastKind &Kind, |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1657 | CXXBaseSpecifierArray &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1658 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1659 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1660 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1661 | if (!FromPtrType) { |
| 1662 | // This must be a null pointer to member pointer conversion |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1663 | assert(From->isNullPointerConstant(Context, |
| 1664 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1665 | "Expr must be null pointer constant!"); |
| 1666 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1667 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1668 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1669 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1670 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1671 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1672 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1673 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1674 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1675 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1676 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1677 | // FIXME: What about dependent types? |
| 1678 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1679 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1680 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1681 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1682 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1683 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1684 | assert(DerivationOkay && |
| 1685 | "Should not have been called if derivation isn't OK."); |
| 1686 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1687 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1688 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1689 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1690 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1691 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1692 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1693 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1694 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1695 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1696 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1697 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1698 | << FromClass << ToClass << QualType(VBase, 0) |
| 1699 | << From->getSourceRange(); |
| 1700 | return true; |
| 1701 | } |
| 1702 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1703 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1704 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 1705 | Paths.front(), |
| 1706 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1707 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1708 | // Must be a base to derived member conversion. |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1709 | BuildBasePathArray(Paths, BasePath); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1710 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1711 | return false; |
| 1712 | } |
| 1713 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1714 | /// IsQualificationConversion - Determines whether the conversion from |
| 1715 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1716 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | bool |
| 1718 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1719 | FromType = Context.getCanonicalType(FromType); |
| 1720 | ToType = Context.getCanonicalType(ToType); |
| 1721 | |
| 1722 | // If FromType and ToType are the same type, this is not a |
| 1723 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 1724 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1725 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1727 | // (C++ 4.4p4): |
| 1728 | // A conversion can add cv-qualifiers at levels other than the first |
| 1729 | // in multi-level pointers, subject to the following rules: [...] |
| 1730 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1731 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1732 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1733 | // Within each iteration of the loop, we check the qualifiers to |
| 1734 | // determine if this still looks like a qualification |
| 1735 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1736 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1737 | // until there are no more pointers or pointers-to-members left to |
| 1738 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1739 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1740 | |
| 1741 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1742 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1743 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1744 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1746 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1747 | // every cv for 0 < k < j. |
| 1748 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1749 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1750 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1752 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1753 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1755 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1756 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1757 | |
| 1758 | // We are left with FromType and ToType being the pointee types |
| 1759 | // after unwrapping the original FromType and ToType the same number |
| 1760 | // of types. If we unwrapped any pointers, and if FromType and |
| 1761 | // ToType have the same unqualified type (since we checked |
| 1762 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1763 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1766 | /// Determines whether there is a user-defined conversion sequence |
| 1767 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1768 | /// ToType. If such a conversion exists, User will contain the |
| 1769 | /// user-defined conversion sequence that performs such a conversion |
| 1770 | /// and this routine will return true. Otherwise, this routine returns |
| 1771 | /// false and User is unspecified. |
| 1772 | /// |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1773 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1774 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1775 | /// functions (C++0x [class.conv.fct]p2). |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1776 | OverloadingResult Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
| 1777 | UserDefinedConversionSequence& User, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1778 | OverloadCandidateSet& CandidateSet, |
| 1779 | bool AllowExplicit) { |
| 1780 | // Whether we will only visit constructors. |
| 1781 | bool ConstructorsOnly = false; |
| 1782 | |
| 1783 | // If the type we are conversion to is a class type, enumerate its |
| 1784 | // constructors. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1785 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1786 | // C++ [over.match.ctor]p1: |
| 1787 | // When objects of class type are direct-initialized (8.5), or |
| 1788 | // copy-initialized from an expression of the same or a |
| 1789 | // derived class type (8.5), overload resolution selects the |
| 1790 | // constructor. [...] For copy-initialization, the candidate |
| 1791 | // functions are all the converting constructors (12.3.1) of |
| 1792 | // that class. The argument list is the expression-list within |
| 1793 | // the parentheses of the initializer. |
| 1794 | if (Context.hasSameUnqualifiedType(ToType, From->getType()) || |
| 1795 | (From->getType()->getAs<RecordType>() && |
| 1796 | IsDerivedFrom(From->getType(), ToType))) |
| 1797 | ConstructorsOnly = true; |
| 1798 | |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 1799 | if (RequireCompleteType(From->getLocStart(), ToType, PDiag())) { |
| 1800 | // We're not going to find any constructors. |
| 1801 | } else if (CXXRecordDecl *ToRecordDecl |
| 1802 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1804 | = Context.DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1805 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1806 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1808 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1809 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1810 | NamedDecl *D = *Con; |
| 1811 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 1812 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1813 | // Find the constructor (which may be a template). |
| 1814 | CXXConstructorDecl *Constructor = 0; |
| 1815 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1816 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1817 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1819 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1820 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1821 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1822 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1823 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1824 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1825 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1826 | AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1827 | /*ExplicitArgs*/ 0, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1828 | &From, 1, CandidateSet, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1829 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1830 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 1831 | // Allow one user-defined conversion when user specifies a |
| 1832 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1833 | AddOverloadCandidate(Constructor, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1834 | &From, 1, CandidateSet, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1835 | /*SuppressUserConversions=*/!ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1836 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1837 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1841 | // Enumerate conversion functions, if we're allowed to. |
| 1842 | if (ConstructorsOnly) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1844 | PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1845 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1847 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1849 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1850 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1851 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 1852 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1853 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1854 | E = Conversions->end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1855 | DeclAccessPair FoundDecl = I.getPair(); |
| 1856 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 1857 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 1858 | if (isa<UsingShadowDecl>(D)) |
| 1859 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1860 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1861 | CXXConversionDecl *Conv; |
| 1862 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1863 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 1864 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1865 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1866 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1869 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1870 | AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1871 | ActingContext, From, ToType, |
| 1872 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1873 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1874 | AddConversionCandidate(Conv, FoundDecl, ActingContext, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 1875 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1879 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1880 | |
| 1881 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1882 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1883 | case OR_Success: |
| 1884 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1886 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1887 | // C++ [over.ics.user]p1: |
| 1888 | // If the user-defined conversion is specified by a |
| 1889 | // constructor (12.3.1), the initial standard conversion |
| 1890 | // sequence converts the source type to the type required by |
| 1891 | // the argument of the constructor. |
| 1892 | // |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1893 | QualType ThisType = Constructor->getThisType(Context); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1894 | if (Best->Conversions[0].isEllipsis()) |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1895 | User.EllipsisConversion = true; |
| 1896 | else { |
| 1897 | User.Before = Best->Conversions[0].Standard; |
| 1898 | User.EllipsisConversion = false; |
| 1899 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1900 | User.ConversionFunction = Constructor; |
| 1901 | User.After.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1902 | User.After.setFromType( |
| 1903 | ThisType->getAs<PointerType>()->getPointeeType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1904 | User.After.setAllToTypes(ToType); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1905 | return OR_Success; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1906 | } else if (CXXConversionDecl *Conversion |
| 1907 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1908 | // C++ [over.ics.user]p1: |
| 1909 | // |
| 1910 | // [...] If the user-defined conversion is specified by a |
| 1911 | // conversion function (12.3.2), the initial standard |
| 1912 | // conversion sequence converts the source type to the |
| 1913 | // implicit object parameter of the conversion function. |
| 1914 | User.Before = Best->Conversions[0].Standard; |
| 1915 | User.ConversionFunction = Conversion; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1916 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
| 1918 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1919 | // The second standard conversion sequence converts the |
| 1920 | // result of the user-defined conversion to the target type |
| 1921 | // for the sequence. Since an implicit conversion sequence |
| 1922 | // is an initialization, the special rules for |
| 1923 | // initialization by user-defined conversion apply when |
| 1924 | // selecting the best user-defined conversion for a |
| 1925 | // user-defined conversion sequence (see 13.3.3 and |
| 1926 | // 13.3.3.1). |
| 1927 | User.After = Best->FinalConversion; |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1928 | return OR_Success; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1929 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1930 | assert(false && "Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1931 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1932 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1934 | case OR_No_Viable_Function: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1935 | return OR_No_Viable_Function; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1936 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1937 | // No conversion here! We're done. |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1938 | return OR_Deleted; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1939 | |
| 1940 | case OR_Ambiguous: |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1941 | return OR_Ambiguous; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 1944 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1945 | } |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1946 | |
| 1947 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1948 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1949 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1950 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1951 | OverloadingResult OvResult = |
| 1952 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1953 | CandidateSet, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 1954 | if (OvResult == OR_Ambiguous) |
| 1955 | Diag(From->getSourceRange().getBegin(), |
| 1956 | diag::err_typecheck_ambiguous_condition) |
| 1957 | << From->getType() << ToType << From->getSourceRange(); |
| 1958 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 1959 | Diag(From->getSourceRange().getBegin(), |
| 1960 | diag::err_typecheck_nonviable_condition) |
| 1961 | << From->getType() << ToType << From->getSourceRange(); |
| 1962 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1963 | return false; |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1964 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &From, 1); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 1965 | return true; |
| 1966 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1968 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1969 | /// conversion sequences to determine whether one is better than the |
| 1970 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1972 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1973 | const ImplicitConversionSequence& ICS2) |
| 1974 | { |
| 1975 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1976 | // conversion sequences (as defined in 13.3.3.1) |
| 1977 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1978 | // conversion sequence than a user-defined conversion sequence or |
| 1979 | // an ellipsis conversion sequence, and |
| 1980 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1981 | // conversion sequence than an ellipsis conversion sequence |
| 1982 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1984 | // C++0x [over.best.ics]p10: |
| 1985 | // For the purpose of ranking implicit conversion sequences as |
| 1986 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 1987 | // treated as a user-defined sequence that is indistinguishable |
| 1988 | // from any other user-defined conversion sequence. |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 1989 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 1990 | return ImplicitConversionSequence::Better; |
| 1991 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 1992 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1993 | |
Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 1994 | // The following checks require both conversion sequences to be of |
| 1995 | // the same kind. |
| 1996 | if (ICS1.getKind() != ICS2.getKind()) |
| 1997 | return ImplicitConversionSequence::Indistinguishable; |
| 1998 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1999 | // Two implicit conversion sequences of the same form are |
| 2000 | // indistinguishable conversion sequences unless one of the |
| 2001 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2002 | if (ICS1.isStandard()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2003 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2004 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2005 | // User-defined conversion sequence U1 is a better conversion |
| 2006 | // sequence than another user-defined conversion sequence U2 if |
| 2007 | // they contain the same user-defined conversion function or |
| 2008 | // constructor and if the second standard conversion sequence of |
| 2009 | // U1 is better than the second standard conversion sequence of |
| 2010 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2012 | ICS2.UserDefined.ConversionFunction) |
| 2013 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 2014 | ICS2.UserDefined.After); |
| 2015 | } |
| 2016 | |
| 2017 | return ImplicitConversionSequence::Indistinguishable; |
| 2018 | } |
| 2019 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2020 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2021 | // determine if one is a proper subset of the other. |
| 2022 | static ImplicitConversionSequence::CompareKind |
| 2023 | compareStandardConversionSubsets(ASTContext &Context, |
| 2024 | const StandardConversionSequence& SCS1, |
| 2025 | const StandardConversionSequence& SCS2) { |
| 2026 | ImplicitConversionSequence::CompareKind Result |
| 2027 | = ImplicitConversionSequence::Indistinguishable; |
| 2028 | |
| 2029 | if (SCS1.Second != SCS2.Second) { |
| 2030 | if (SCS1.Second == ICK_Identity) |
| 2031 | Result = ImplicitConversionSequence::Better; |
| 2032 | else if (SCS2.Second == ICK_Identity) |
| 2033 | Result = ImplicitConversionSequence::Worse; |
| 2034 | else |
| 2035 | return ImplicitConversionSequence::Indistinguishable; |
| 2036 | } else if (!Context.hasSameType(SCS1.getToType(1), SCS2.getToType(1))) |
| 2037 | return ImplicitConversionSequence::Indistinguishable; |
| 2038 | |
| 2039 | if (SCS1.Third == SCS2.Third) { |
| 2040 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2041 | : ImplicitConversionSequence::Indistinguishable; |
| 2042 | } |
| 2043 | |
| 2044 | if (SCS1.Third == ICK_Identity) |
| 2045 | return Result == ImplicitConversionSequence::Worse |
| 2046 | ? ImplicitConversionSequence::Indistinguishable |
| 2047 | : ImplicitConversionSequence::Better; |
| 2048 | |
| 2049 | if (SCS2.Third == ICK_Identity) |
| 2050 | return Result == ImplicitConversionSequence::Better |
| 2051 | ? ImplicitConversionSequence::Indistinguishable |
| 2052 | : ImplicitConversionSequence::Worse; |
| 2053 | |
| 2054 | return ImplicitConversionSequence::Indistinguishable; |
| 2055 | } |
| 2056 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2057 | /// CompareStandardConversionSequences - Compare two standard |
| 2058 | /// conversion sequences to determine whether one is better than the |
| 2059 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2061 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 2062 | const StandardConversionSequence& SCS2) |
| 2063 | { |
| 2064 | // Standard conversion sequence S1 is a better conversion sequence |
| 2065 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2066 | |
| 2067 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2068 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2069 | // excluding any Lvalue Transformation; the identity conversion |
| 2070 | // sequence is considered to be a subsequence of any |
| 2071 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2072 | if (ImplicitConversionSequence::CompareKind CK |
| 2073 | = compareStandardConversionSubsets(Context, SCS1, SCS2)) |
| 2074 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2075 | |
| 2076 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2077 | // defined below), or, if not that, |
| 2078 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2079 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2080 | if (Rank1 < Rank2) |
| 2081 | return ImplicitConversionSequence::Better; |
| 2082 | else if (Rank2 < Rank1) |
| 2083 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2085 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2086 | // are indistinguishable unless one of the following rules |
| 2087 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2089 | // A conversion that is not a conversion of a pointer, or |
| 2090 | // pointer to member, to bool is better than another conversion |
| 2091 | // that is such a conversion. |
| 2092 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2093 | return SCS2.isPointerConversionToBool() |
| 2094 | ? ImplicitConversionSequence::Better |
| 2095 | : ImplicitConversionSequence::Worse; |
| 2096 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2097 | // C++ [over.ics.rank]p4b2: |
| 2098 | // |
| 2099 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2100 | // conversion of B* to A* is better than conversion of B* to |
| 2101 | // void*, and conversion of A* to void* is better than conversion |
| 2102 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2104 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2106 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2107 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2108 | // Exactly one of the conversion sequences is a conversion to |
| 2109 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2110 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2111 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2112 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2113 | // Neither conversion sequence converts to a void pointer; compare |
| 2114 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2115 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 2116 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 2117 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2118 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2119 | // Both conversion sequences are conversions to void |
| 2120 | // pointers. Compare the source types to determine if there's an |
| 2121 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2122 | QualType FromType1 = SCS1.getFromType(); |
| 2123 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2124 | |
| 2125 | // Adjust the types we're converting from via the array-to-pointer |
| 2126 | // conversion, if we need to. |
| 2127 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2128 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2129 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2130 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2131 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2132 | QualType FromPointee1 |
| 2133 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2134 | QualType FromPointee2 |
| 2135 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2137 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2138 | return ImplicitConversionSequence::Better; |
| 2139 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2140 | return ImplicitConversionSequence::Worse; |
| 2141 | |
| 2142 | // Objective-C++: If one interface is more specific than the |
| 2143 | // other, it is the better one. |
| 2144 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2145 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2146 | if (FromIface1 && FromIface1) { |
| 2147 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2148 | return ImplicitConversionSequence::Better; |
| 2149 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2150 | return ImplicitConversionSequence::Worse; |
| 2151 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2152 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2153 | |
| 2154 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2155 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2157 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2158 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2160 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2161 | // C++0x [over.ics.rank]p3b4: |
| 2162 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2163 | // implicit object parameter of a non-static member function declared |
| 2164 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 2165 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 2166 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 2167 | // or if the member function in this case has a ref qualifier. |
| 2168 | // (Of course, we don't have ref qualifiers yet.) |
| 2169 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 2170 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 2171 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2172 | |
| 2173 | // C++ [over.ics.rank]p3b4: |
| 2174 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2175 | // which the references refer are the same type except for |
| 2176 | // top-level cv-qualifiers, and the type to which the reference |
| 2177 | // initialized by S2 refers is more cv-qualified than the type |
| 2178 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2179 | QualType T1 = SCS1.getToType(2); |
| 2180 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2181 | T1 = Context.getCanonicalType(T1); |
| 2182 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2183 | Qualifiers T1Quals, T2Quals; |
| 2184 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2185 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2186 | if (UnqualT1 == UnqualT2) { |
| 2187 | // If the type is an array type, promote the element qualifiers to the type |
| 2188 | // for comparison. |
| 2189 | if (isa<ArrayType>(T1) && T1Quals) |
| 2190 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2191 | if (isa<ArrayType>(T2) && T2Quals) |
| 2192 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2193 | if (T2.isMoreQualifiedThan(T1)) |
| 2194 | return ImplicitConversionSequence::Better; |
| 2195 | else if (T1.isMoreQualifiedThan(T2)) |
| 2196 | return ImplicitConversionSequence::Worse; |
| 2197 | } |
| 2198 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2199 | |
| 2200 | return ImplicitConversionSequence::Indistinguishable; |
| 2201 | } |
| 2202 | |
| 2203 | /// CompareQualificationConversions - Compares two standard conversion |
| 2204 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2206 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2207 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2209 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2210 | // -- S1 and S2 differ only in their qualification conversion and |
| 2211 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2212 | // cv-qualification signature of type T1 is a proper subset of |
| 2213 | // the cv-qualification signature of type T2, and S1 is not the |
| 2214 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2215 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2216 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2217 | return ImplicitConversionSequence::Indistinguishable; |
| 2218 | |
| 2219 | // FIXME: the example in the standard doesn't use a qualification |
| 2220 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2221 | QualType T1 = SCS1.getToType(2); |
| 2222 | QualType T2 = SCS2.getToType(2); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2223 | T1 = Context.getCanonicalType(T1); |
| 2224 | T2 = Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2225 | Qualifiers T1Quals, T2Quals; |
| 2226 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2227 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2228 | |
| 2229 | // If the types are the same, we won't learn anything by unwrapped |
| 2230 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2231 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2232 | return ImplicitConversionSequence::Indistinguishable; |
| 2233 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2234 | // If the type is an array type, promote the element qualifiers to the type |
| 2235 | // for comparison. |
| 2236 | if (isa<ArrayType>(T1) && T1Quals) |
| 2237 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2238 | if (isa<ArrayType>(T2) && T2Quals) |
| 2239 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2240 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2241 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2242 | = ImplicitConversionSequence::Indistinguishable; |
| 2243 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 2244 | // Within each iteration of the loop, we check the qualifiers to |
| 2245 | // determine if this still looks like a qualification |
| 2246 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2247 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2248 | // until there are no more pointers or pointers-to-members left |
| 2249 | // to unwrap. This essentially mimics what |
| 2250 | // IsQualificationConversion does, but here we're checking for a |
| 2251 | // strict subset of qualifiers. |
| 2252 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2253 | // The qualifiers are the same, so this doesn't tell us anything |
| 2254 | // about how the sequences rank. |
| 2255 | ; |
| 2256 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2257 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2258 | if (Result == ImplicitConversionSequence::Worse) |
| 2259 | // Neither has qualifiers that are a subset of the other's |
| 2260 | // qualifiers. |
| 2261 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2263 | Result = ImplicitConversionSequence::Better; |
| 2264 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2265 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2266 | if (Result == ImplicitConversionSequence::Better) |
| 2267 | // Neither has qualifiers that are a subset of the other's |
| 2268 | // qualifiers. |
| 2269 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2271 | Result = ImplicitConversionSequence::Worse; |
| 2272 | } else { |
| 2273 | // Qualifiers are disjoint. |
| 2274 | return ImplicitConversionSequence::Indistinguishable; |
| 2275 | } |
| 2276 | |
| 2277 | // If the types after this point are equivalent, we're done. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2278 | if (Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2279 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2282 | // Check that the winning standard conversion sequence isn't using |
| 2283 | // the deprecated string literal array to pointer conversion. |
| 2284 | switch (Result) { |
| 2285 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2286 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2287 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2288 | break; |
| 2289 | |
| 2290 | case ImplicitConversionSequence::Indistinguishable: |
| 2291 | break; |
| 2292 | |
| 2293 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2294 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2295 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2296 | break; |
| 2297 | } |
| 2298 | |
| 2299 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2302 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2303 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2304 | /// various kinds of derived-to-base conversions (C++ |
| 2305 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2306 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2307 | ImplicitConversionSequence::CompareKind |
| 2308 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 2309 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2310 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2311 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2312 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2313 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2314 | |
| 2315 | // Adjust the types we're converting from via the array-to-pointer |
| 2316 | // conversion, if we need to. |
| 2317 | if (SCS1.First == ICK_Array_To_Pointer) |
| 2318 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 2319 | if (SCS2.First == ICK_Array_To_Pointer) |
| 2320 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 2321 | |
| 2322 | // Canonicalize all of the types. |
| 2323 | FromType1 = Context.getCanonicalType(FromType1); |
| 2324 | ToType1 = Context.getCanonicalType(ToType1); |
| 2325 | FromType2 = Context.getCanonicalType(FromType2); |
| 2326 | ToType2 = Context.getCanonicalType(ToType2); |
| 2327 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2328 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2329 | // |
| 2330 | // If class B is derived directly or indirectly from class A and |
| 2331 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2332 | // |
| 2333 | // For Objective-C, we let A, B, and C also be Objective-C |
| 2334 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2335 | |
| 2336 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2338 | SCS2.Second == ICK_Pointer_Conversion && |
| 2339 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2340 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2341 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2343 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2345 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2346 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2347 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2348 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2349 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2350 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2351 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAs<ObjCInterfaceType>(); |
| 2352 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAs<ObjCInterfaceType>(); |
| 2353 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAs<ObjCInterfaceType>(); |
| 2354 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAs<ObjCInterfaceType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2356 | // -- 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] | 2357 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2358 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2359 | return ImplicitConversionSequence::Better; |
| 2360 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2361 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (ToIface1 && ToIface2) { |
| 2364 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 2365 | return ImplicitConversionSequence::Better; |
| 2366 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 2367 | return ImplicitConversionSequence::Worse; |
| 2368 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2369 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2370 | |
| 2371 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2372 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 2373 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2374 | return ImplicitConversionSequence::Better; |
| 2375 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2376 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2378 | if (FromIface1 && FromIface2) { |
| 2379 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 2380 | return ImplicitConversionSequence::Better; |
| 2381 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 2382 | return ImplicitConversionSequence::Worse; |
| 2383 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2384 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2387 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2388 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2389 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2390 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
| 2391 | const MemberPointerType * FromMemPointer1 = |
| 2392 | FromType1->getAs<MemberPointerType>(); |
| 2393 | const MemberPointerType * ToMemPointer1 = |
| 2394 | ToType1->getAs<MemberPointerType>(); |
| 2395 | const MemberPointerType * FromMemPointer2 = |
| 2396 | FromType2->getAs<MemberPointerType>(); |
| 2397 | const MemberPointerType * ToMemPointer2 = |
| 2398 | ToType2->getAs<MemberPointerType>(); |
| 2399 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2400 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2401 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2402 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2403 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2404 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2405 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2406 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2407 | // 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] | 2408 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 2409 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 2410 | return ImplicitConversionSequence::Worse; |
| 2411 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 2412 | return ImplicitConversionSequence::Better; |
| 2413 | } |
| 2414 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2415 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
| 2416 | if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 2417 | return ImplicitConversionSequence::Better; |
| 2418 | else if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 2419 | return ImplicitConversionSequence::Worse; |
| 2420 | } |
| 2421 | } |
| 2422 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2423 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2424 | // -- conversion of C to B is better than conversion of C to A, |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2425 | // -- binding of an expression of type C to a reference of type |
| 2426 | // B& is better than binding an expression of type C to a |
| 2427 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2428 | if (Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2429 | !Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2430 | if (IsDerivedFrom(ToType1, ToType2)) |
| 2431 | return ImplicitConversionSequence::Better; |
| 2432 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 2433 | return ImplicitConversionSequence::Worse; |
| 2434 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2435 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2436 | // -- conversion of B to A is better than conversion of C to A. |
Douglas Gregor | 83af86a | 2010-02-25 19:01:05 +0000 | [diff] [blame] | 2437 | // -- binding of an expression of type B to a reference of type |
| 2438 | // A& is better than binding an expression of type C to a |
| 2439 | // reference of type A&, |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2440 | if (!Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2441 | Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2442 | if (IsDerivedFrom(FromType2, FromType1)) |
| 2443 | return ImplicitConversionSequence::Better; |
| 2444 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 2445 | return ImplicitConversionSequence::Worse; |
| 2446 | } |
| 2447 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2449 | return ImplicitConversionSequence::Indistinguishable; |
| 2450 | } |
| 2451 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2452 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2453 | /// determine whether they are reference-related, |
| 2454 | /// reference-compatible, reference-compatible with added |
| 2455 | /// qualification, or incompatible, for use in C++ initialization by |
| 2456 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2457 | /// type, and the first type (T1) is the pointee type of the reference |
| 2458 | /// type being initialized. |
| 2459 | Sema::ReferenceCompareResult |
| 2460 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2461 | QualType OrigT1, QualType OrigT2, |
| 2462 | bool& DerivedToBase) { |
| 2463 | assert(!OrigT1->isReferenceType() && |
| 2464 | "T1 must be the pointee type of the reference type"); |
| 2465 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2466 | |
| 2467 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2468 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2469 | Qualifiers T1Quals, T2Quals; |
| 2470 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2471 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2472 | |
| 2473 | // C++ [dcl.init.ref]p4: |
| 2474 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2475 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2476 | // T1 is a base class of T2. |
| 2477 | if (UnqualT1 == UnqualT2) |
| 2478 | DerivedToBase = false; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2479 | else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2480 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 2481 | DerivedToBase = true; |
| 2482 | else |
| 2483 | return Ref_Incompatible; |
| 2484 | |
| 2485 | // At this point, we know that T1 and T2 are reference-related (at |
| 2486 | // least). |
| 2487 | |
| 2488 | // If the type is an array type, promote the element qualifiers to the type |
| 2489 | // for comparison. |
| 2490 | if (isa<ArrayType>(T1) && T1Quals) |
| 2491 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 2492 | if (isa<ArrayType>(T2) && T2Quals) |
| 2493 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 2494 | |
| 2495 | // C++ [dcl.init.ref]p4: |
| 2496 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 2497 | // reference-related to T2 and cv1 is the same cv-qualification |
| 2498 | // as, or greater cv-qualification than, cv2. For purposes of |
| 2499 | // overload resolution, cases for which cv1 is greater |
| 2500 | // cv-qualification than cv2 are identified as |
| 2501 | // reference-compatible with added qualification (see 13.3.3.2). |
| 2502 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 2503 | return Ref_Compatible; |
| 2504 | else if (T1.isMoreQualifiedThan(T2)) |
| 2505 | return Ref_Compatible_With_Added_Qualification; |
| 2506 | else |
| 2507 | return Ref_Related; |
| 2508 | } |
| 2509 | |
| 2510 | /// \brief Compute an implicit conversion sequence for reference |
| 2511 | /// initialization. |
| 2512 | static ImplicitConversionSequence |
| 2513 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 2514 | SourceLocation DeclLoc, |
| 2515 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2516 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2517 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 2518 | |
| 2519 | // Most paths end in a failed conversion. |
| 2520 | ImplicitConversionSequence ICS; |
| 2521 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 2522 | |
| 2523 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 2524 | QualType T2 = Init->getType(); |
| 2525 | |
| 2526 | // If the initializer is the address of an overloaded function, try |
| 2527 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2528 | // type of the resulting function. |
| 2529 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2530 | DeclAccessPair Found; |
| 2531 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 2532 | false, Found)) |
| 2533 | T2 = Fn->getType(); |
| 2534 | } |
| 2535 | |
| 2536 | // Compute some basic properties of the types and the initializer. |
| 2537 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 2538 | bool DerivedToBase = false; |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2539 | Expr::isLvalueResult InitLvalue = Init->isLvalue(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2540 | Sema::ReferenceCompareResult RefRelationship |
| 2541 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
| 2542 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2543 | |
| 2544 | // C++ [over.ics.ref]p3: |
| 2545 | // Except for an implicit object parameter, for which see 13.3.1, |
| 2546 | // a standard conversion sequence cannot be formed if it requires |
| 2547 | // binding an lvalue reference to non-const to an rvalue or |
| 2548 | // binding an rvalue reference to an lvalue. |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2549 | // |
| 2550 | // FIXME: DPG doesn't trust this code. It seems far too early to |
| 2551 | // abort because of a binding of an rvalue reference to an lvalue. |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2552 | if (isRValRef && InitLvalue == Expr::LV_Valid) |
| 2553 | return ICS; |
| 2554 | |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2555 | // C++0x [dcl.init.ref]p16: |
| 2556 | // A reference to type "cv1 T1" is initialized by an expression |
| 2557 | // of type "cv2 T2" as follows: |
| 2558 | |
| 2559 | // -- If the initializer expression |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2560 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2561 | // reference-compatible with "cv2 T2," or |
| 2562 | // |
| 2563 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 2564 | if (InitLvalue == Expr::LV_Valid && |
| 2565 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2566 | // C++ [over.ics.ref]p1: |
| 2567 | // When a parameter of reference type binds directly (8.5.3) |
| 2568 | // to an argument expression, the implicit conversion sequence |
| 2569 | // is the identity conversion, unless the argument expression |
| 2570 | // has a type that is a derived class of the parameter type, |
| 2571 | // in which case the implicit conversion sequence is a |
| 2572 | // derived-to-base Conversion (13.3.3.1). |
| 2573 | ICS.setStandard(); |
| 2574 | ICS.Standard.First = ICK_Identity; |
| 2575 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2576 | ICS.Standard.Third = ICK_Identity; |
| 2577 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2578 | ICS.Standard.setToType(0, T2); |
| 2579 | ICS.Standard.setToType(1, T1); |
| 2580 | ICS.Standard.setToType(2, T1); |
| 2581 | ICS.Standard.ReferenceBinding = true; |
| 2582 | ICS.Standard.DirectBinding = true; |
| 2583 | ICS.Standard.RRefBinding = false; |
| 2584 | ICS.Standard.CopyConstructor = 0; |
| 2585 | |
| 2586 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 2587 | // derived-to-base conversions is suppressed when we're |
| 2588 | // computing the implicit conversion sequence (C++ |
| 2589 | // [over.best.ics]p2). |
| 2590 | return ICS; |
| 2591 | } |
| 2592 | |
| 2593 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 2594 | // not reference-related to T2, and can be implicitly |
| 2595 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 2596 | // is reference-compatible with "cv3 T3" 92) (this |
| 2597 | // conversion is selected by enumerating the applicable |
| 2598 | // conversion functions (13.3.1.6) and choosing the best |
| 2599 | // one through overload resolution (13.3)), |
| 2600 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
| 2601 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
| 2602 | RefRelationship == Sema::Ref_Incompatible) { |
| 2603 | CXXRecordDecl *T2RecordDecl |
| 2604 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 2605 | |
| 2606 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 2607 | const UnresolvedSetImpl *Conversions |
| 2608 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 2609 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 2610 | E = Conversions->end(); I != E; ++I) { |
| 2611 | NamedDecl *D = *I; |
| 2612 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2613 | if (isa<UsingShadowDecl>(D)) |
| 2614 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2615 | |
| 2616 | FunctionTemplateDecl *ConvTemplate |
| 2617 | = dyn_cast<FunctionTemplateDecl>(D); |
| 2618 | CXXConversionDecl *Conv; |
| 2619 | if (ConvTemplate) |
| 2620 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2621 | else |
| 2622 | Conv = cast<CXXConversionDecl>(D); |
| 2623 | |
| 2624 | // If the conversion function doesn't return a reference type, |
| 2625 | // it can't be considered for this conversion. |
| 2626 | if (Conv->getConversionType()->isLValueReferenceType() && |
| 2627 | (AllowExplicit || !Conv->isExplicit())) { |
| 2628 | if (ConvTemplate) |
| 2629 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
| 2630 | Init, DeclType, CandidateSet); |
| 2631 | else |
| 2632 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
| 2633 | DeclType, CandidateSet); |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | OverloadCandidateSet::iterator Best; |
| 2638 | switch (S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2639 | case OR_Success: |
| 2640 | // C++ [over.ics.ref]p1: |
| 2641 | // |
| 2642 | // [...] If the parameter binds directly to the result of |
| 2643 | // applying a conversion function to the argument |
| 2644 | // expression, the implicit conversion sequence is a |
| 2645 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 2646 | // second standard conversion sequence either an identity |
| 2647 | // conversion or, if the conversion function returns an |
| 2648 | // entity of a type that is a derived class of the parameter |
| 2649 | // type, a derived-to-base Conversion. |
| 2650 | if (!Best->FinalConversion.DirectBinding) |
| 2651 | break; |
| 2652 | |
| 2653 | ICS.setUserDefined(); |
| 2654 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 2655 | ICS.UserDefined.After = Best->FinalConversion; |
| 2656 | ICS.UserDefined.ConversionFunction = Best->Function; |
| 2657 | ICS.UserDefined.EllipsisConversion = false; |
| 2658 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 2659 | ICS.UserDefined.After.DirectBinding && |
| 2660 | "Expected a direct reference binding!"); |
| 2661 | return ICS; |
| 2662 | |
| 2663 | case OR_Ambiguous: |
| 2664 | ICS.setAmbiguous(); |
| 2665 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 2666 | Cand != CandidateSet.end(); ++Cand) |
| 2667 | if (Cand->Viable) |
| 2668 | ICS.Ambiguous.addConversion(Cand->Function); |
| 2669 | return ICS; |
| 2670 | |
| 2671 | case OR_No_Viable_Function: |
| 2672 | case OR_Deleted: |
| 2673 | // There was no suitable conversion, or we found a deleted |
| 2674 | // conversion; continue with other checks. |
| 2675 | break; |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | // -- Otherwise, the reference shall be to a non-volatile const |
| 2680 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 2681 | // rvalue reference and the initializer expression shall be an rvalue. |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 2682 | // |
| 2683 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 2684 | // point, which is that, due to p2 (which short-circuits reference |
| 2685 | // binding by only attempting a simple conversion for non-direct |
| 2686 | // bindings) and p3's strange wording, we allow a const volatile |
| 2687 | // reference to bind to an rvalue. Hence the check for the presence |
| 2688 | // of "const" rather than checking for "const" being the only |
| 2689 | // qualifier. |
| 2690 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2691 | return ICS; |
| 2692 | |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2693 | // -- if T2 is a class type and |
| 2694 | // -- the initializer expression is an rvalue and "cv1 T1" |
| 2695 | // is reference-compatible with "cv2 T2," or |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2696 | // |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2697 | // -- T1 is not reference-related to T2 and the initializer |
| 2698 | // expression can be implicitly converted to an rvalue |
| 2699 | // of type "cv3 T3" (this conversion is selected by |
| 2700 | // enumerating the applicable conversion functions |
| 2701 | // (13.3.1.6) and choosing the best one through overload |
| 2702 | // resolution (13.3)), |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2703 | // |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2704 | // then the reference is bound to the initializer |
| 2705 | // expression rvalue in the first case and to the object |
| 2706 | // that is the result of the conversion in the second case |
| 2707 | // (or, in either case, to the appropriate base class |
| 2708 | // subobject of the object). |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2709 | // |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2710 | // We're only checking the first case here, which is a direct |
| 2711 | // binding in C++0x but not in C++03. |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2712 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
| 2713 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2714 | ICS.setStandard(); |
| 2715 | ICS.Standard.First = ICK_Identity; |
| 2716 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 2717 | ICS.Standard.Third = ICK_Identity; |
| 2718 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 2719 | ICS.Standard.setToType(0, T2); |
| 2720 | ICS.Standard.setToType(1, T1); |
| 2721 | ICS.Standard.setToType(2, T1); |
| 2722 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | f93df19 | 2010-04-18 08:46:23 +0000 | [diff] [blame] | 2723 | ICS.Standard.DirectBinding = S.getLangOptions().CPlusPlus0x; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2724 | ICS.Standard.RRefBinding = isRValRef; |
| 2725 | ICS.Standard.CopyConstructor = 0; |
| 2726 | return ICS; |
| 2727 | } |
| 2728 | |
| 2729 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 2730 | // initialized from the initializer expression using the |
| 2731 | // rules for a non-reference copy initialization (8.5). The |
| 2732 | // reference is then bound to the temporary. If T1 is |
| 2733 | // reference-related to T2, cv1 must be the same |
| 2734 | // cv-qualification as, or greater cv-qualification than, |
| 2735 | // cv2; otherwise, the program is ill-formed. |
| 2736 | if (RefRelationship == Sema::Ref_Related) { |
| 2737 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 2738 | // we would be reference-compatible or reference-compatible with |
| 2739 | // added qualification. But that wasn't the case, so the reference |
| 2740 | // initialization fails. |
| 2741 | return ICS; |
| 2742 | } |
| 2743 | |
| 2744 | // If at least one of the types is a class type, the types are not |
| 2745 | // related, and we aren't allowed any user conversions, the |
| 2746 | // reference binding fails. This case is important for breaking |
| 2747 | // recursion, since TryImplicitConversion below will attempt to |
| 2748 | // create a temporary through the use of a copy constructor. |
| 2749 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 2750 | (T1->isRecordType() || T2->isRecordType())) |
| 2751 | return ICS; |
| 2752 | |
| 2753 | // C++ [over.ics.ref]p2: |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2754 | // When a parameter of reference type is not bound directly to |
| 2755 | // an argument expression, the conversion sequence is the one |
| 2756 | // required to convert the argument expression to the |
| 2757 | // underlying type of the reference according to |
| 2758 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 2759 | // to copy-initializing a temporary of the underlying type with |
| 2760 | // the argument expression. Any difference in top-level |
| 2761 | // cv-qualification is subsumed by the initialization itself |
| 2762 | // and does not constitute a conversion. |
| 2763 | ICS = S.TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 2764 | /*AllowExplicit=*/false, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2765 | /*InOverloadResolution=*/false); |
| 2766 | |
| 2767 | // Of course, that's still a reference binding. |
| 2768 | if (ICS.isStandard()) { |
| 2769 | ICS.Standard.ReferenceBinding = true; |
| 2770 | ICS.Standard.RRefBinding = isRValRef; |
| 2771 | } else if (ICS.isUserDefined()) { |
| 2772 | ICS.UserDefined.After.ReferenceBinding = true; |
| 2773 | ICS.UserDefined.After.RRefBinding = isRValRef; |
| 2774 | } |
| 2775 | return ICS; |
| 2776 | } |
| 2777 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2778 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 2779 | /// ToType from the expression From. Return the implicit conversion |
| 2780 | /// sequence required to pass this argument, which may be a bad |
| 2781 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2782 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 2783 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2784 | static ImplicitConversionSequence |
| 2785 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
Douglas Gregor | dcd27ff | 2010-04-16 17:53:55 +0000 | [diff] [blame] | 2786 | bool SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2787 | bool InOverloadResolution) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2788 | if (ToType->isReferenceType()) |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2789 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2790 | /*FIXME:*/From->getLocStart(), |
| 2791 | SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 2792 | /*AllowExplicit=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2794 | return S.TryImplicitConversion(From, ToType, |
| 2795 | SuppressUserConversions, |
| 2796 | /*AllowExplicit=*/false, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 2797 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2800 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2801 | /// parameter of the given member function (@c Method) from the |
| 2802 | /// expression @p From. |
| 2803 | ImplicitConversionSequence |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2804 | Sema::TryObjectArgumentInitialization(QualType OrigFromType, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2805 | CXXMethodDecl *Method, |
| 2806 | CXXRecordDecl *ActingContext) { |
| 2807 | QualType ClassType = Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2808 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 2809 | // const volatile object. |
| 2810 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 2811 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
| 2812 | QualType ImplicitParamType = Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2813 | |
| 2814 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2815 | // to exit early. |
| 2816 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2817 | |
| 2818 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 2819 | QualType FromType = OrigFromType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2820 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2821 | FromType = PT->getPointeeType(); |
| 2822 | |
| 2823 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2824 | |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 2825 | // The implicit object parameter is has the type "reference to cv X", |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2826 | // where X is the class of which the function is a member |
| 2827 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2828 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2829 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2830 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2831 | // reference binding here, that allows class rvalues to bind to |
| 2832 | // non-constant references. |
| 2833 | |
| 2834 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2835 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2836 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2837 | if (ImplicitParamType.getCVRQualifiers() |
| 2838 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2839 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2840 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 2841 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2842 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2843 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2844 | |
| 2845 | // Check that we have either the same type or a derived type. It |
| 2846 | // affects the conversion rank. |
| 2847 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2848 | ImplicitConversionKind SecondKind; |
| 2849 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 2850 | SecondKind = ICK_Identity; |
| 2851 | } else if (IsDerivedFrom(FromType, ClassType)) |
| 2852 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2853 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2854 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 2855 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2856 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 2857 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2858 | |
| 2859 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2860 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2861 | ICS.Standard.setAsIdentityConversion(); |
| 2862 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2863 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2864 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2865 | ICS.Standard.ReferenceBinding = true; |
| 2866 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2867 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2868 | return ICS; |
| 2869 | } |
| 2870 | |
| 2871 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2872 | /// the implicit object parameter for the given Method with the given |
| 2873 | /// expression. |
| 2874 | bool |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2875 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 2876 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2877 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2878 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2879 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2881 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2883 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2884 | FromRecordType = PT->getPointeeType(); |
| 2885 | DestType = Method->getThisType(Context); |
| 2886 | } else { |
| 2887 | FromRecordType = From->getType(); |
| 2888 | DestType = ImplicitParamRecordType; |
| 2889 | } |
| 2890 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2891 | // Note that we always use the true parent context when performing |
| 2892 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2893 | ImplicitConversionSequence ICS |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2894 | = TryObjectArgumentInitialization(From->getType(), Method, |
| 2895 | Method->getParent()); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2896 | if (ICS.isBad()) |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2897 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2898 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2899 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2901 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2902 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2904 | if (!Context.hasSameType(From->getType(), DestType)) |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 2905 | ImpCastExprToType(From, DestType, CastExpr::CK_NoOp, |
| 2906 | /*isLvalue=*/!From->getType()->isPointerType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2907 | return false; |
| 2908 | } |
| 2909 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2910 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2911 | /// expression From to bool (C++0x [conv]p3). |
| 2912 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2914 | // FIXME: Are these flags correct? |
| 2915 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2917 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2921 | /// of the expression From to bool (C++0x [conv]p3). |
| 2922 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2923 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2924 | if (!ICS.isBad()) |
| 2925 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2926 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2927 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2928 | return Diag(From->getSourceRange().getBegin(), |
| 2929 | diag::err_typecheck_bool_condition) |
| 2930 | << From->getType() << From->getSourceRange(); |
| 2931 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2934 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2935 | /// candidate functions, using the given function call arguments. If |
| 2936 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2937 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2938 | /// |
| 2939 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 2940 | /// based on an incomplete set of function arguments. This feature is used by |
| 2941 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | void |
| 2943 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2944 | DeclAccessPair FoundDecl, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2945 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2946 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2947 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2948 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2950 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2951 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2952 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2953 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2955 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2956 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2957 | // If we get here, it's because we're calling a member function |
| 2958 | // that is named without a member access expression (e.g., |
| 2959 | // "this->f") that was either written explicitly or created |
| 2960 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2961 | // function, e.g., X::f(). We use an empty type for the implied |
| 2962 | // object argument (C++ [over.call.func]p3), and the acting context |
| 2963 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2964 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2965 | QualType(), Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 2966 | SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2967 | return; |
| 2968 | } |
| 2969 | // We treat a constructor like a non-member function, since its object |
| 2970 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2973 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 2974 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2975 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2976 | // Overload resolution is always an unevaluated context. |
| 2977 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 2978 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2979 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 2980 | // C++ [class.copy]p3: |
| 2981 | // A member function template is never instantiated to perform the copy |
| 2982 | // of a class object to an object of its class type. |
| 2983 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
| 2984 | if (NumArgs == 1 && |
| 2985 | Constructor->isCopyConstructorLikeSpecialization() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 2986 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 2987 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2988 | return; |
| 2989 | } |
| 2990 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2991 | // Add this candidate |
| 2992 | CandidateSet.push_back(OverloadCandidate()); |
| 2993 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2994 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2995 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2996 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2997 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2998 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2999 | |
| 3000 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3001 | |
| 3002 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3003 | // parameters is viable only if it has an ellipsis in its parameter |
| 3004 | // list (8.3.5). |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3005 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
| 3006 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3007 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3008 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3009 | return; |
| 3010 | } |
| 3011 | |
| 3012 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3013 | // is viable only if the (m+1)st parameter has a default argument |
| 3014 | // (8.3.6). For the purposes of overload resolution, the |
| 3015 | // parameter list is truncated on the right, so that there are |
| 3016 | // exactly m parameters. |
| 3017 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3018 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3019 | // Not enough arguments. |
| 3020 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3021 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3022 | return; |
| 3023 | } |
| 3024 | |
| 3025 | // Determine the implicit conversion sequences for each of the |
| 3026 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3027 | Candidate.Conversions.resize(NumArgs); |
| 3028 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3029 | if (ArgIdx < NumArgsInProto) { |
| 3030 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3031 | // exist for each argument an implicit conversion sequence |
| 3032 | // (13.3.3.1) that converts that argument to the corresponding |
| 3033 | // parameter of F. |
| 3034 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3036 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 3037 | SuppressUserConversions, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3038 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3039 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3040 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3041 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3042 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3043 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3044 | } else { |
| 3045 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3046 | // argument for which there is no corresponding parameter is |
| 3047 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3048 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3053 | /// \brief Add all of the function declarations in the given function set to |
| 3054 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3055 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3056 | Expr **Args, unsigned NumArgs, |
| 3057 | OverloadCandidateSet& CandidateSet, |
| 3058 | bool SuppressUserConversions) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3059 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3060 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3061 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3062 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3063 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3064 | cast<CXXMethodDecl>(FD)->getParent(), |
| 3065 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3066 | CandidateSet, SuppressUserConversions); |
| 3067 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3068 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3069 | SuppressUserConversions); |
| 3070 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3071 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3072 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3073 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3074 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3075 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3076 | /*FIXME: explicit args */ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3077 | Args[0]->getType(), Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3078 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3079 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3080 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3081 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3082 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3083 | Args, NumArgs, CandidateSet, |
| 3084 | SuppressUserConversions); |
| 3085 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3086 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3089 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3090 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3091 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3092 | QualType ObjectType, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3093 | Expr **Args, unsigned NumArgs, |
| 3094 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3095 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3096 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3097 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3098 | |
| 3099 | if (isa<UsingShadowDecl>(Decl)) |
| 3100 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
| 3101 | |
| 3102 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3103 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3104 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3105 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3106 | /*ExplicitArgs*/ 0, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3107 | ObjectType, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3108 | CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3109 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3110 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3111 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3112 | ObjectType, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3113 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3117 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3118 | /// of candidate functions, using the given function call arguments |
| 3119 | /// and the object argument (@c Object). For example, in a call |
| 3120 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 3121 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 3122 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3123 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3125 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3126 | CXXRecordDecl *ActingContext, QualType ObjectType, |
| 3127 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3128 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3129 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3131 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3132 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3133 | assert(!isa<CXXConstructorDecl>(Method) && |
| 3134 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3135 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3136 | if (!CandidateSet.isNewCandidate(Method)) |
| 3137 | return; |
| 3138 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3139 | // Overload resolution is always an unevaluated context. |
| 3140 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3141 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3142 | // Add this candidate |
| 3143 | CandidateSet.push_back(OverloadCandidate()); |
| 3144 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3145 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3146 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3147 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3148 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3149 | |
| 3150 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3151 | |
| 3152 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3153 | // parameters is viable only if it has an ellipsis in its parameter |
| 3154 | // list (8.3.5). |
| 3155 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3156 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3157 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3158 | return; |
| 3159 | } |
| 3160 | |
| 3161 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3162 | // is viable only if the (m+1)st parameter has a default argument |
| 3163 | // (8.3.6). For the purposes of overload resolution, the |
| 3164 | // parameter list is truncated on the right, so that there are |
| 3165 | // exactly m parameters. |
| 3166 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 3167 | if (NumArgs < MinRequiredArgs) { |
| 3168 | // Not enough arguments. |
| 3169 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3170 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3171 | return; |
| 3172 | } |
| 3173 | |
| 3174 | Candidate.Viable = true; |
| 3175 | Candidate.Conversions.resize(NumArgs + 1); |
| 3176 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3177 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3178 | // The implicit object argument is ignored. |
| 3179 | Candidate.IgnoreObjectArgument = true; |
| 3180 | else { |
| 3181 | // Determine the implicit conversion sequence for the object |
| 3182 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3183 | Candidate.Conversions[0] |
| 3184 | = TryObjectArgumentInitialization(ObjectType, Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3185 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3186 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3187 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3188 | return; |
| 3189 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | // Determine the implicit conversion sequences for each of the |
| 3193 | // arguments. |
| 3194 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3195 | if (ArgIdx < NumArgsInProto) { |
| 3196 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3197 | // exist for each argument an implicit conversion sequence |
| 3198 | // (13.3.3.1) that converts that argument to the corresponding |
| 3199 | // parameter of F. |
| 3200 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3202 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3203 | SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 3204 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3205 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3206 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3207 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3208 | break; |
| 3209 | } |
| 3210 | } else { |
| 3211 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3212 | // argument for which there is no corresponding parameter is |
| 3213 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3214 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3215 | } |
| 3216 | } |
| 3217 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 3218 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3219 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 3220 | /// set, using template argument deduction to produce an appropriate member |
| 3221 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3223 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3224 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3225 | CXXRecordDecl *ActingContext, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3226 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3227 | QualType ObjectType, |
| 3228 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3229 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3230 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3231 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 3232 | return; |
| 3233 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3234 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3236 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | // 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] | 3238 | // candidate functions in the usual way.113) A given name can refer to one |
| 3239 | // or more function templates and also to a set of overloaded non-template |
| 3240 | // functions. In such a case, the candidate functions generated from each |
| 3241 | // function template are combined with the set of non-template candidate |
| 3242 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3243 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3244 | FunctionDecl *Specialization = 0; |
| 3245 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3246 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3247 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3248 | CandidateSet.push_back(OverloadCandidate()); |
| 3249 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3250 | Candidate.FoundDecl = FoundDecl; |
| 3251 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 3252 | Candidate.Viable = false; |
| 3253 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3254 | Candidate.IsSurrogate = false; |
| 3255 | Candidate.IgnoreObjectArgument = false; |
| 3256 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3257 | Info); |
| 3258 | return; |
| 3259 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3261 | // Add the function template specialization produced by template argument |
| 3262 | // deduction as a candidate. |
| 3263 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3265 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3266 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3267 | ActingContext, ObjectType, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3268 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3271 | /// \brief Add a C++ function template specialization as a candidate |
| 3272 | /// in the candidate set, using template argument deduction to produce |
| 3273 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3275 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3276 | DeclAccessPair FoundDecl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3277 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3278 | Expr **Args, unsigned NumArgs, |
| 3279 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3280 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3281 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3282 | return; |
| 3283 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3284 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3286 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | // 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] | 3288 | // candidate functions in the usual way.113) A given name can refer to one |
| 3289 | // or more function templates and also to a set of overloaded non-template |
| 3290 | // functions. In such a case, the candidate functions generated from each |
| 3291 | // function template are combined with the set of non-template candidate |
| 3292 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3293 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3294 | FunctionDecl *Specialization = 0; |
| 3295 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3296 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3297 | Args, NumArgs, Specialization, Info)) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3298 | CandidateSet.push_back(OverloadCandidate()); |
| 3299 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3300 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3301 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3302 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3303 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 3304 | Candidate.IsSurrogate = false; |
| 3305 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3306 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3307 | Info); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3308 | return; |
| 3309 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3311 | // Add the function template specialization produced by template argument |
| 3312 | // deduction as a candidate. |
| 3313 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3314 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3315 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3318 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3320 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | /// 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] | 3322 | /// (which may or may not be the same type as the type that the |
| 3323 | /// conversion function produces). |
| 3324 | void |
| 3325 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3326 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3327 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3328 | Expr *From, QualType ToType, |
| 3329 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3330 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 3331 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3332 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3333 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3334 | return; |
| 3335 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3336 | // Overload resolution is always an unevaluated context. |
| 3337 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3338 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3339 | // Add this candidate |
| 3340 | CandidateSet.push_back(OverloadCandidate()); |
| 3341 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3342 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3343 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3344 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3345 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3346 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3347 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3348 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3350 | // Determine the implicit conversion sequence for the implicit |
| 3351 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3352 | Candidate.Viable = true; |
| 3353 | Candidate.Conversions.resize(1); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3354 | Candidate.Conversions[0] |
| 3355 | = TryObjectArgumentInitialization(From->getType(), Conversion, |
| 3356 | ActingContext); |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 3357 | // Conversion functions to a different type in the base class is visible in |
| 3358 | // the derived class. So, a derived to base conversion should not participate |
| 3359 | // in overload resolution. |
| 3360 | if (Candidate.Conversions[0].Standard.Second == ICK_Derived_To_Base) |
| 3361 | Candidate.Conversions[0].Standard.Second = ICK_Identity; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3362 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3363 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3364 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3365 | return; |
| 3366 | } |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3367 | |
| 3368 | // We won't go through a user-define type conversion function to convert a |
| 3369 | // derived to base as such conversions are given Conversion Rank. They only |
| 3370 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 3371 | QualType FromCanon |
| 3372 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 3373 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 3374 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 3375 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3376 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 3377 | return; |
| 3378 | } |
| 3379 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3380 | // To determine what the conversion from the result of calling the |
| 3381 | // conversion function to the type we're eventually trying to |
| 3382 | // convert to (ToType), we need to synthesize a call to the |
| 3383 | // conversion function and attempt copy initialization from it. This |
| 3384 | // makes sure that we get the right semantics with respect to |
| 3385 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 3386 | // call on the stack and we don't need its arguments to be |
| 3387 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3389 | From->getLocStart()); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3390 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3391 | CastExpr::CK_FunctionToPointerDecay, |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 3392 | &ConversionRef, CXXBaseSpecifierArray(), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
| 3394 | // 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] | 3395 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 3396 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3398 | Conversion->getConversionType().getNonReferenceType(), |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 3399 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | ImplicitConversionSequence ICS = |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3401 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3402 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3403 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3405 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3406 | case ImplicitConversionSequence::StandardConversion: |
| 3407 | Candidate.FinalConversion = ICS.Standard; |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 3408 | |
| 3409 | // C++ [over.ics.user]p3: |
| 3410 | // If the user-defined conversion is specified by a specialization of a |
| 3411 | // conversion function template, the second standard conversion sequence |
| 3412 | // shall have exact match rank. |
| 3413 | if (Conversion->getPrimaryTemplate() && |
| 3414 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 3415 | Candidate.Viable = false; |
| 3416 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 3417 | } |
| 3418 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | |
| 3421 | case ImplicitConversionSequence::BadConversion: |
| 3422 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3423 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | |
| 3426 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3428 | "Can only end up with a standard conversion sequence or failure"); |
| 3429 | } |
| 3430 | } |
| 3431 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3432 | /// \brief Adds a conversion function template specialization |
| 3433 | /// candidate to the overload set, using template argument deduction |
| 3434 | /// to deduce the template arguments of the conversion function |
| 3435 | /// template from the type that we are converting to (C++ |
| 3436 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3438 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3439 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3440 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3441 | Expr *From, QualType ToType, |
| 3442 | OverloadCandidateSet &CandidateSet) { |
| 3443 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 3444 | "Only conversion function templates permitted here"); |
| 3445 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3446 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 3447 | return; |
| 3448 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3449 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3450 | CXXConversionDecl *Specialization = 0; |
| 3451 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3452 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3453 | Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 3454 | CandidateSet.push_back(OverloadCandidate()); |
| 3455 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 3456 | Candidate.FoundDecl = FoundDecl; |
| 3457 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 3458 | Candidate.Viable = false; |
| 3459 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 3460 | Candidate.IsSurrogate = false; |
| 3461 | Candidate.IgnoreObjectArgument = false; |
| 3462 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
| 3463 | Info); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3464 | return; |
| 3465 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3467 | // Add the conversion function template specialization produced by |
| 3468 | // template argument deduction as a candidate. |
| 3469 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3470 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3471 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3474 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 3475 | /// converts the given @c Object to a function pointer via the |
| 3476 | /// conversion function @c Conversion, and then attempts to call it |
| 3477 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 3478 | /// the type of function that we'll eventually be calling. |
| 3479 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3480 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3481 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3482 | const FunctionProtoType *Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3483 | QualType ObjectType, |
| 3484 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3485 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3486 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 3487 | return; |
| 3488 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3489 | // Overload resolution is always an unevaluated context. |
| 3490 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3491 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3492 | CandidateSet.push_back(OverloadCandidate()); |
| 3493 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3494 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3495 | Candidate.Function = 0; |
| 3496 | Candidate.Surrogate = Conversion; |
| 3497 | Candidate.Viable = true; |
| 3498 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3499 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3500 | Candidate.Conversions.resize(NumArgs + 1); |
| 3501 | |
| 3502 | // Determine the implicit conversion sequence for the implicit |
| 3503 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | ImplicitConversionSequence ObjectInit |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3505 | = TryObjectArgumentInitialization(ObjectType, Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3506 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3507 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3508 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 3509 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3510 | return; |
| 3511 | } |
| 3512 | |
| 3513 | // The first conversion is actually a user-defined conversion whose |
| 3514 | // first conversion is ObjectInit's standard conversion (which is |
| 3515 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3516 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3517 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3518 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3519 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3521 | = Candidate.Conversions[0].UserDefined.Before; |
| 3522 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 3523 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3525 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3526 | |
| 3527 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3528 | // parameters is viable only if it has an ellipsis in its parameter |
| 3529 | // list (8.3.5). |
| 3530 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 3531 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3532 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
| 3535 | |
| 3536 | // Function types don't have any default arguments, so just check if |
| 3537 | // we have enough arguments. |
| 3538 | if (NumArgs < NumArgsInProto) { |
| 3539 | // Not enough arguments. |
| 3540 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3541 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3542 | return; |
| 3543 | } |
| 3544 | |
| 3545 | // Determine the implicit conversion sequences for each of the |
| 3546 | // arguments. |
| 3547 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3548 | if (ArgIdx < NumArgsInProto) { |
| 3549 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3550 | // exist for each argument an implicit conversion sequence |
| 3551 | // (13.3.3.1) that converts that argument to the corresponding |
| 3552 | // parameter of F. |
| 3553 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3555 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3556 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3557 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3558 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3559 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3560 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3561 | break; |
| 3562 | } |
| 3563 | } else { |
| 3564 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3565 | // argument for which there is no corresponding parameter is |
| 3566 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3567 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | } |
| 3571 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3572 | /// \brief Add overload candidates for overloaded operators that are |
| 3573 | /// member functions. |
| 3574 | /// |
| 3575 | /// Add the overloaded operator candidates that are member functions |
| 3576 | /// for the operator Op that was used in an operator expression such |
| 3577 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 3578 | /// CandidateSet will store the added overload candidates. (C++ |
| 3579 | /// [over.match.oper]). |
| 3580 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 3581 | SourceLocation OpLoc, |
| 3582 | Expr **Args, unsigned NumArgs, |
| 3583 | OverloadCandidateSet& CandidateSet, |
| 3584 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3585 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 3586 | |
| 3587 | // C++ [over.match.oper]p3: |
| 3588 | // For a unary operator @ with an operand of a type whose |
| 3589 | // cv-unqualified version is T1, and for a binary operator @ with |
| 3590 | // a left operand of a type whose cv-unqualified version is T1 and |
| 3591 | // a right operand of a type whose cv-unqualified version is T2, |
| 3592 | // three sets of candidate functions, designated member |
| 3593 | // candidates, non-member candidates and built-in candidates, are |
| 3594 | // constructed as follows: |
| 3595 | QualType T1 = Args[0]->getType(); |
| 3596 | QualType T2; |
| 3597 | if (NumArgs > 1) |
| 3598 | T2 = Args[1]->getType(); |
| 3599 | |
| 3600 | // -- If T1 is a class type, the set of member candidates is the |
| 3601 | // result of the qualified lookup of T1::operator@ |
| 3602 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 3603 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3604 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3605 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3606 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3607 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3608 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3609 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 3610 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 3611 | Operators.suppressDiagnostics(); |
| 3612 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 3614 | OperEnd = Operators.end(); |
| 3615 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3616 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3617 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3618 | Args + 1, NumArgs - 1, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3619 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3620 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3623 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 3624 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 3625 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3626 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 3627 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3628 | /// operator. NumContextualBoolArguments is the number of arguments |
| 3629 | /// (at the beginning of the argument list) that will be contextually |
| 3630 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3631 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3632 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3633 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3634 | bool IsAssignmentOperator, |
| 3635 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3636 | // Overload resolution is always an unevaluated context. |
| 3637 | EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated); |
| 3638 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3639 | // Add this candidate |
| 3640 | CandidateSet.push_back(OverloadCandidate()); |
| 3641 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3642 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3643 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 3644 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3645 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3646 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 3647 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 3648 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 3649 | |
| 3650 | // Determine the implicit conversion sequences for each of the |
| 3651 | // arguments. |
| 3652 | Candidate.Viable = true; |
| 3653 | Candidate.Conversions.resize(NumArgs); |
| 3654 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3655 | // C++ [over.match.oper]p4: |
| 3656 | // For the built-in assignment operators, conversions of the |
| 3657 | // left operand are restricted as follows: |
| 3658 | // -- no temporaries are introduced to hold the left operand, and |
| 3659 | // -- no user-defined conversions are applied to the left |
| 3660 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3661 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3662 | // |
| 3663 | // We block these conversions by turning off user-defined |
| 3664 | // conversions, since that is the only way that initialization of |
| 3665 | // a reference to a non-class type can occur from something that |
| 3666 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3667 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3669 | "Contextual conversion to bool requires bool type"); |
| 3670 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 3671 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3673 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 3674 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3675 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3676 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3677 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3678 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3679 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3680 | break; |
| 3681 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 3686 | /// candidate operator functions for built-in operators (C++ |
| 3687 | /// [over.built]). The types are separated into pointer types and |
| 3688 | /// enumeration types. |
| 3689 | class BuiltinCandidateTypeSet { |
| 3690 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3691 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3692 | |
| 3693 | /// PointerTypes - The set of pointer types that will be used in the |
| 3694 | /// built-in candidates. |
| 3695 | TypeSet PointerTypes; |
| 3696 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3697 | /// MemberPointerTypes - The set of member pointer types that will be |
| 3698 | /// used in the built-in candidates. |
| 3699 | TypeSet MemberPointerTypes; |
| 3700 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3701 | /// EnumerationTypes - The set of enumeration types that will be |
| 3702 | /// used in the built-in candidates. |
| 3703 | TypeSet EnumerationTypes; |
| 3704 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3705 | /// Sema - The semantic analysis instance where we are building the |
| 3706 | /// candidate type set. |
| 3707 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3709 | /// Context - The AST context in which we will build the type sets. |
| 3710 | ASTContext &Context; |
| 3711 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3712 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3713 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3714 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3715 | |
| 3716 | public: |
| 3717 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3718 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3719 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3721 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3723 | void AddTypesConvertedFrom(QualType Ty, |
| 3724 | SourceLocation Loc, |
| 3725 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3726 | bool AllowExplicitConversions, |
| 3727 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3728 | |
| 3729 | /// pointer_begin - First pointer type found; |
| 3730 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 3731 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3732 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3733 | iterator pointer_end() { return PointerTypes.end(); } |
| 3734 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3735 | /// member_pointer_begin - First member pointer type found; |
| 3736 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 3737 | |
| 3738 | /// member_pointer_end - Past the last member pointer type found; |
| 3739 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 3740 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3741 | /// enumeration_begin - First enumeration type found; |
| 3742 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 3743 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3744 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3745 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 3746 | }; |
| 3747 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3748 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3749 | /// the set of pointer types along with any more-qualified variants of |
| 3750 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3751 | /// will add "int const *", "int const volatile *", "int const |
| 3752 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3753 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3754 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3755 | /// |
| 3756 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3757 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3758 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 3759 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3760 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3761 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3762 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3763 | return false; |
| 3764 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3765 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
| 3766 | assert(PointerTy && "type was not a pointer type!"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3767 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3768 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3769 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3770 | // (the qualifier would sink to the element type), and for another, the |
| 3771 | // only overload situation where it matters is subscript or pointer +- int, |
| 3772 | // and those shouldn't have qualifier variants anyway. |
| 3773 | if (PointeeTy->isArrayType()) |
| 3774 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3775 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 3776 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 3777 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3778 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 3779 | bool hasRestrict = VisibleQuals.hasRestrict(); |
| 3780 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3781 | // Iterate through all strict supersets of BaseCVR. |
| 3782 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3783 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3784 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 3785 | // in the types. |
| 3786 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 3787 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3788 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3789 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | return true; |
| 3793 | } |
| 3794 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3795 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 3796 | /// to the set of pointer types along with any more-qualified variants of |
| 3797 | /// that type. For example, if @p Ty is "int const *", this routine |
| 3798 | /// will add "int const *", "int const volatile *", "int const |
| 3799 | /// restrict *", and "int const volatile restrict *" to the set of |
| 3800 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 3801 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3802 | /// |
| 3803 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3804 | bool |
| 3805 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 3806 | QualType Ty) { |
| 3807 | // Insert this type. |
| 3808 | if (!MemberPointerTypes.insert(Ty)) |
| 3809 | return false; |
| 3810 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3811 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 3812 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3813 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3814 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 3815 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 3816 | // (the qualifier would sink to the element type), and for another, the |
| 3817 | // only overload situation where it matters is subscript or pointer +- int, |
| 3818 | // and those shouldn't have qualifier variants anyway. |
| 3819 | if (PointeeTy->isArrayType()) |
| 3820 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3821 | const Type *ClassTy = PointerTy->getClass(); |
| 3822 | |
| 3823 | // Iterate through all strict supersets of the pointee type's CVR |
| 3824 | // qualifiers. |
| 3825 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 3826 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 3827 | if ((CVR | BaseCVR) != CVR) continue; |
| 3828 | |
| 3829 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
| 3830 | MemberPointerTypes.insert(Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | return true; |
| 3834 | } |
| 3835 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3836 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 3837 | /// 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] | 3838 | /// primarily interested in pointer types and enumeration types. We also |
| 3839 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3840 | /// AllowUserConversions is true if we should look at the conversion |
| 3841 | /// functions of a class type, and AllowExplicitConversions if we |
| 3842 | /// should also include the explicit conversion functions of a class |
| 3843 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3845 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3846 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3847 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3848 | bool AllowExplicitConversions, |
| 3849 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3850 | // Only deal with canonical types. |
| 3851 | Ty = Context.getCanonicalType(Ty); |
| 3852 | |
| 3853 | // Look through reference types; they aren't part of the type of an |
| 3854 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3855 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3856 | Ty = RefTy->getPointeeType(); |
| 3857 | |
| 3858 | // We don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3859 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3860 | |
Sebastian Redl | 65ae200 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 3861 | // If we're dealing with an array type, decay to the pointer. |
| 3862 | if (Ty->isArrayType()) |
| 3863 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 3864 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3865 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3866 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 3867 | |
| 3868 | // Insert our type, and its more-qualified variants, into the set |
| 3869 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3870 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3871 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3872 | } else if (Ty->isMemberPointerType()) { |
| 3873 | // Member pointers are far easier, since the pointee can't be converted. |
| 3874 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 3875 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3876 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 3877 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3878 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3879 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3880 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3881 | // No conversion functions in incomplete types. |
| 3882 | return; |
| 3883 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3884 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3885 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3886 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | ae01f78 | 2009-10-07 17:26:09 +0000 | [diff] [blame] | 3887 | = ClassDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3888 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3889 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3890 | NamedDecl *D = I.getDecl(); |
| 3891 | if (isa<UsingShadowDecl>(D)) |
| 3892 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3893 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3895 | // about which builtin types we can convert to. |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3896 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3897 | continue; |
| 3898 | |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3899 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3900 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3901 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3902 | VisibleQuals); |
| 3903 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3904 | } |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3909 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 3910 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 3911 | /// given type to the candidate set. |
| 3912 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 3913 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3915 | unsigned NumArgs, |
| 3916 | OverloadCandidateSet &CandidateSet) { |
| 3917 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3919 | // T& operator=(T&, T) |
| 3920 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 3921 | ParamTypes[1] = T; |
| 3922 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3923 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3925 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 3926 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3927 | ParamTypes[0] |
| 3928 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3929 | ParamTypes[1] = T; |
| 3930 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3932 | } |
| 3933 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3935 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 3936 | /// 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] | 3937 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 3938 | Qualifiers VRQuals; |
| 3939 | const RecordType *TyRec; |
| 3940 | if (const MemberPointerType *RHSMPType = |
| 3941 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 3942 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3943 | else |
| 3944 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 3945 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 3946 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3947 | VRQuals.addVolatile(); |
| 3948 | VRQuals.addRestrict(); |
| 3949 | return VRQuals; |
| 3950 | } |
| 3951 | |
| 3952 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3953 | if (!ClassDecl->hasDefinition()) |
| 3954 | return VRQuals; |
| 3955 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3956 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 3957 | ClassDecl->getVisibleConversionFunctions(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3958 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3959 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3960 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3961 | NamedDecl *D = I.getDecl(); |
| 3962 | if (isa<UsingShadowDecl>(D)) |
| 3963 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3964 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 3965 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 3966 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 3967 | CanTy = ResTypeRef->getPointeeType(); |
| 3968 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 3969 | // as see them. |
| 3970 | bool done = false; |
| 3971 | while (!done) { |
| 3972 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 3973 | CanTy = ResTypePtr->getPointeeType(); |
| 3974 | else if (const MemberPointerType *ResTypeMPtr = |
| 3975 | CanTy->getAs<MemberPointerType>()) |
| 3976 | CanTy = ResTypeMPtr->getPointeeType(); |
| 3977 | else |
| 3978 | done = true; |
| 3979 | if (CanTy.isVolatileQualified()) |
| 3980 | VRQuals.addVolatile(); |
| 3981 | if (CanTy.isRestrictQualified()) |
| 3982 | VRQuals.addRestrict(); |
| 3983 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 3984 | return VRQuals; |
| 3985 | } |
| 3986 | } |
| 3987 | } |
| 3988 | return VRQuals; |
| 3989 | } |
| 3990 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3991 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 3992 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 3993 | /// on the operator @p Op and the arguments given. For example, if the |
| 3994 | /// operator is a binary '+', this routine might add "int |
| 3995 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3996 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3998 | SourceLocation OpLoc, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3999 | Expr **Args, unsigned NumArgs, |
| 4000 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4001 | // The set of "promoted arithmetic types", which are the arithmetic |
| 4002 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 4003 | // that the first few of these types are the promoted integral |
| 4004 | // types; these types need to be first. |
| 4005 | // FIXME: What about complex? |
| 4006 | const unsigned FirstIntegralType = 0; |
| 4007 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4009 | LastPromotedIntegralType = 13; |
| 4010 | const unsigned FirstPromotedArithmeticType = 7, |
| 4011 | LastPromotedArithmeticType = 16; |
| 4012 | const unsigned NumArithmeticTypes = 16; |
| 4013 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4014 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 4015 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4016 | Context.SignedCharTy, Context.ShortTy, |
| 4017 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 4018 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 4019 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 4020 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 4021 | }; |
Douglas Gregor | b8440a7 | 2009-10-21 22:01:30 +0000 | [diff] [blame] | 4022 | assert(ArithmeticTypes[FirstPromotedIntegralType] == Context.IntTy && |
| 4023 | "Invalid first promoted integral type"); |
| 4024 | assert(ArithmeticTypes[LastPromotedIntegralType - 1] |
| 4025 | == Context.UnsignedLongLongTy && |
| 4026 | "Invalid last promoted integral type"); |
| 4027 | assert(ArithmeticTypes[FirstPromotedArithmeticType] == Context.IntTy && |
| 4028 | "Invalid first promoted arithmetic type"); |
| 4029 | assert(ArithmeticTypes[LastPromotedArithmeticType - 1] |
| 4030 | == Context.LongDoubleTy && |
| 4031 | "Invalid last promoted arithmetic type"); |
| 4032 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4033 | // Find all of the types that the arguments can convert to, but only |
| 4034 | // if the operator we're looking at has built-in operator candidates |
| 4035 | // that make use of these types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4036 | Qualifiers VisibleTypeConversionsQuals; |
| 4037 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4038 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4039 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
| 4040 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4041 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4042 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 4043 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4044 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4045 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4046 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4047 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4048 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4049 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4050 | OpLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4051 | true, |
| 4052 | (Op == OO_Exclaim || |
| 4053 | Op == OO_AmpAmp || |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4054 | Op == OO_PipePipe), |
| 4055 | VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | bool isComparison = false; |
| 4059 | switch (Op) { |
| 4060 | case OO_None: |
| 4061 | case NUM_OVERLOADED_OPERATORS: |
| 4062 | assert(false && "Expected an overloaded operator"); |
| 4063 | break; |
| 4064 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4065 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4067 | goto UnaryStar; |
| 4068 | else |
| 4069 | goto BinaryStar; |
| 4070 | break; |
| 4071 | |
| 4072 | case OO_Plus: // '+' is either unary or binary |
| 4073 | if (NumArgs == 1) |
| 4074 | goto UnaryPlus; |
| 4075 | else |
| 4076 | goto BinaryPlus; |
| 4077 | break; |
| 4078 | |
| 4079 | case OO_Minus: // '-' is either unary or binary |
| 4080 | if (NumArgs == 1) |
| 4081 | goto UnaryMinus; |
| 4082 | else |
| 4083 | goto BinaryMinus; |
| 4084 | break; |
| 4085 | |
| 4086 | case OO_Amp: // '&' is either unary or binary |
| 4087 | if (NumArgs == 1) |
| 4088 | goto UnaryAmp; |
| 4089 | else |
| 4090 | goto BinaryAmp; |
| 4091 | |
| 4092 | case OO_PlusPlus: |
| 4093 | case OO_MinusMinus: |
| 4094 | // C++ [over.built]p3: |
| 4095 | // |
| 4096 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 4097 | // is either volatile or empty, there exist candidate operator |
| 4098 | // functions of the form |
| 4099 | // |
| 4100 | // VQ T& operator++(VQ T&); |
| 4101 | // T operator++(VQ T&, int); |
| 4102 | // |
| 4103 | // C++ [over.built]p4: |
| 4104 | // |
| 4105 | // For every pair (T, VQ), where T is an arithmetic type other |
| 4106 | // than bool, and VQ is either volatile or empty, there exist |
| 4107 | // candidate operator functions of the form |
| 4108 | // |
| 4109 | // VQ T& operator--(VQ T&); |
| 4110 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4111 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4112 | Arith < NumArithmeticTypes; ++Arith) { |
| 4113 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4115 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4116 | |
| 4117 | // Non-volatile version. |
| 4118 | if (NumArgs == 1) |
| 4119 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4120 | else |
| 4121 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4122 | // heuristic to reduce number of builtin candidates in the set. |
| 4123 | // Add volatile version only if there are conversions to a volatile type. |
| 4124 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4125 | // Volatile version |
| 4126 | ParamTypes[0] |
| 4127 | = Context.getLValueReferenceType(Context.getVolatileType(ArithTy)); |
| 4128 | if (NumArgs == 1) |
| 4129 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4130 | else |
| 4131 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 4132 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | // C++ [over.built]p5: |
| 4136 | // |
| 4137 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4138 | // cv-unqualified object type, and VQ is either volatile or |
| 4139 | // empty, there exist candidate operator functions of the form |
| 4140 | // |
| 4141 | // T*VQ& operator++(T*VQ&); |
| 4142 | // T*VQ& operator--(T*VQ&); |
| 4143 | // T* operator++(T*VQ&, int); |
| 4144 | // T* operator--(T*VQ&, int); |
| 4145 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4146 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4147 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4148 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4149 | continue; |
| 4150 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | QualType ParamTypes[2] = { |
| 4152 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4153 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4155 | // Without volatile |
| 4156 | if (NumArgs == 1) |
| 4157 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4158 | else |
| 4159 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4160 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4161 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4162 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4163 | // With volatile |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4164 | ParamTypes[0] |
| 4165 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4166 | if (NumArgs == 1) |
| 4167 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 4168 | else |
| 4169 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4170 | } |
| 4171 | } |
| 4172 | break; |
| 4173 | |
| 4174 | UnaryStar: |
| 4175 | // C++ [over.built]p6: |
| 4176 | // For every cv-qualified or cv-unqualified object type T, there |
| 4177 | // exist candidate operator functions of the form |
| 4178 | // |
| 4179 | // T& operator*(T*); |
| 4180 | // |
| 4181 | // C++ [over.built]p7: |
| 4182 | // For every function type T, there exist candidate operator |
| 4183 | // functions of the form |
| 4184 | // T& operator*(T*); |
| 4185 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4186 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4187 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4188 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4190 | &ParamTy, Args, 1, CandidateSet); |
| 4191 | } |
| 4192 | break; |
| 4193 | |
| 4194 | UnaryPlus: |
| 4195 | // C++ [over.built]p8: |
| 4196 | // For every type T, there exist candidate operator functions of |
| 4197 | // the form |
| 4198 | // |
| 4199 | // T* operator+(T*); |
| 4200 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4201 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4202 | QualType ParamTy = *Ptr; |
| 4203 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 4204 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4206 | // Fall through |
| 4207 | |
| 4208 | UnaryMinus: |
| 4209 | // C++ [over.built]p9: |
| 4210 | // For every promoted arithmetic type T, there exist candidate |
| 4211 | // operator functions of the form |
| 4212 | // |
| 4213 | // T operator+(T); |
| 4214 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4216 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 4217 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 4218 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 4219 | } |
| 4220 | break; |
| 4221 | |
| 4222 | case OO_Tilde: |
| 4223 | // C++ [over.built]p10: |
| 4224 | // For every promoted integral type T, there exist candidate |
| 4225 | // operator functions of the form |
| 4226 | // |
| 4227 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4228 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4229 | Int < LastPromotedIntegralType; ++Int) { |
| 4230 | QualType IntTy = ArithmeticTypes[Int]; |
| 4231 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 4232 | } |
| 4233 | break; |
| 4234 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4235 | case OO_New: |
| 4236 | case OO_Delete: |
| 4237 | case OO_Array_New: |
| 4238 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4239 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4240 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4241 | break; |
| 4242 | |
| 4243 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4244 | UnaryAmp: |
| 4245 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4246 | // C++ [over.match.oper]p3: |
| 4247 | // -- For the operator ',', the unary operator '&', or the |
| 4248 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4249 | break; |
| 4250 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4251 | case OO_EqualEqual: |
| 4252 | case OO_ExclaimEqual: |
| 4253 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4254 | // For every pointer to member type T, there exist candidate operator |
| 4255 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4256 | // |
| 4257 | // bool operator==(T,T); |
| 4258 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4260 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4261 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4262 | MemPtr != MemPtrEnd; |
| 4263 | ++MemPtr) { |
| 4264 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 4265 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4266 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4268 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4270 | case OO_Less: |
| 4271 | case OO_Greater: |
| 4272 | case OO_LessEqual: |
| 4273 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4274 | // C++ [over.built]p15: |
| 4275 | // |
| 4276 | // For every pointer or enumeration type T, there exist |
| 4277 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4278 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4279 | // bool operator<(T, T); |
| 4280 | // bool operator>(T, T); |
| 4281 | // bool operator<=(T, T); |
| 4282 | // bool operator>=(T, T); |
| 4283 | // bool operator==(T, T); |
| 4284 | // bool operator!=(T, T); |
| 4285 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4286 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4287 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4288 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4289 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4291 | = CandidateTypes.enumeration_begin(); |
| 4292 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 4293 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 4294 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 4295 | } |
| 4296 | |
| 4297 | // Fall through. |
| 4298 | isComparison = true; |
| 4299 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4300 | BinaryPlus: |
| 4301 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4302 | if (!isComparison) { |
| 4303 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 4304 | |
| 4305 | // C++ [over.built]p13: |
| 4306 | // |
| 4307 | // For every cv-qualified or cv-unqualified object type T |
| 4308 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4310 | // T* operator+(T*, ptrdiff_t); |
| 4311 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 4312 | // T* operator-(T*, ptrdiff_t); |
| 4313 | // T* operator+(ptrdiff_t, T*); |
| 4314 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 4315 | // |
| 4316 | // C++ [over.built]p14: |
| 4317 | // |
| 4318 | // For every T, where T is a pointer to object type, there |
| 4319 | // exist candidate operator functions of the form |
| 4320 | // |
| 4321 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4323 | = CandidateTypes.pointer_begin(); |
| 4324 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4325 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 4326 | |
| 4327 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 4328 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4329 | |
| 4330 | if (Op == OO_Plus) { |
| 4331 | // T* operator+(ptrdiff_t, T*); |
| 4332 | ParamTypes[0] = ParamTypes[1]; |
| 4333 | ParamTypes[1] = *Ptr; |
| 4334 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4335 | } else { |
| 4336 | // ptrdiff_t operator-(T, T); |
| 4337 | ParamTypes[1] = *Ptr; |
| 4338 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 4339 | Args, 2, CandidateSet); |
| 4340 | } |
| 4341 | } |
| 4342 | } |
| 4343 | // Fall through |
| 4344 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4345 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4346 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4347 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4348 | // C++ [over.built]p12: |
| 4349 | // |
| 4350 | // For every pair of promoted arithmetic types L and R, there |
| 4351 | // exist candidate operator functions of the form |
| 4352 | // |
| 4353 | // LR operator*(L, R); |
| 4354 | // LR operator/(L, R); |
| 4355 | // LR operator+(L, R); |
| 4356 | // LR operator-(L, R); |
| 4357 | // bool operator<(L, R); |
| 4358 | // bool operator>(L, R); |
| 4359 | // bool operator<=(L, R); |
| 4360 | // bool operator>=(L, R); |
| 4361 | // bool operator==(L, R); |
| 4362 | // bool operator!=(L, R); |
| 4363 | // |
| 4364 | // where LR is the result of the usual arithmetic conversions |
| 4365 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4366 | // |
| 4367 | // C++ [over.built]p24: |
| 4368 | // |
| 4369 | // For every pair of promoted arithmetic types L and R, there exist |
| 4370 | // candidate operator functions of the form |
| 4371 | // |
| 4372 | // LR operator?(bool, L, R); |
| 4373 | // |
| 4374 | // where LR is the result of the usual arithmetic conversions |
| 4375 | // between types L and R. |
| 4376 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4378 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4380 | Right < LastPromotedArithmeticType; ++Right) { |
| 4381 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4382 | QualType Result |
| 4383 | = isComparison |
| 4384 | ? Context.BoolTy |
| 4385 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4386 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4387 | } |
| 4388 | } |
| 4389 | break; |
| 4390 | |
| 4391 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4392 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4393 | case OO_Caret: |
| 4394 | case OO_Pipe: |
| 4395 | case OO_LessLess: |
| 4396 | case OO_GreaterGreater: |
| 4397 | // C++ [over.built]p17: |
| 4398 | // |
| 4399 | // For every pair of promoted integral types L and R, there |
| 4400 | // exist candidate operator functions of the form |
| 4401 | // |
| 4402 | // LR operator%(L, R); |
| 4403 | // LR operator&(L, R); |
| 4404 | // LR operator^(L, R); |
| 4405 | // LR operator|(L, R); |
| 4406 | // L operator<<(L, R); |
| 4407 | // L operator>>(L, R); |
| 4408 | // |
| 4409 | // where LR is the result of the usual arithmetic conversions |
| 4410 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4412 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4413 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4414 | Right < LastPromotedIntegralType; ++Right) { |
| 4415 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 4416 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 4417 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4418 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4419 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 4420 | } |
| 4421 | } |
| 4422 | break; |
| 4423 | |
| 4424 | case OO_Equal: |
| 4425 | // C++ [over.built]p20: |
| 4426 | // |
| 4427 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4428 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4429 | // empty, there exist candidate operator functions of the form |
| 4430 | // |
| 4431 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4432 | for (BuiltinCandidateTypeSet::iterator |
| 4433 | Enum = CandidateTypes.enumeration_begin(), |
| 4434 | EnumEnd = CandidateTypes.enumeration_end(); |
| 4435 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4437 | CandidateSet); |
| 4438 | for (BuiltinCandidateTypeSet::iterator |
| 4439 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4440 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4441 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4442 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4443 | CandidateSet); |
| 4444 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4445 | |
| 4446 | case OO_PlusEqual: |
| 4447 | case OO_MinusEqual: |
| 4448 | // C++ [over.built]p19: |
| 4449 | // |
| 4450 | // For every pair (T, VQ), where T is any type and VQ is either |
| 4451 | // volatile or empty, there exist candidate operator functions |
| 4452 | // of the form |
| 4453 | // |
| 4454 | // T*VQ& operator=(T*VQ&, T*); |
| 4455 | // |
| 4456 | // C++ [over.built]p21: |
| 4457 | // |
| 4458 | // For every pair (T, VQ), where T is a cv-qualified or |
| 4459 | // cv-unqualified object type and VQ is either volatile or |
| 4460 | // empty, there exist candidate operator functions of the form |
| 4461 | // |
| 4462 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 4463 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 4464 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4465 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4466 | QualType ParamTypes[2]; |
| 4467 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 4468 | |
| 4469 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4470 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4471 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4472 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4473 | |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4474 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 4475 | VisibleTypeConversionsQuals.hasVolatile()) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4476 | // volatile version |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4477 | ParamTypes[0] |
| 4478 | = Context.getLValueReferenceType(Context.getVolatileType(*Ptr)); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4479 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4480 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4481 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4482 | } |
| 4483 | // Fall through. |
| 4484 | |
| 4485 | case OO_StarEqual: |
| 4486 | case OO_SlashEqual: |
| 4487 | // C++ [over.built]p18: |
| 4488 | // |
| 4489 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 4490 | // VQ is either volatile or empty, and R is a promoted |
| 4491 | // arithmetic type, there exist candidate operator functions of |
| 4492 | // the form |
| 4493 | // |
| 4494 | // VQ L& operator=(VQ L&, R); |
| 4495 | // VQ L& operator*=(VQ L&, R); |
| 4496 | // VQ L& operator/=(VQ L&, R); |
| 4497 | // VQ L& operator+=(VQ L&, R); |
| 4498 | // VQ L& operator-=(VQ L&, R); |
| 4499 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4500 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4501 | Right < LastPromotedArithmeticType; ++Right) { |
| 4502 | QualType ParamTypes[2]; |
| 4503 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4504 | |
| 4505 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4506 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4507 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4508 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4509 | |
| 4510 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 4511 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4512 | ParamTypes[0] = Context.getVolatileType(ArithmeticTypes[Left]); |
| 4513 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4514 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4515 | /*IsAssigmentOperator=*/Op == OO_Equal); |
| 4516 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | break; |
| 4520 | |
| 4521 | case OO_PercentEqual: |
| 4522 | case OO_LessLessEqual: |
| 4523 | case OO_GreaterGreaterEqual: |
| 4524 | case OO_AmpEqual: |
| 4525 | case OO_CaretEqual: |
| 4526 | case OO_PipeEqual: |
| 4527 | // C++ [over.built]p22: |
| 4528 | // |
| 4529 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 4530 | // is either volatile or empty, and R is a promoted integral |
| 4531 | // type, there exist candidate operator functions of the form |
| 4532 | // |
| 4533 | // VQ L& operator%=(VQ L&, R); |
| 4534 | // VQ L& operator<<=(VQ L&, R); |
| 4535 | // VQ L& operator>>=(VQ L&, R); |
| 4536 | // VQ L& operator&=(VQ L&, R); |
| 4537 | // VQ L& operator^=(VQ L&, R); |
| 4538 | // VQ L& operator|=(VQ L&, R); |
| 4539 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4541 | Right < LastPromotedIntegralType; ++Right) { |
| 4542 | QualType ParamTypes[2]; |
| 4543 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 4544 | |
| 4545 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4546 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4547 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
Fariborz Jahanian | a4a9334 | 2009-10-20 00:04:40 +0000 | [diff] [blame] | 4548 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 4549 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 4550 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 4551 | ParamTypes[0] = Context.getVolatileType(ParamTypes[0]); |
| 4552 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
| 4553 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 4554 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | break; |
| 4558 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4559 | case OO_Exclaim: { |
| 4560 | // C++ [over.operator]p23: |
| 4561 | // |
| 4562 | // There also exist candidate operator functions of the form |
| 4563 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4565 | // bool operator&&(bool, bool); [BELOW] |
| 4566 | // bool operator||(bool, bool); [BELOW] |
| 4567 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4568 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 4569 | /*IsAssignmentOperator=*/false, |
| 4570 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4571 | break; |
| 4572 | } |
| 4573 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4574 | case OO_AmpAmp: |
| 4575 | case OO_PipePipe: { |
| 4576 | // C++ [over.operator]p23: |
| 4577 | // |
| 4578 | // There also exist candidate operator functions of the form |
| 4579 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4580 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4581 | // bool operator&&(bool, bool); |
| 4582 | // bool operator||(bool, bool); |
| 4583 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4584 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 4585 | /*IsAssignmentOperator=*/false, |
| 4586 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4587 | break; |
| 4588 | } |
| 4589 | |
| 4590 | case OO_Subscript: |
| 4591 | // C++ [over.built]p13: |
| 4592 | // |
| 4593 | // For every cv-qualified or cv-unqualified object type T there |
| 4594 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4596 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 4597 | // T& operator[](T*, ptrdiff_t); |
| 4598 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 4599 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 4600 | // T& operator[](ptrdiff_t, T*); |
| 4601 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 4602 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4603 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4604 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4605 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4606 | |
| 4607 | // T& operator[](T*, ptrdiff_t) |
| 4608 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4609 | |
| 4610 | // T& operator[](ptrdiff_t, T*); |
| 4611 | ParamTypes[0] = ParamTypes[1]; |
| 4612 | ParamTypes[1] = *Ptr; |
| 4613 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4614 | } |
| 4615 | break; |
| 4616 | |
| 4617 | case OO_ArrowStar: |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4618 | // C++ [over.built]p11: |
| 4619 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 4620 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 4621 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 4622 | // there exist candidate operator functions of the form |
| 4623 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 4624 | // where CV12 is the union of CV1 and CV2. |
| 4625 | { |
| 4626 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4627 | CandidateTypes.pointer_begin(); |
| 4628 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 4629 | QualType C1Ty = (*Ptr); |
| 4630 | QualType C1; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4631 | QualifierCollector Q1; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4632 | if (const PointerType *PointerTy = C1Ty->getAs<PointerType>()) { |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4633 | C1 = QualType(Q1.strip(PointerTy->getPointeeType()), 0); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4634 | if (!isa<RecordType>(C1)) |
| 4635 | continue; |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4636 | // heuristic to reduce number of builtin candidates in the set. |
| 4637 | // Add volatile/restrict version only if there are conversions to a |
| 4638 | // volatile/restrict type. |
| 4639 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 4640 | continue; |
| 4641 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 4642 | continue; |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4643 | } |
| 4644 | for (BuiltinCandidateTypeSet::iterator |
| 4645 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 4646 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 4647 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 4648 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 4649 | QualType C2 = QualType(mptr->getClass(), 0); |
Fariborz Jahanian | 12df37c | 2009-10-07 16:56:50 +0000 | [diff] [blame] | 4650 | C2 = C2.getUnqualifiedType(); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4651 | if (C1 != C2 && !IsDerivedFrom(C1, C2)) |
| 4652 | break; |
| 4653 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 4654 | // build CV12 T& |
| 4655 | QualType T = mptr->getPointeeType(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4656 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 4657 | T.isVolatileQualified()) |
| 4658 | continue; |
| 4659 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 4660 | T.isRestrictQualified()) |
| 4661 | continue; |
Fariborz Jahanian | 4dc1246 | 2009-10-09 16:34:40 +0000 | [diff] [blame] | 4662 | T = Q1.apply(T); |
Fariborz Jahanian | 34d93dc | 2009-10-06 23:08:05 +0000 | [diff] [blame] | 4663 | QualType ResultTy = Context.getLValueReferenceType(T); |
| 4664 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 4665 | } |
| 4666 | } |
| 4667 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4668 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4669 | |
| 4670 | case OO_Conditional: |
| 4671 | // Note that we don't consider the first argument, since it has been |
| 4672 | // contextually converted to bool long ago. The candidates below are |
| 4673 | // therefore added as binary. |
| 4674 | // |
| 4675 | // C++ [over.built]p24: |
| 4676 | // For every type T, where T is a pointer or pointer-to-member type, |
| 4677 | // there exist candidate operator functions of the form |
| 4678 | // |
| 4679 | // T operator?(bool, T, T); |
| 4680 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4681 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 4682 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 4683 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4684 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4685 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4686 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 4687 | CandidateTypes.member_pointer_begin(), |
| 4688 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 4689 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 4690 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 4691 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4692 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4693 | } |
| 4694 | } |
| 4695 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4696 | /// \brief Add function candidates found via argument-dependent lookup |
| 4697 | /// to the set of overloading candidates. |
| 4698 | /// |
| 4699 | /// This routine performs argument-dependent name lookup based on the |
| 4700 | /// given function name (which may also be an operator name) and adds |
| 4701 | /// all of the overload candidates found by ADL to the overload |
| 4702 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4704 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4705 | bool Operator, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4706 | Expr **Args, unsigned NumArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4707 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4708 | OverloadCandidateSet& CandidateSet, |
| 4709 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4710 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4711 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 4712 | // FIXME: This approach for uniquing ADL results (and removing |
| 4713 | // redundant candidates from the set) relies on pointer-equality, |
| 4714 | // which means we need to key off the canonical decl. However, |
| 4715 | // always going back to the canonical decl might not get us the |
| 4716 | // right set of default arguments. What default arguments are |
| 4717 | // we supposed to consider on ADL candidates, anyway? |
| 4718 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4719 | // FIXME: Pass in the explicit template arguments? |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4720 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4721 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4722 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4723 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 4724 | CandEnd = CandidateSet.end(); |
| 4725 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4726 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4727 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4728 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4729 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4730 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4731 | |
| 4732 | // For each of the ADL candidates we found, add it to the overload |
| 4733 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 4734 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4735 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4736 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4737 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4738 | continue; |
| 4739 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4740 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 4741 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 4742 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 4743 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4744 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4745 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 4746 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4749 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 4750 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4751 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4752 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4753 | const OverloadCandidate& Cand2, |
| 4754 | SourceLocation Loc) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4755 | // Define viable functions to be better candidates than non-viable |
| 4756 | // functions. |
| 4757 | if (!Cand2.Viable) |
| 4758 | return Cand1.Viable; |
| 4759 | else if (!Cand1.Viable) |
| 4760 | return false; |
| 4761 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4762 | // C++ [over.match.best]p1: |
| 4763 | // |
| 4764 | // -- if F is a static member function, ICS1(F) is defined such |
| 4765 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 4766 | // any function G, and, symmetrically, ICS1(G) is neither |
| 4767 | // better nor worse than ICS1(F). |
| 4768 | unsigned StartArg = 0; |
| 4769 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 4770 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4771 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4772 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | // A viable function F1 is defined to be a better function than another |
| 4774 | // 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] | 4775 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4776 | unsigned NumArgs = Cand1.Conversions.size(); |
| 4777 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 4778 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4779 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4780 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 4781 | Cand2.Conversions[ArgIdx])) { |
| 4782 | case ImplicitConversionSequence::Better: |
| 4783 | // Cand1 has a better conversion sequence. |
| 4784 | HasBetterConversion = true; |
| 4785 | break; |
| 4786 | |
| 4787 | case ImplicitConversionSequence::Worse: |
| 4788 | // Cand1 can't be better than Cand2. |
| 4789 | return false; |
| 4790 | |
| 4791 | case ImplicitConversionSequence::Indistinguishable: |
| 4792 | // Do nothing. |
| 4793 | break; |
| 4794 | } |
| 4795 | } |
| 4796 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4798 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4799 | if (HasBetterConversion) |
| 4800 | return true; |
| 4801 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4802 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 4803 | // specialization, or, if not that, |
| 4804 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 4805 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 4806 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
| 4808 | // -- F1 and F2 are function template specializations, and the function |
| 4809 | // template for F1 is more specialized than the template for F2 |
| 4810 | // 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] | 4811 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 4812 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 4813 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4814 | if (FunctionTemplateDecl *BetterTemplate |
| 4815 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 4816 | Cand2.Function->getPrimaryTemplate(), |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4817 | Loc, |
Douglas Gregor | 6010da0 | 2009-09-14 23:02:14 +0000 | [diff] [blame] | 4818 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
| 4819 | : TPOC_Call)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4820 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4821 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4822 | // -- the context is an initialization by user-defined conversion |
| 4823 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 4824 | // from the return type of F1 to the destination type (i.e., |
| 4825 | // the type of the entity being initialized) is a better |
| 4826 | // conversion sequence than the standard conversion sequence |
| 4827 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4828 | if (Cand1.Function && Cand2.Function && |
| 4829 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4830 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 4831 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 4832 | Cand2.FinalConversion)) { |
| 4833 | case ImplicitConversionSequence::Better: |
| 4834 | // Cand1 has a better conversion sequence. |
| 4835 | return true; |
| 4836 | |
| 4837 | case ImplicitConversionSequence::Worse: |
| 4838 | // Cand1 can't be better than Cand2. |
| 4839 | return false; |
| 4840 | |
| 4841 | case ImplicitConversionSequence::Indistinguishable: |
| 4842 | // Do nothing |
| 4843 | break; |
| 4844 | } |
| 4845 | } |
| 4846 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4847 | return false; |
| 4848 | } |
| 4849 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4850 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4851 | /// within an overload candidate set. |
| 4852 | /// |
| 4853 | /// \param CandidateSet the set of candidate functions. |
| 4854 | /// |
| 4855 | /// \param Loc the location of the function name (or operator symbol) for |
| 4856 | /// which overload resolution occurs. |
| 4857 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4859 | /// function, Best points to the candidate function found. |
| 4860 | /// |
| 4861 | /// \returns The result of overload resolution. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4862 | OverloadingResult Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
| 4863 | SourceLocation Loc, |
| 4864 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4865 | // Find the best viable function. |
| 4866 | Best = CandidateSet.end(); |
| 4867 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4868 | Cand != CandidateSet.end(); ++Cand) { |
| 4869 | if (Cand->Viable) { |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4870 | if (Best == CandidateSet.end() || |
| 4871 | isBetterOverloadCandidate(*Cand, *Best, Loc)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4872 | Best = Cand; |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | // If we didn't find any viable functions, abort. |
| 4877 | if (Best == CandidateSet.end()) |
| 4878 | return OR_No_Viable_Function; |
| 4879 | |
| 4880 | // Make sure that this function is better than every other viable |
| 4881 | // function. If not, we have an ambiguity. |
| 4882 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4883 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4885 | Cand != Best && |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4886 | !isBetterOverloadCandidate(*Best, *Cand, Loc)) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4887 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4888 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4889 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4890 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4892 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4893 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4894 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4895 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4896 | return OR_Deleted; |
| 4897 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4898 | // C++ [basic.def.odr]p2: |
| 4899 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4900 | // when referred to from a potentially-evaluated expression. [Note: this |
| 4901 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4902 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 4903 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 4904 | if (Best->Function) |
| 4905 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4906 | return OR_Success; |
| 4907 | } |
| 4908 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4909 | namespace { |
| 4910 | |
| 4911 | enum OverloadCandidateKind { |
| 4912 | oc_function, |
| 4913 | oc_method, |
| 4914 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4915 | oc_function_template, |
| 4916 | oc_method_template, |
| 4917 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4918 | oc_implicit_default_constructor, |
| 4919 | oc_implicit_copy_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4920 | oc_implicit_copy_assignment |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4921 | }; |
| 4922 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4923 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 4924 | FunctionDecl *Fn, |
| 4925 | std::string &Description) { |
| 4926 | bool isTemplate = false; |
| 4927 | |
| 4928 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 4929 | isTemplate = true; |
| 4930 | Description = S.getTemplateArgumentBindingsText( |
| 4931 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 4932 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4933 | |
| 4934 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4935 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4936 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4937 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4938 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 4939 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4943 | // This actually gets spelled 'candidate function' for now, but |
| 4944 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4945 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4946 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4947 | |
| 4948 | assert(Meth->isCopyAssignment() |
| 4949 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4950 | return oc_implicit_copy_assignment; |
| 4951 | } |
| 4952 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4953 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | } // end anonymous namespace |
| 4957 | |
| 4958 | // Notes the location of an overload candidate. |
| 4959 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4960 | std::string FnDesc; |
| 4961 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 4962 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 4963 | << (unsigned) K << FnDesc; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4966 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 4967 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 4968 | /// target types of the conversion. |
| 4969 | void Sema::DiagnoseAmbiguousConversion(const ImplicitConversionSequence &ICS, |
| 4970 | SourceLocation CaretLoc, |
| 4971 | const PartialDiagnostic &PDiag) { |
| 4972 | Diag(CaretLoc, PDiag) |
| 4973 | << ICS.Ambiguous.getFromType() << ICS.Ambiguous.getToType(); |
| 4974 | for (AmbiguousConversionSequence::const_iterator |
| 4975 | I = ICS.Ambiguous.begin(), E = ICS.Ambiguous.end(); I != E; ++I) { |
| 4976 | NoteOverloadCandidate(*I); |
| 4977 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4980 | namespace { |
| 4981 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4982 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 4983 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 4984 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4985 | assert(Cand->Function && "for now, candidate must be a function"); |
| 4986 | FunctionDecl *Fn = Cand->Function; |
| 4987 | |
| 4988 | // There's a conversion slot for the object argument if this is a |
| 4989 | // non-constructor method. Note that 'I' corresponds the |
| 4990 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4991 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4992 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4993 | if (I == 0) |
| 4994 | isObjectArgument = true; |
| 4995 | else |
| 4996 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 4999 | std::string FnDesc; |
| 5000 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 5001 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5002 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 5003 | QualType FromTy = Conv.Bad.getFromType(); |
| 5004 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5005 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5006 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5007 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5008 | Expr *E = FromExpr->IgnoreParens(); |
| 5009 | if (isa<UnaryOperator>(E)) |
| 5010 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5011 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 5012 | |
| 5013 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 5014 | << (unsigned) FnKind << FnDesc |
| 5015 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5016 | << ToTy << Name << I+1; |
| 5017 | return; |
| 5018 | } |
| 5019 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5020 | // Do some hand-waving analysis to see if the non-viability is due |
| 5021 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5022 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 5023 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 5024 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 5025 | CToTy = RT->getPointeeType(); |
| 5026 | else { |
| 5027 | // TODO: detect and diagnose the full richness of const mismatches. |
| 5028 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 5029 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 5030 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 5031 | } |
| 5032 | |
| 5033 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 5034 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 5035 | // It is dumb that we have to do this here. |
| 5036 | while (isa<ArrayType>(CFromTy)) |
| 5037 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5038 | while (isa<ArrayType>(CToTy)) |
| 5039 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 5040 | |
| 5041 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 5042 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 5043 | |
| 5044 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 5045 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 5046 | << (unsigned) FnKind << FnDesc |
| 5047 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5048 | << FromTy |
| 5049 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 5050 | << (unsigned) isObjectArgument << I+1; |
| 5051 | return; |
| 5052 | } |
| 5053 | |
| 5054 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 5055 | assert(CVR && "unexpected qualifiers mismatch"); |
| 5056 | |
| 5057 | if (isObjectArgument) { |
| 5058 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 5059 | << (unsigned) FnKind << FnDesc |
| 5060 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5061 | << FromTy << (CVR - 1); |
| 5062 | } else { |
| 5063 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 5064 | << (unsigned) FnKind << FnDesc |
| 5065 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5066 | << FromTy << (CVR - 1) << I+1; |
| 5067 | } |
| 5068 | return; |
| 5069 | } |
| 5070 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 5071 | // Diagnose references or pointers to incomplete types differently, |
| 5072 | // since it's far from impossible that the incompleteness triggered |
| 5073 | // the failure. |
| 5074 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 5075 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 5076 | TempFromTy = PTy->getPointeeType(); |
| 5077 | if (TempFromTy->isIncompleteType()) { |
| 5078 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 5079 | << (unsigned) FnKind << FnDesc |
| 5080 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 5081 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
| 5082 | return; |
| 5083 | } |
| 5084 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 5085 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5086 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 5087 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5088 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 5089 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 5093 | unsigned NumFormalArgs) { |
| 5094 | // TODO: treat calls to a missing default constructor as a special case |
| 5095 | |
| 5096 | FunctionDecl *Fn = Cand->Function; |
| 5097 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 5098 | |
| 5099 | unsigned MinParams = Fn->getMinRequiredArguments(); |
| 5100 | |
| 5101 | // at least / at most / exactly |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5102 | // FIXME: variadic templates "at most" should account for parameter packs |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5103 | unsigned mode, modeCount; |
| 5104 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5105 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 5106 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5107 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5108 | if (MinParams != FnTy->getNumArgs() || FnTy->isVariadic()) |
| 5109 | mode = 0; // "at least" |
| 5110 | else |
| 5111 | mode = 2; // "exactly" |
| 5112 | modeCount = MinParams; |
| 5113 | } else { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5114 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 5115 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 5116 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5117 | if (MinParams != FnTy->getNumArgs()) |
| 5118 | mode = 1; // "at most" |
| 5119 | else |
| 5120 | mode = 2; // "exactly" |
| 5121 | modeCount = FnTy->getNumArgs(); |
| 5122 | } |
| 5123 | |
| 5124 | std::string Description; |
| 5125 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 5126 | |
| 5127 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5128 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
| 5129 | << modeCount << NumFormalArgs; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5132 | /// Diagnose a failed template-argument deduction. |
| 5133 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 5134 | Expr **Args, unsigned NumArgs) { |
| 5135 | FunctionDecl *Fn = Cand->Function; // pattern |
| 5136 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5137 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5138 | NamedDecl *ParamD; |
| 5139 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 5140 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 5141 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5142 | switch (Cand->DeductionFailure.Result) { |
| 5143 | case Sema::TDK_Success: |
| 5144 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 5145 | |
| 5146 | case Sema::TDK_Incomplete: { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5147 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 5148 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 5149 | << ParamD->getDeclName(); |
| 5150 | return; |
| 5151 | } |
| 5152 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5153 | case Sema::TDK_Inconsistent: |
| 5154 | case Sema::TDK_InconsistentQuals: { |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5155 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5156 | int which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5157 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5158 | which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5159 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5160 | which = 1; |
| 5161 | else { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5162 | which = 2; |
| 5163 | } |
| 5164 | |
| 5165 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
| 5166 | << which << ParamD->getDeclName() |
| 5167 | << *Cand->DeductionFailure.getFirstArg() |
| 5168 | << *Cand->DeductionFailure.getSecondArg(); |
| 5169 | return; |
| 5170 | } |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 5172 | case Sema::TDK_InvalidExplicitArguments: |
| 5173 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
| 5174 | if (ParamD->getDeclName()) |
| 5175 | S.Diag(Fn->getLocation(), |
| 5176 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 5177 | << ParamD->getDeclName(); |
| 5178 | else { |
| 5179 | int index = 0; |
| 5180 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 5181 | index = TTP->getIndex(); |
| 5182 | else if (NonTypeTemplateParmDecl *NTTP |
| 5183 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 5184 | index = NTTP->getIndex(); |
| 5185 | else |
| 5186 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
| 5187 | S.Diag(Fn->getLocation(), |
| 5188 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 5189 | << (index + 1); |
| 5190 | } |
| 5191 | return; |
| 5192 | |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 5193 | case Sema::TDK_TooManyArguments: |
| 5194 | case Sema::TDK_TooFewArguments: |
| 5195 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 5196 | return; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 5197 | |
| 5198 | case Sema::TDK_InstantiationDepth: |
| 5199 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
| 5200 | return; |
| 5201 | |
| 5202 | case Sema::TDK_SubstitutionFailure: { |
| 5203 | std::string ArgString; |
| 5204 | if (TemplateArgumentList *Args |
| 5205 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 5206 | ArgString = S.getTemplateArgumentBindingsText( |
| 5207 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 5208 | *Args); |
| 5209 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 5210 | << ArgString; |
| 5211 | return; |
| 5212 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 5213 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5214 | // TODO: diagnose these individually, then kill off |
| 5215 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5216 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5217 | case Sema::TDK_FailedOverloadResolution: |
| 5218 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
| 5219 | return; |
| 5220 | } |
| 5221 | } |
| 5222 | |
| 5223 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 5224 | /// already generated a primary error at the call site. |
| 5225 | /// |
| 5226 | /// It really does need to be a single diagnostic with its caret |
| 5227 | /// pointed at the candidate declaration. Yes, this creates some |
| 5228 | /// major challenges of technical writing. Yes, this makes pointing |
| 5229 | /// out problems with specific arguments quite awkward. It's still |
| 5230 | /// better than generating twenty screens of text for every failed |
| 5231 | /// overload. |
| 5232 | /// |
| 5233 | /// It would be great to be able to express per-candidate problems |
| 5234 | /// more richly for those diagnostic clients that cared, but we'd |
| 5235 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5236 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 5237 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5238 | FunctionDecl *Fn = Cand->Function; |
| 5239 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5240 | // Note deleted candidates, but only if they're viable. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5241 | if (Cand->Viable && (Fn->isDeleted() || Fn->hasAttr<UnavailableAttr>())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5242 | std::string FnDesc; |
| 5243 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 5244 | |
| 5245 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5246 | << FnKind << FnDesc << Fn->isDeleted(); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5247 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5250 | // We don't really have anything else to say about viable candidates. |
| 5251 | if (Cand->Viable) { |
| 5252 | S.NoteOverloadCandidate(Fn); |
| 5253 | return; |
| 5254 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5255 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5256 | switch (Cand->FailureKind) { |
| 5257 | case ovl_fail_too_many_arguments: |
| 5258 | case ovl_fail_too_few_arguments: |
| 5259 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5260 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5261 | case ovl_fail_bad_deduction: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 5262 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 5263 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5264 | case ovl_fail_trivial_conversion: |
| 5265 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 5266 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5267 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5268 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5269 | case ovl_fail_bad_conversion: { |
| 5270 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 5271 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 5272 | if (Cand->Conversions[I].isBad()) |
| 5273 | return DiagnoseBadConversion(S, Cand, I); |
| 5274 | |
| 5275 | // FIXME: this currently happens when we're called from SemaInit |
| 5276 | // when user-conversion overload fails. Figure out how to handle |
| 5277 | // those conditions and diagnose them well. |
| 5278 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5279 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5280 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 5284 | // Desugar the type of the surrogate down to a function type, |
| 5285 | // retaining as many typedefs as possible while still showing |
| 5286 | // the function type (and, therefore, its parameter types). |
| 5287 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 5288 | bool isLValueReference = false; |
| 5289 | bool isRValueReference = false; |
| 5290 | bool isPointer = false; |
| 5291 | if (const LValueReferenceType *FnTypeRef = |
| 5292 | FnType->getAs<LValueReferenceType>()) { |
| 5293 | FnType = FnTypeRef->getPointeeType(); |
| 5294 | isLValueReference = true; |
| 5295 | } else if (const RValueReferenceType *FnTypeRef = |
| 5296 | FnType->getAs<RValueReferenceType>()) { |
| 5297 | FnType = FnTypeRef->getPointeeType(); |
| 5298 | isRValueReference = true; |
| 5299 | } |
| 5300 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 5301 | FnType = FnTypePtr->getPointeeType(); |
| 5302 | isPointer = true; |
| 5303 | } |
| 5304 | // Desugar down to a function type. |
| 5305 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 5306 | // Reconstruct the pointer/reference as appropriate. |
| 5307 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 5308 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 5309 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 5310 | |
| 5311 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 5312 | << FnType; |
| 5313 | } |
| 5314 | |
| 5315 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 5316 | const char *Opc, |
| 5317 | SourceLocation OpLoc, |
| 5318 | OverloadCandidate *Cand) { |
| 5319 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 5320 | std::string TypeStr("operator"); |
| 5321 | TypeStr += Opc; |
| 5322 | TypeStr += "("; |
| 5323 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 5324 | if (Cand->Conversions.size() == 1) { |
| 5325 | TypeStr += ")"; |
| 5326 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 5327 | } else { |
| 5328 | TypeStr += ", "; |
| 5329 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 5330 | TypeStr += ")"; |
| 5331 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 5336 | OverloadCandidate *Cand) { |
| 5337 | unsigned NoOperands = Cand->Conversions.size(); |
| 5338 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 5339 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5340 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 5341 | if (!ICS.isAmbiguous()) continue; |
| 5342 | |
| 5343 | S.DiagnoseAmbiguousConversion(ICS, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5344 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5345 | } |
| 5346 | } |
| 5347 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5348 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 5349 | if (Cand->Function) |
| 5350 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5351 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5352 | return Cand->Surrogate->getLocation(); |
| 5353 | return SourceLocation(); |
| 5354 | } |
| 5355 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5356 | struct CompareOverloadCandidatesForDisplay { |
| 5357 | Sema &S; |
| 5358 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5359 | |
| 5360 | bool operator()(const OverloadCandidate *L, |
| 5361 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 5362 | // Fast-path this check. |
| 5363 | if (L == R) return false; |
| 5364 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5365 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5366 | if (L->Viable) { |
| 5367 | if (!R->Viable) return true; |
| 5368 | |
| 5369 | // TODO: introduce a tri-valued comparison for overload |
| 5370 | // candidates. Would be more worthwhile if we had a sort |
| 5371 | // that could exploit it. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5372 | if (S.isBetterOverloadCandidate(*L, *R, SourceLocation())) return true; |
| 5373 | if (S.isBetterOverloadCandidate(*R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5374 | } else if (R->Viable) |
| 5375 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5376 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5377 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5378 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5379 | // Criteria by which we can sort non-viable candidates: |
| 5380 | if (!L->Viable) { |
| 5381 | // 1. Arity mismatches come after other candidates. |
| 5382 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 5383 | L->FailureKind == ovl_fail_too_few_arguments) |
| 5384 | return false; |
| 5385 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 5386 | R->FailureKind == ovl_fail_too_few_arguments) |
| 5387 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5388 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5389 | // 2. Bad conversions come first and are ordered by the number |
| 5390 | // of bad conversions and quality of good conversions. |
| 5391 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 5392 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 5393 | return true; |
| 5394 | |
| 5395 | // If there's any ordering between the defined conversions... |
| 5396 | // FIXME: this might not be transitive. |
| 5397 | assert(L->Conversions.size() == R->Conversions.size()); |
| 5398 | |
| 5399 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 5400 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 5401 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5402 | switch (S.CompareImplicitConversionSequences(L->Conversions[I], |
| 5403 | R->Conversions[I])) { |
| 5404 | case ImplicitConversionSequence::Better: |
| 5405 | leftBetter++; |
| 5406 | break; |
| 5407 | |
| 5408 | case ImplicitConversionSequence::Worse: |
| 5409 | leftBetter--; |
| 5410 | break; |
| 5411 | |
| 5412 | case ImplicitConversionSequence::Indistinguishable: |
| 5413 | break; |
| 5414 | } |
| 5415 | } |
| 5416 | if (leftBetter > 0) return true; |
| 5417 | if (leftBetter < 0) return false; |
| 5418 | |
| 5419 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 5420 | return false; |
| 5421 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 5422 | // TODO: others? |
| 5423 | } |
| 5424 | |
| 5425 | // Sort everything else by location. |
| 5426 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 5427 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 5428 | |
| 5429 | // Put candidates without locations (e.g. builtins) at the end. |
| 5430 | if (LLoc.isInvalid()) return false; |
| 5431 | if (RLoc.isInvalid()) return true; |
| 5432 | |
| 5433 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5434 | } |
| 5435 | }; |
| 5436 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5437 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 5438 | /// computes up to the first |
| 5439 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 5440 | Expr **Args, unsigned NumArgs) { |
| 5441 | assert(!Cand->Viable); |
| 5442 | |
| 5443 | // Don't do anything on failures other than bad conversion. |
| 5444 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 5445 | |
| 5446 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5447 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5448 | unsigned ConvCount = Cand->Conversions.size(); |
| 5449 | while (true) { |
| 5450 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 5451 | ConvIdx++; |
| 5452 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 5453 | break; |
| 5454 | } |
| 5455 | |
| 5456 | if (ConvIdx == ConvCount) |
| 5457 | return; |
| 5458 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 5459 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 5460 | "remaining conversion is initialized?"); |
| 5461 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 5462 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5463 | // operation somehow. |
| 5464 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5465 | |
| 5466 | const FunctionProtoType* Proto; |
| 5467 | unsigned ArgIdx = ConvIdx; |
| 5468 | |
| 5469 | if (Cand->IsSurrogate) { |
| 5470 | QualType ConvType |
| 5471 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 5472 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 5473 | ConvType = ConvPtrType->getPointeeType(); |
| 5474 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 5475 | ArgIdx--; |
| 5476 | } else if (Cand->Function) { |
| 5477 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 5478 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 5479 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 5480 | ArgIdx--; |
| 5481 | } else { |
| 5482 | // Builtin binary operator with a bad first conversion. |
| 5483 | assert(ConvCount <= 3); |
| 5484 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 5485 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5486 | = TryCopyInitialization(S, Args[ConvIdx], |
| 5487 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
| 5488 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5489 | /*InOverloadResolution*/ true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5490 | return; |
| 5491 | } |
| 5492 | |
| 5493 | // Fill in the rest of the conversions. |
| 5494 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 5495 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 5496 | if (ArgIdx < NumArgsInProto) |
| 5497 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5498 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
| 5499 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 5500 | /*InOverloadResolution=*/true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5501 | else |
| 5502 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 5503 | } |
| 5504 | } |
| 5505 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5506 | } // end anonymous namespace |
| 5507 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5508 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 5509 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5510 | /// set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5511 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5512 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5513 | OverloadCandidateDisplayKind OCD, |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 5514 | Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 5515 | const char *Opc, |
Fariborz Jahanian | 29f9d39 | 2009-10-09 00:13:15 +0000 | [diff] [blame] | 5516 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5517 | // Sort the candidates by viability and position. Sorting directly would |
| 5518 | // be prohibitive, so we make a set of pointers and sort those. |
| 5519 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
| 5520 | if (OCD == OCD_AllCandidates) Cands.reserve(CandidateSet.size()); |
| 5521 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 5522 | LastCand = CandidateSet.end(); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5523 | Cand != LastCand; ++Cand) { |
| 5524 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5525 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 5526 | else if (OCD == OCD_AllCandidates) { |
| 5527 | CompleteNonViableCandidate(*this, Cand, Args, NumArgs); |
| 5528 | Cands.push_back(Cand); |
| 5529 | } |
| 5530 | } |
| 5531 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 5532 | std::sort(Cands.begin(), Cands.end(), |
| 5533 | CompareOverloadCandidatesForDisplay(*this)); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5534 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5535 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5536 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 5537 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
| 5538 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 5539 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 5540 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5541 | if (Cand->Function) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5542 | NoteFunctionCandidate(*this, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5543 | else if (Cand->IsSurrogate) |
| 5544 | NoteSurrogateCandidate(*this, Cand); |
| 5545 | |
| 5546 | // This a builtin candidate. We do not, in general, want to list |
| 5547 | // every possible builtin candidate. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5548 | else if (Cand->Viable) { |
| 5549 | // Generally we only see ambiguities including viable builtin |
| 5550 | // operators if overload resolution got screwed up by an |
| 5551 | // ambiguous user-defined conversion. |
| 5552 | // |
| 5553 | // FIXME: It's quite possible for different conversions to see |
| 5554 | // different ambiguities, though. |
| 5555 | if (!ReportedAmbiguousConversions) { |
| 5556 | NoteAmbiguousUserConversions(*this, OpLoc, Cand); |
| 5557 | ReportedAmbiguousConversions = true; |
| 5558 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5559 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 5560 | // If this is a viable builtin, print it. |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 5561 | NoteBuiltinOperatorCandidate(*this, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5562 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5566 | static bool CheckUnresolvedAccess(Sema &S, OverloadExpr *E, DeclAccessPair D) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5567 | if (isa<UnresolvedLookupExpr>(E)) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5568 | return S.CheckUnresolvedLookupAccess(cast<UnresolvedLookupExpr>(E), D); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5569 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5570 | return S.CheckUnresolvedMemberAccess(cast<UnresolvedMemberExpr>(E), D); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5573 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 5574 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 5575 | /// expression with overloaded function type and @p ToType is the type |
| 5576 | /// we're trying to resolve to. For example: |
| 5577 | /// |
| 5578 | /// @code |
| 5579 | /// int f(double); |
| 5580 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5581 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5582 | /// int (*pfd)(double) = f; // selects f(double) |
| 5583 | /// @endcode |
| 5584 | /// |
| 5585 | /// This routine returns the resulting FunctionDecl if it could be |
| 5586 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 5587 | /// routine will emit diagnostics if there is an error. |
| 5588 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5589 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5590 | bool Complain, |
| 5591 | DeclAccessPair &FoundResult) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5592 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5593 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5594 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5595 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5596 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 5597 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5598 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5599 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5600 | FunctionType = MemTypePtr->getPointeeType(); |
| 5601 | IsMember = true; |
| 5602 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5603 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5604 | // C++ [over.over]p1: |
| 5605 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5606 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5607 | // C++ [over.over]p1: |
| 5608 | // [...] The overloaded function name can be preceded by the & |
| 5609 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5610 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
| 5611 | TemplateArgumentListInfo ETABuffer, *ExplicitTemplateArgs = 0; |
| 5612 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 5613 | OvlExpr->getExplicitTemplateArgs().copyInto(ETABuffer); |
| 5614 | ExplicitTemplateArgs = &ETABuffer; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5615 | } |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5616 | |
| 5617 | // We expect a pointer or reference to function, or a function pointer. |
| 5618 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
| 5619 | if (!FunctionType->isFunctionType()) { |
| 5620 | if (Complain) |
| 5621 | Diag(From->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 5622 | << OvlExpr->getName() << ToType; |
| 5623 | |
| 5624 | return 0; |
| 5625 | } |
| 5626 | |
| 5627 | assert(From->getType() == Context.OverloadTy); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5628 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5629 | // Look through all of the overloaded functions, searching for one |
| 5630 | // whose type matches exactly. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5631 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
Douglas Gregor | b242683d | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 5632 | llvm::SmallVector<FunctionDecl *, 4> NonMatches; |
| 5633 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5634 | bool FoundNonTemplateFunction = false; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5635 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5636 | E = OvlExpr->decls_end(); I != E; ++I) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5637 | // Look through any using declarations to find the underlying function. |
| 5638 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 5639 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5640 | // C++ [over.over]p3: |
| 5641 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 5642 | // targets of type "pointer-to-function" or "reference-to-function." |
| 5643 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5644 | // type "pointer-to-member-function." |
| 5645 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5646 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5647 | if (FunctionTemplateDecl *FunctionTemplate |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5648 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5649 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5650 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5651 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5652 | // static when converting to member pointer. |
| 5653 | if (Method->isStatic() == IsMember) |
| 5654 | continue; |
| 5655 | } else if (IsMember) |
| 5656 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5658 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5659 | // If the name is a function template, template argument deduction is |
| 5660 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5661 | // resulting template argument list is used to generate a single |
| 5662 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5663 | // overloaded functions considered. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5664 | // 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] | 5665 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5666 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5667 | if (TemplateDeductionResult Result |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5668 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5669 | FunctionType, Specialization, Info)) { |
| 5670 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5671 | (void)Result; |
| 5672 | } else { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5673 | // FIXME: If the match isn't exact, shouldn't we just drop this as |
| 5674 | // a candidate? Find a testcase before changing the code. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5675 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5676 | == Context.getCanonicalType(Specialization->getType())); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5677 | Matches.push_back(std::make_pair(I.getPair(), |
| 5678 | cast<FunctionDecl>(Specialization->getCanonicalDecl()))); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5679 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5680 | |
| 5681 | continue; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5682 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5683 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5684 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5685 | // Skip non-static functions when converting to pointer, and static |
| 5686 | // when converting to member pointer. |
| 5687 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5688 | continue; |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5689 | |
| 5690 | // If we have explicit template arguments, skip non-templates. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5691 | if (OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 5692 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5693 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5694 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5695 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 5696 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 5697 | QualType ResultTy; |
| 5698 | if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || |
| 5699 | IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, |
| 5700 | ResultTy)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5701 | Matches.push_back(std::make_pair(I.getPair(), |
| 5702 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5703 | FoundNonTemplateFunction = true; |
| 5704 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5705 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5708 | // If there were 0 or 1 matches, we're done. |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5709 | if (Matches.empty()) { |
| 5710 | if (Complain) { |
| 5711 | Diag(From->getLocStart(), diag::err_addr_ovl_no_viable) |
| 5712 | << OvlExpr->getName() << FunctionType; |
| 5713 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5714 | E = OvlExpr->decls_end(); |
| 5715 | I != E; ++I) |
| 5716 | if (FunctionDecl *F = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl())) |
| 5717 | NoteOverloadCandidate(F); |
| 5718 | } |
| 5719 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5720 | return 0; |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 5721 | } else if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5722 | FunctionDecl *Result = Matches[0].second; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5723 | FoundResult = Matches[0].first; |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5724 | MarkDeclarationReferenced(From->getLocStart(), Result); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5725 | if (Complain) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5726 | CheckAddressOfMemberAccess(OvlExpr, Matches[0].first); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5727 | return Result; |
| 5728 | } |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5729 | |
| 5730 | // C++ [over.over]p4: |
| 5731 | // If more than one function is selected, [...] |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5732 | if (!FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 5733 | // [...] and any given function template specialization F1 is |
| 5734 | // eliminated if the set contains a second function template |
| 5735 | // specialization whose function template is more specialized |
| 5736 | // than the function template of F1 according to the partial |
| 5737 | // ordering rules of 14.5.5.2. |
| 5738 | |
| 5739 | // The algorithm specified above is quadratic. We instead use a |
| 5740 | // two-pass algorithm (similar to the one used to identify the |
| 5741 | // best viable function in an overload set) that identifies the |
| 5742 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5743 | |
| 5744 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 5745 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5746 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5747 | |
| 5748 | UnresolvedSetIterator Result = |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5749 | getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5750 | TPOC_Other, From->getLocStart(), |
| 5751 | PDiag(), |
| 5752 | PDiag(diag::err_addr_ovl_ambiguous) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5753 | << Matches[0].second->getDeclName(), |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 5754 | PDiag(diag::note_ovl_candidate) |
| 5755 | << (unsigned) oc_function_template); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5756 | assert(Result != MatchesCopy.end() && "no most-specialized template"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5757 | MarkDeclarationReferenced(From->getLocStart(), *Result); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5758 | FoundResult = Matches[Result - MatchesCopy.begin()].first; |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5759 | if (Complain) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5760 | CheckUnresolvedAccess(*this, OvlExpr, FoundResult); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5761 | DiagnoseUseOfDecl(FoundResult, OvlExpr->getNameLoc()); |
| 5762 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5763 | return cast<FunctionDecl>(*Result); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5764 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5765 | |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5766 | // [...] any function template specializations in the set are |
| 5767 | // eliminated if the set also contains a non-template function, [...] |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5768 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5769 | if (Matches[I].second->getPrimaryTemplate() == 0) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5770 | ++I; |
| 5771 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5772 | Matches[I] = Matches[--N]; |
| 5773 | Matches.set_size(N); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5774 | } |
| 5775 | } |
Douglas Gregor | fae1d71 | 2009-09-26 03:56:17 +0000 | [diff] [blame] | 5776 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5777 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5778 | // selected function. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5779 | if (Matches.size() == 1) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5780 | MarkDeclarationReferenced(From->getLocStart(), Matches[0].second); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5781 | FoundResult = Matches[0].first; |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5782 | if (Complain) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5783 | CheckUnresolvedAccess(*this, OvlExpr, Matches[0].first); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5784 | DiagnoseUseOfDecl(Matches[0].first, OvlExpr->getNameLoc()); |
| 5785 | } |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5786 | return cast<FunctionDecl>(Matches[0].second); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 5787 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5788 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 5789 | // FIXME: We should probably return the same thing that BestViableFunction |
| 5790 | // returns (even if we issue the diagnostics here). |
| 5791 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5792 | << Matches[0].second->getDeclName(); |
| 5793 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 5794 | NoteOverloadCandidate(Matches[I].second); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5795 | return 0; |
| 5796 | } |
| 5797 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5798 | /// \brief Given an expression that refers to an overloaded function, try to |
| 5799 | /// resolve that overloaded function expression down to a single function. |
| 5800 | /// |
| 5801 | /// This routine can only resolve template-ids that refer to a single function |
| 5802 | /// template, where that template-id refers to a single template whose template |
| 5803 | /// arguments are either provided by the template-id or have defaults, |
| 5804 | /// as described in C++0x [temp.arg.explicit]p3. |
| 5805 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From) { |
| 5806 | // C++ [over.over]p1: |
| 5807 | // [...] [Note: any redundant set of parentheses surrounding the |
| 5808 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5809 | // C++ [over.over]p1: |
| 5810 | // [...] The overloaded function name can be preceded by the & |
| 5811 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5812 | |
| 5813 | if (From->getType() != Context.OverloadTy) |
| 5814 | return 0; |
| 5815 | |
| 5816 | OverloadExpr *OvlExpr = OverloadExpr::find(From).getPointer(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5817 | |
| 5818 | // If we didn't actually find any template-ids, we're done. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5819 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5820 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5821 | |
| 5822 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 5823 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5824 | |
| 5825 | // Look through all of the overloaded functions, searching for one |
| 5826 | // whose type matches exactly. |
| 5827 | FunctionDecl *Matched = 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 5828 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 5829 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5830 | // C++0x [temp.arg.explicit]p3: |
| 5831 | // [...] In contexts where deduction is done and fails, or in contexts |
| 5832 | // where deduction is not done, if a template argument list is |
| 5833 | // specified and it, along with any default template arguments, |
| 5834 | // identifies a single function template specialization, then the |
| 5835 | // template-id is an lvalue for the function template specialization. |
| 5836 | FunctionTemplateDecl *FunctionTemplate = cast<FunctionTemplateDecl>(*I); |
| 5837 | |
| 5838 | // C++ [over.over]p2: |
| 5839 | // If the name is a function template, template argument deduction is |
| 5840 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 5841 | // resulting template argument list is used to generate a single |
| 5842 | // function template specialization, which is added to the set of |
| 5843 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5844 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5845 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 5846 | if (TemplateDeductionResult Result |
| 5847 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 5848 | Specialization, Info)) { |
| 5849 | // FIXME: make a note of the failed deduction for diagnostics. |
| 5850 | (void)Result; |
| 5851 | continue; |
| 5852 | } |
| 5853 | |
| 5854 | // Multiple matches; we can't resolve to a single declaration. |
| 5855 | if (Matched) |
| 5856 | return 0; |
| 5857 | |
| 5858 | Matched = Specialization; |
| 5859 | } |
| 5860 | |
| 5861 | return Matched; |
| 5862 | } |
| 5863 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5864 | /// \brief Add a single candidate to the overload set. |
| 5865 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5866 | DeclAccessPair FoundDecl, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5867 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5868 | Expr **Args, unsigned NumArgs, |
| 5869 | OverloadCandidateSet &CandidateSet, |
| 5870 | bool PartialOverloading) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5871 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5872 | if (isa<UsingShadowDecl>(Callee)) |
| 5873 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 5874 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5875 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5876 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5877 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 5878 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5879 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | if (FunctionTemplateDecl *FuncTemplate |
| 5883 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5884 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 5885 | ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5886 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5887 | return; |
| 5888 | } |
| 5889 | |
| 5890 | assert(false && "unhandled case in overloaded call candidate"); |
| 5891 | |
| 5892 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 5896 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5897 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5898 | Expr **Args, unsigned NumArgs, |
| 5899 | OverloadCandidateSet &CandidateSet, |
| 5900 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5901 | |
| 5902 | #ifndef NDEBUG |
| 5903 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 5904 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5905 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5906 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 5907 | // and let Y be the lookup set produced by argument dependent |
| 5908 | // lookup (defined as follows). If X contains |
| 5909 | // |
| 5910 | // -- a declaration of a class member, or |
| 5911 | // |
| 5912 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5913 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5914 | // |
| 5915 | // -- a declaration that is neither a function or a function |
| 5916 | // template |
| 5917 | // |
| 5918 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5919 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5920 | if (ULE->requiresADL()) { |
| 5921 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5922 | E = ULE->decls_end(); I != E; ++I) { |
| 5923 | assert(!(*I)->getDeclContext()->isRecord()); |
| 5924 | assert(isa<UsingShadowDecl>(*I) || |
| 5925 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 5926 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5927 | } |
| 5928 | } |
| 5929 | #endif |
| 5930 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5931 | // It would be nice to avoid this copy. |
| 5932 | TemplateArgumentListInfo TABuffer; |
| 5933 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5934 | if (ULE->hasExplicitTemplateArgs()) { |
| 5935 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5936 | ExplicitTemplateArgs = &TABuffer; |
| 5937 | } |
| 5938 | |
| 5939 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 5940 | E = ULE->decls_end(); I != E; ++I) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5941 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5942 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5943 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5944 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5945 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 5946 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 5947 | Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5948 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 5949 | CandidateSet, |
| 5950 | PartialOverloading); |
| 5951 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5952 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5953 | static Sema::OwningExprResult Destroy(Sema &SemaRef, Expr *Fn, |
| 5954 | Expr **Args, unsigned NumArgs) { |
| 5955 | Fn->Destroy(SemaRef.Context); |
| 5956 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 5957 | Args[Arg]->Destroy(SemaRef.Context); |
| 5958 | return SemaRef.ExprError(); |
| 5959 | } |
| 5960 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5961 | /// Attempts to recover from a call where no functions were found. |
| 5962 | /// |
| 5963 | /// Returns true if new candidates were found. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5964 | static Sema::OwningExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5965 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5966 | UnresolvedLookupExpr *ULE, |
| 5967 | SourceLocation LParenLoc, |
| 5968 | Expr **Args, unsigned NumArgs, |
| 5969 | SourceLocation *CommaLocs, |
| 5970 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5971 | |
| 5972 | CXXScopeSpec SS; |
| 5973 | if (ULE->getQualifier()) { |
| 5974 | SS.setScopeRep(ULE->getQualifier()); |
| 5975 | SS.setRange(ULE->getQualifierRange()); |
| 5976 | } |
| 5977 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5978 | TemplateArgumentListInfo TABuffer; |
| 5979 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 5980 | if (ULE->hasExplicitTemplateArgs()) { |
| 5981 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 5982 | ExplicitTemplateArgs = &TABuffer; |
| 5983 | } |
| 5984 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5985 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 5986 | Sema::LookupOrdinaryName); |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 5987 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R)) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5988 | return Destroy(SemaRef, Fn, Args, NumArgs); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 5989 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5990 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 5991 | |
| 5992 | // Build an implicit member call if appropriate. Just drop the |
| 5993 | // casts and such from the call, we don't really care. |
| 5994 | Sema::OwningExprResult NewFn = SemaRef.ExprError(); |
| 5995 | if ((*R.begin())->isCXXClassMember()) |
| 5996 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, ExplicitTemplateArgs); |
| 5997 | else if (ExplicitTemplateArgs) |
| 5998 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 5999 | else |
| 6000 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 6001 | |
| 6002 | if (NewFn.isInvalid()) |
| 6003 | return Destroy(SemaRef, Fn, Args, NumArgs); |
| 6004 | |
| 6005 | Fn->Destroy(SemaRef.Context); |
| 6006 | |
| 6007 | // This shouldn't cause an infinite loop because we're giving it |
| 6008 | // an expression with non-empty lookup results, which should never |
| 6009 | // end up here. |
| 6010 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, move(NewFn), LParenLoc, |
| 6011 | Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs), |
| 6012 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6013 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6014 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6015 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6016 | /// (which eventually refers to the declaration Func) and the call |
| 6017 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 6018 | /// to a specific function. If overload resolution succeeds, returns |
| 6019 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 6020 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6021 | /// arguments and Fn, and returns NULL. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6022 | Sema::OwningExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6023 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6024 | SourceLocation LParenLoc, |
| 6025 | Expr **Args, unsigned NumArgs, |
| 6026 | SourceLocation *CommaLocs, |
| 6027 | SourceLocation RParenLoc) { |
| 6028 | #ifndef NDEBUG |
| 6029 | if (ULE->requiresADL()) { |
| 6030 | // To do ADL, we must have found an unqualified name. |
| 6031 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 6032 | |
| 6033 | // We don't perform ADL for implicit declarations of builtins. |
| 6034 | // Verify that this was correctly set up. |
| 6035 | FunctionDecl *F; |
| 6036 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 6037 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 6038 | F->getBuiltinID() && F->isImplicit()) |
| 6039 | assert(0 && "performing ADL for builtin"); |
| 6040 | |
| 6041 | // We don't perform ADL in C. |
| 6042 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 6043 | } |
| 6044 | #endif |
| 6045 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6046 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 6047 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6048 | // Add the functions denoted by the callee to the set of candidate |
| 6049 | // functions, including those from argument-dependent lookup. |
| 6050 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6051 | |
| 6052 | // If we found nothing, try to recover. |
| 6053 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 6054 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6055 | if (CandidateSet.empty()) |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 6056 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6057 | CommaLocs, RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 6058 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6059 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6060 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6061 | case OR_Success: { |
| 6062 | FunctionDecl *FDecl = Best->Function; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6063 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6064 | DiagnoseUseOfDecl(Best->FoundDecl, ULE->getNameLoc()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6065 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6066 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 6067 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6068 | |
| 6069 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 6070 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6071 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6072 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6073 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6074 | break; |
| 6075 | |
| 6076 | case OR_Ambiguous: |
| 6077 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6078 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6079 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6080 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6081 | |
| 6082 | case OR_Deleted: |
| 6083 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 6084 | << Best->Function->isDeleted() |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6085 | << ULE->getName() |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6086 | << Fn->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6087 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6088 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | // Overload resolution failed. Destroy all of the subexpressions and |
| 6092 | // return NULL. |
| 6093 | Fn->Destroy(Context); |
| 6094 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 6095 | Args[Arg]->Destroy(Context); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 6096 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6099 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6100 | return Functions.size() > 1 || |
| 6101 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 6102 | } |
| 6103 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6104 | /// \brief Create a unary operation that may resolve to an overloaded |
| 6105 | /// operator. |
| 6106 | /// |
| 6107 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 6108 | /// |
| 6109 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 6110 | /// operator. |
| 6111 | /// |
| 6112 | /// \param Functions The set of non-member functions that will be |
| 6113 | /// considered by overload resolution. The caller needs to build this |
| 6114 | /// set based on the context using, e.g., |
| 6115 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6116 | /// set should not contain any member functions; those will be added |
| 6117 | /// by CreateOverloadedUnaryOp(). |
| 6118 | /// |
| 6119 | /// \param input The input argument. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6120 | Sema::OwningExprResult |
| 6121 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 6122 | const UnresolvedSetImpl &Fns, |
| 6123 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6124 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 6125 | Expr *Input = (Expr *)input.get(); |
| 6126 | |
| 6127 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 6128 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 6129 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6130 | |
| 6131 | Expr *Args[2] = { Input, 0 }; |
| 6132 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6133 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6134 | // For post-increment and post-decrement, add the implicit '0' as |
| 6135 | // the second argument, so that we know this is a post-increment or |
| 6136 | // post-decrement. |
| 6137 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 6138 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6139 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6140 | SourceLocation()); |
| 6141 | NumArgs = 2; |
| 6142 | } |
| 6143 | |
| 6144 | if (Input->isTypeDependent()) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6145 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6146 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6147 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6148 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6149 | /*ADL*/ true, IsOverloaded(Fns)); |
| 6150 | Fn->addDecls(Fns.begin(), Fns.end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6151 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6152 | input.release(); |
| 6153 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 6154 | &Args[0], NumArgs, |
| 6155 | Context.DependentTy, |
| 6156 | OpLoc)); |
| 6157 | } |
| 6158 | |
| 6159 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6160 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6161 | |
| 6162 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6163 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6164 | |
| 6165 | // Add operator candidates that are member functions. |
| 6166 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 6167 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6168 | // Add candidates from ADL. |
| 6169 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | 6ec89d4 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 6170 | Args, NumArgs, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6171 | /*ExplicitTemplateArgs*/ 0, |
| 6172 | CandidateSet); |
| 6173 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6174 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6175 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6176 | |
| 6177 | // Perform overload resolution. |
| 6178 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6179 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6180 | case OR_Success: { |
| 6181 | // We found a built-in operator or an overloaded operator. |
| 6182 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6183 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6184 | if (FnDecl) { |
| 6185 | // We matched an overloaded operator. Build a call to that |
| 6186 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6187 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6188 | // Convert the arguments. |
| 6189 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6190 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6191 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6192 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 6193 | Best->FoundDecl, Method)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6194 | return ExprError(); |
| 6195 | } else { |
| 6196 | // Convert the arguments. |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6197 | OwningExprResult InputInit |
| 6198 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6199 | FnDecl->getParamDecl(0)), |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6200 | SourceLocation(), |
| 6201 | move(input)); |
| 6202 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6203 | return ExprError(); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 6205 | input = move(InputInit); |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 6206 | Input = (Expr *)input.get(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6209 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6210 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6211 | // Determine the result type |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6212 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6213 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6214 | // Build the actual expression node. |
| 6215 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6216 | SourceLocation()); |
| 6217 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6218 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6219 | input.release(); |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6220 | Args[0] = Input; |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6221 | ExprOwningPtr<CallExpr> TheCall(this, |
| 6222 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 6223 | Args, NumArgs, ResultTy, OpLoc)); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6224 | |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 6225 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6226 | FnDecl)) |
| 6227 | return ExprError(); |
| 6228 | |
| 6229 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6230 | } else { |
| 6231 | // We matched a built-in operator. Convert the arguments, then |
| 6232 | // break out so that we will build the appropriate built-in |
| 6233 | // operator node. |
| 6234 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6235 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6236 | return ExprError(); |
| 6237 | |
| 6238 | break; |
| 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | case OR_No_Viable_Function: |
| 6243 | // No viable function; fall through to handling this as a |
| 6244 | // built-in operator, which will produce an error message for us. |
| 6245 | break; |
| 6246 | |
| 6247 | case OR_Ambiguous: |
| 6248 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6249 | << UnaryOperator::getOpcodeStr(Opc) |
| 6250 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6251 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6252 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6253 | return ExprError(); |
| 6254 | |
| 6255 | case OR_Deleted: |
| 6256 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6257 | << Best->Function->isDeleted() |
| 6258 | << UnaryOperator::getOpcodeStr(Opc) |
| 6259 | << Input->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6260 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6261 | return ExprError(); |
| 6262 | } |
| 6263 | |
| 6264 | // Either we found no viable overloaded operator or we matched a |
| 6265 | // built-in operator. In either case, fall through to trying to |
| 6266 | // build a built-in operation. |
| 6267 | input.release(); |
| 6268 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 6269 | } |
| 6270 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6271 | /// \brief Create a binary operation that may resolve to an overloaded |
| 6272 | /// operator. |
| 6273 | /// |
| 6274 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 6275 | /// |
| 6276 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 6277 | /// operator. |
| 6278 | /// |
| 6279 | /// \param Functions The set of non-member functions that will be |
| 6280 | /// considered by overload resolution. The caller needs to build this |
| 6281 | /// set based on the context using, e.g., |
| 6282 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 6283 | /// set should not contain any member functions; those will be added |
| 6284 | /// by CreateOverloadedBinOp(). |
| 6285 | /// |
| 6286 | /// \param LHS Left-hand argument. |
| 6287 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6288 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6289 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6290 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6291 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6292 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6293 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6294 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6295 | |
| 6296 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 6297 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 6298 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 6299 | |
| 6300 | // If either side is type-dependent, create an appropriate dependent |
| 6301 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6302 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6303 | if (Fns.empty()) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6304 | // If there are no functions to store, just build a dependent |
| 6305 | // BinaryOperator or CompoundAssignment. |
| 6306 | if (Opc <= BinaryOperator::Assign || Opc > BinaryOperator::OrAssign) |
| 6307 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
| 6308 | Context.DependentTy, OpLoc)); |
| 6309 | |
| 6310 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 6311 | Context.DependentTy, |
| 6312 | Context.DependentTy, |
| 6313 | Context.DependentTy, |
| 6314 | OpLoc)); |
| 6315 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6316 | |
| 6317 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6318 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6319 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6320 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6321 | 0, SourceRange(), OpName, OpLoc, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6322 | /*ADL*/ true, IsOverloaded(Fns)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6323 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6324 | Fn->addDecls(Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6325 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6327 | Context.DependentTy, |
| 6328 | OpLoc)); |
| 6329 | } |
| 6330 | |
| 6331 | // If this is the .* operator, which is not overloadable, just |
| 6332 | // create a built-in binary operator. |
| 6333 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6334 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6335 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 6336 | // If this is the assignment operator, we only perform overload resolution |
| 6337 | // if the left-hand side is a class or enumeration type. This is actually |
| 6338 | // a hack. The standard requires that we do overload resolution between the |
| 6339 | // various built-in candidates, but as DR507 points out, this can lead to |
| 6340 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 6341 | // Note that we go the traditional code path for compound assignment forms. |
| 6342 | if (Opc==BinaryOperator::Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6343 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6345 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6346 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6347 | |
| 6348 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6349 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6350 | |
| 6351 | // Add operator candidates that are member functions. |
| 6352 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 6353 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6354 | // Add candidates from ADL. |
| 6355 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 6356 | Args, 2, |
| 6357 | /*ExplicitTemplateArgs*/ 0, |
| 6358 | CandidateSet); |
| 6359 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6360 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 6361 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6362 | |
| 6363 | // Perform overload resolution. |
| 6364 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6365 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6366 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6367 | // We found a built-in operator or an overloaded operator. |
| 6368 | FunctionDecl *FnDecl = Best->Function; |
| 6369 | |
| 6370 | if (FnDecl) { |
| 6371 | // We matched an overloaded operator. Build a call to that |
| 6372 | // operator. |
| 6373 | |
| 6374 | // Convert the arguments. |
| 6375 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6376 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6377 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 6378 | |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6379 | OwningExprResult Arg1 |
| 6380 | = PerformCopyInitialization( |
| 6381 | InitializedEntity::InitializeParameter( |
| 6382 | FnDecl->getParamDecl(0)), |
| 6383 | SourceLocation(), |
| 6384 | Owned(Args[1])); |
| 6385 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6386 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6387 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6388 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6389 | Best->FoundDecl, Method)) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6390 | return ExprError(); |
| 6391 | |
| 6392 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6393 | } else { |
| 6394 | // Convert the arguments. |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6395 | OwningExprResult Arg0 |
| 6396 | = PerformCopyInitialization( |
| 6397 | InitializedEntity::InitializeParameter( |
| 6398 | FnDecl->getParamDecl(0)), |
| 6399 | SourceLocation(), |
| 6400 | Owned(Args[0])); |
| 6401 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6402 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 6403 | |
| 6404 | OwningExprResult Arg1 |
| 6405 | = PerformCopyInitialization( |
| 6406 | InitializedEntity::InitializeParameter( |
| 6407 | FnDecl->getParamDecl(1)), |
| 6408 | SourceLocation(), |
| 6409 | Owned(Args[1])); |
| 6410 | if (Arg1.isInvalid()) |
| 6411 | return ExprError(); |
| 6412 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 6413 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6416 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 6417 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6418 | // Determine the result type |
| 6419 | QualType ResultTy |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6420 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6421 | ResultTy = ResultTy.getNonReferenceType(); |
| 6422 | |
| 6423 | // Build the actual expression node. |
| 6424 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 6425 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6426 | UsualUnaryConversions(FnExpr); |
| 6427 | |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 6428 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6429 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
| 6430 | Args, 2, ResultTy, |
| 6431 | OpLoc)); |
| 6432 | |
| 6433 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 6434 | FnDecl)) |
| 6435 | return ExprError(); |
| 6436 | |
| 6437 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6438 | } else { |
| 6439 | // We matched a built-in operator. Convert the arguments, then |
| 6440 | // break out so that we will build the appropriate built-in |
| 6441 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6442 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6443 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6444 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6445 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6446 | return ExprError(); |
| 6447 | |
| 6448 | break; |
| 6449 | } |
| 6450 | } |
| 6451 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6452 | case OR_No_Viable_Function: { |
| 6453 | // C++ [over.match.oper]p9: |
| 6454 | // If the operator is the operator , [...] and there are no |
| 6455 | // viable functions, then the operator is assumed to be the |
| 6456 | // built-in operator and interpreted according to clause 5. |
| 6457 | if (Opc == BinaryOperator::Comma) |
| 6458 | break; |
| 6459 | |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6460 | // For class as left operand for assignment or compound assigment operator |
| 6461 | // do not fall through to handling in built-in, but report that no overloaded |
| 6462 | // assignment operator found |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6463 | OwningExprResult Result = ExprError(); |
| 6464 | if (Args[0]->getType()->isRecordType() && |
| 6465 | Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6466 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 6467 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6468 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6469 | } else { |
| 6470 | // No viable function; try to create a built-in operation, which will |
| 6471 | // produce an error. Then, show the non-viable candidates. |
| 6472 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 6473 | } |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6474 | assert(Result.isInvalid() && |
| 6475 | "C++ binary operator overloading is missing candidates!"); |
| 6476 | if (Result.isInvalid()) |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6477 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6478 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6479 | return move(Result); |
| 6480 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6481 | |
| 6482 | case OR_Ambiguous: |
| 6483 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 6484 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6485 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6486 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Fariborz Jahanian | e719643 | 2009-10-12 20:11:40 +0000 | [diff] [blame] | 6487 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6488 | return ExprError(); |
| 6489 | |
| 6490 | case OR_Deleted: |
| 6491 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 6492 | << Best->Function->isDeleted() |
| 6493 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6494 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6495 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6496 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6497 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6498 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 6499 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 6500 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6501 | } |
| 6502 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6503 | Action::OwningExprResult |
| 6504 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 6505 | SourceLocation RLoc, |
| 6506 | ExprArg Base, ExprArg Idx) { |
| 6507 | Expr *Args[2] = { static_cast<Expr*>(Base.get()), |
| 6508 | static_cast<Expr*>(Idx.get()) }; |
| 6509 | DeclarationName OpName = |
| 6510 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 6511 | |
| 6512 | // If either side is type-dependent, create an appropriate dependent |
| 6513 | // expression. |
| 6514 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 6515 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6516 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6517 | UnresolvedLookupExpr *Fn |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6518 | = UnresolvedLookupExpr::Create(Context, /*Dependent*/ true, NamingClass, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6519 | 0, SourceRange(), OpName, LLoc, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 6520 | /*ADL*/ true, /*Overloaded*/ false); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 6521 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6522 | |
| 6523 | Base.release(); |
| 6524 | Idx.release(); |
| 6525 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 6526 | Args, 2, |
| 6527 | Context.DependentTy, |
| 6528 | RLoc)); |
| 6529 | } |
| 6530 | |
| 6531 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6532 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6533 | |
| 6534 | // Subscript can only be overloaded as a member function. |
| 6535 | |
| 6536 | // Add operator candidates that are member functions. |
| 6537 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6538 | |
| 6539 | // Add builtin operator candidates. |
| 6540 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 6541 | |
| 6542 | // Perform overload resolution. |
| 6543 | OverloadCandidateSet::iterator Best; |
| 6544 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
| 6545 | case OR_Success: { |
| 6546 | // We found a built-in operator or an overloaded operator. |
| 6547 | FunctionDecl *FnDecl = Best->Function; |
| 6548 | |
| 6549 | if (FnDecl) { |
| 6550 | // We matched an overloaded operator. Build a call to that |
| 6551 | // operator. |
| 6552 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6553 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6554 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6555 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6556 | // Convert the arguments. |
| 6557 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6558 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6559 | Best->FoundDecl, Method)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6560 | return ExprError(); |
| 6561 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 6562 | // Convert the arguments. |
| 6563 | OwningExprResult InputInit |
| 6564 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 6565 | FnDecl->getParamDecl(0)), |
| 6566 | SourceLocation(), |
| 6567 | Owned(Args[1])); |
| 6568 | if (InputInit.isInvalid()) |
| 6569 | return ExprError(); |
| 6570 | |
| 6571 | Args[1] = InputInit.takeAs<Expr>(); |
| 6572 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6573 | // Determine the result type |
| 6574 | QualType ResultTy |
| 6575 | = FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 6576 | ResultTy = ResultTy.getNonReferenceType(); |
| 6577 | |
| 6578 | // Build the actual expression node. |
| 6579 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 6580 | LLoc); |
| 6581 | UsualUnaryConversions(FnExpr); |
| 6582 | |
| 6583 | Base.release(); |
| 6584 | Idx.release(); |
| 6585 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6586 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 6587 | FnExpr, Args, 2, |
| 6588 | ResultTy, RLoc)); |
| 6589 | |
| 6590 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall.get(), |
| 6591 | FnDecl)) |
| 6592 | return ExprError(); |
| 6593 | |
| 6594 | return MaybeBindToTemporary(TheCall.release()); |
| 6595 | } else { |
| 6596 | // We matched a built-in operator. Convert the arguments, then |
| 6597 | // break out so that we will build the appropriate built-in |
| 6598 | // operator node. |
| 6599 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6600 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6601 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6602 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6603 | return ExprError(); |
| 6604 | |
| 6605 | break; |
| 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6610 | if (CandidateSet.empty()) |
| 6611 | Diag(LLoc, diag::err_ovl_no_oper) |
| 6612 | << Args[0]->getType() << /*subscript*/ 0 |
| 6613 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 6614 | else |
| 6615 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 6616 | << Args[0]->getType() |
| 6617 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6618 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6619 | "[]", LLoc); |
| 6620 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
| 6623 | case OR_Ambiguous: |
| 6624 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 6625 | << "[]" << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6626 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, 2, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6627 | "[]", LLoc); |
| 6628 | return ExprError(); |
| 6629 | |
| 6630 | case OR_Deleted: |
| 6631 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 6632 | << Best->Function->isDeleted() << "[]" |
| 6633 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6634 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, 2, |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6635 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 6636 | return ExprError(); |
| 6637 | } |
| 6638 | |
| 6639 | // We matched a built-in operator; build it. |
| 6640 | Base.release(); |
| 6641 | Idx.release(); |
| 6642 | return CreateBuiltinArraySubscriptExpr(Owned(Args[0]), LLoc, |
| 6643 | Owned(Args[1]), RLoc); |
| 6644 | } |
| 6645 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6646 | /// BuildCallToMemberFunction - Build a call to a member |
| 6647 | /// function. MemExpr is the expression that refers to the member |
| 6648 | /// function (and includes the object parameter), Args/NumArgs are the |
| 6649 | /// arguments to the function call (not including the object |
| 6650 | /// parameter). The caller needs to validate that the member |
| 6651 | /// expression refers to a member function or an overloaded member |
| 6652 | /// function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6653 | Sema::OwningExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6654 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 6655 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6656 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 6657 | SourceLocation RParenLoc) { |
| 6658 | // Dig out the member expression. This holds both the object |
| 6659 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6660 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
| 6661 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6662 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6663 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 6664 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6665 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6666 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 6667 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6668 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6669 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6670 | Qualifier = MemExpr->getQualifier(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6671 | } else { |
| 6672 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 6673 | Qualifier = UnresExpr->getQualifier(); |
| 6674 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6675 | QualType ObjectType = UnresExpr->getBaseType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6676 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6677 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6678 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6679 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6680 | // FIXME: avoid copy. |
| 6681 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 6682 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 6683 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 6684 | TemplateArgs = &TemplateArgsBuffer; |
| 6685 | } |
| 6686 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6687 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 6688 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 6689 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6690 | NamedDecl *Func = *I; |
| 6691 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 6692 | if (isa<UsingShadowDecl>(Func)) |
| 6693 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 6694 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6695 | if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6696 | // If explicit template arguments were provided, we can't call a |
| 6697 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6698 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 6699 | continue; |
| 6700 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6701 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6702 | Args, NumArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6703 | CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6704 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6705 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6706 | I.getPair(), ActingDC, TemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6707 | ObjectType, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6708 | CandidateSet, |
| 6709 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6710 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 6711 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6712 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6713 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 6714 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6715 | OverloadCandidateSet::iterator Best; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6716 | switch (BestViableFunction(CandidateSet, UnresExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6717 | case OR_Success: |
| 6718 | Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6719 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6720 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6721 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6722 | break; |
| 6723 | |
| 6724 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6725 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6726 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6727 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6728 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6729 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6730 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6731 | |
| 6732 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6733 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6734 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6735 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6736 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6737 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6738 | |
| 6739 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6740 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6741 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 6742 | << DeclName << MemExprE->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6743 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6744 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6745 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6748 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6749 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6750 | // If overload resolution picked a static member, build a |
| 6751 | // non-member call based on that function. |
| 6752 | if (Method->isStatic()) { |
| 6753 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 6754 | Args, NumArgs, RParenLoc); |
| 6755 | } |
| 6756 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 6757 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6758 | } |
| 6759 | |
| 6760 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6761 | ExprOwningPtr<CXXMemberCallExpr> |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6762 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExprE, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6763 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6764 | Method->getResultType().getNonReferenceType(), |
| 6765 | RParenLoc)); |
| 6766 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6767 | // Check for a valid return type. |
| 6768 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
| 6769 | TheCall.get(), Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6770 | return ExprError(); |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 6771 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6772 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6773 | // We only need to do this if there was actually an overload; otherwise |
| 6774 | // it was done at lookup. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6775 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6776 | if (!Method->isStatic() && |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6777 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 6778 | FoundDecl, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6779 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6780 | MemExpr->setBase(ObjectArg); |
| 6781 | |
| 6782 | // Convert the rest of the arguments |
Douglas Gregor | c8be952 | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 6783 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6784 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6785 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6786 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6787 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 6788 | if (CheckFunctionCall(Method, TheCall.get())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6789 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 6790 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 6791 | return MaybeBindToTemporary(TheCall.release()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6794 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 6795 | /// type (C++ [over.call.object]), which can end up invoking an |
| 6796 | /// overloaded function call operator (@c operator()) or performing a |
| 6797 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6798 | Sema::ExprResult |
| 6799 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 6800 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6801 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6802 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6803 | SourceLocation RParenLoc) { |
| 6804 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6805 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6806 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6807 | // C++ [over.call.object]p1: |
| 6808 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6809 | // 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] | 6810 | // candidate functions includes at least the function call |
| 6811 | // operators of T. The function call operators of T are obtained by |
| 6812 | // ordinary lookup of the name operator() in the context of |
| 6813 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6814 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6815 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6816 | |
| 6817 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6818 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6819 | << Object->getSourceRange())) |
| 6820 | return true; |
| 6821 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6822 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 6823 | LookupQualifiedName(R, Record->getDecl()); |
| 6824 | R.suppressDiagnostics(); |
| 6825 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 6826 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6827 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6828 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 6829 | Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 6830 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 6831 | } |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6832 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6833 | // C++ [over.call.object]p2: |
| 6834 | // In addition, for each conversion function declared in T of the |
| 6835 | // form |
| 6836 | // |
| 6837 | // operator conversion-type-id () cv-qualifier; |
| 6838 | // |
| 6839 | // where cv-qualifier is the same cv-qualification as, or a |
| 6840 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 6841 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 6842 | // R", or the type "reference to pointer to function of |
| 6843 | // (P1,...,Pn) returning R", or the type "reference to function |
| 6844 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6845 | // is also considered as a candidate function. Similarly, |
| 6846 | // surrogate call functions are added to the set of candidate |
| 6847 | // functions for each conversion function declared in an |
| 6848 | // accessible base class provided the function is not hidden |
| 6849 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6850 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 6851 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6852 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6853 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6854 | NamedDecl *D = *I; |
| 6855 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 6856 | if (isa<UsingShadowDecl>(D)) |
| 6857 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 6858 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6859 | // Skip over templated conversion functions; they aren't |
| 6860 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6861 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6862 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6863 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6864 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6865 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6866 | // Strip the reference type (if any) and then the pointer type (if |
| 6867 | // any) to get down to what might be a function type. |
| 6868 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 6869 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 6870 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6871 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 6872 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6873 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 6874 | Object->getType(), Args, NumArgs, |
| 6875 | CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6876 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6877 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6878 | // Perform overload resolution. |
| 6879 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6880 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6881 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6882 | // Overload resolution succeeded; we'll build the appropriate call |
| 6883 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6884 | break; |
| 6885 | |
| 6886 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 6887 | if (CandidateSet.empty()) |
| 6888 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 6889 | << Object->getType() << /*call*/ 1 |
| 6890 | << Object->getSourceRange(); |
| 6891 | else |
| 6892 | Diag(Object->getSourceRange().getBegin(), |
| 6893 | diag::err_ovl_no_viable_object_call) |
| 6894 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6895 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6896 | break; |
| 6897 | |
| 6898 | case OR_Ambiguous: |
| 6899 | Diag(Object->getSourceRange().getBegin(), |
| 6900 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6901 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6902 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6903 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6904 | |
| 6905 | case OR_Deleted: |
| 6906 | Diag(Object->getSourceRange().getBegin(), |
| 6907 | diag::err_ovl_deleted_object_call) |
| 6908 | << Best->Function->isDeleted() |
| 6909 | << Object->getType() << Object->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 6910 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6911 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6912 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6913 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6914 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6915 | // We had an error; delete all of the subexpressions and return |
| 6916 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6917 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6918 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6919 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6920 | return true; |
| 6921 | } |
| 6922 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6923 | if (Best->Function == 0) { |
| 6924 | // Since there is no function declaration, this is one of the |
| 6925 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6926 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6927 | = cast<CXXConversionDecl>( |
| 6928 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 6929 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6930 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6931 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6932 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6933 | // We selected one of the surrogate functions that converts the |
| 6934 | // object parameter to a function pointer. Perform the conversion |
| 6935 | // on the object argument, then let ActOnCallExpr finish the job. |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6936 | |
| 6937 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6938 | // and then call it. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6939 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(Object, Best->FoundDecl, |
| 6940 | Conv); |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 6941 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 6942 | return ActOnCallExpr(S, ExprArg(*this, CE), LParenLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6943 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
Douglas Gregor | e5e775b | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 6944 | CommaLocs, RParenLoc).result(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6947 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 6948 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 6949 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6950 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 6951 | // that calls this method, using Object for the implicit object |
| 6952 | // parameter and passing along the remaining arguments. |
| 6953 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6954 | const FunctionProtoType *Proto = Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6955 | |
| 6956 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 6957 | unsigned NumArgsToCheck = NumArgs; |
| 6958 | |
| 6959 | // Build the full argument list for the method call (the |
| 6960 | // implicit object parameter is placed at the beginning of the |
| 6961 | // list). |
| 6962 | Expr **MethodArgs; |
| 6963 | if (NumArgs < NumArgsInProto) { |
| 6964 | NumArgsToCheck = NumArgsInProto; |
| 6965 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 6966 | } else { |
| 6967 | MethodArgs = new Expr*[NumArgs + 1]; |
| 6968 | } |
| 6969 | MethodArgs[0] = Object; |
| 6970 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 6971 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6972 | |
| 6973 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6974 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6975 | UsualUnaryConversions(NewFn); |
| 6976 | |
| 6977 | // Once we've built TheCall, all of the expressions are properly |
| 6978 | // owned. |
| 6979 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6980 | ExprOwningPtr<CXXOperatorCallExpr> |
| 6981 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6982 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6983 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6984 | delete [] MethodArgs; |
| 6985 | |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 6986 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall.get(), |
| 6987 | Method)) |
| 6988 | return true; |
| 6989 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6990 | // We may have default arguments. If so, we need to allocate more |
| 6991 | // slots in the call for them. |
| 6992 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 6993 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 6994 | else if (NumArgs > NumArgsInProto) |
| 6995 | NumArgsToCheck = NumArgsInProto; |
| 6996 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 6997 | bool IsError = false; |
| 6998 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 6999 | // Initialize the implicit object parameter. |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 7000 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7001 | Best->FoundDecl, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7002 | TheCall->setArg(0, Object); |
| 7003 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7004 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7005 | // Check the argument types. |
| 7006 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7007 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7008 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7009 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7010 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7011 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 7012 | |
| 7013 | OwningExprResult InputInit |
| 7014 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
| 7015 | Method->getParamDecl(i)), |
| 7016 | SourceLocation(), Owned(Arg)); |
| 7017 | |
| 7018 | IsError |= InputInit.isInvalid(); |
| 7019 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7020 | } else { |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 7021 | OwningExprResult DefArg |
| 7022 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 7023 | if (DefArg.isInvalid()) { |
| 7024 | IsError = true; |
| 7025 | break; |
| 7026 | } |
| 7027 | |
| 7028 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 7029 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7030 | |
| 7031 | TheCall->setArg(i + 1, Arg); |
| 7032 | } |
| 7033 | |
| 7034 | // If this is a variadic call, handle args passed through "...". |
| 7035 | if (Proto->isVariadic()) { |
| 7036 | // Promote the arguments (C99 6.5.2.2p7). |
| 7037 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 7038 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7039 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7040 | TheCall->setArg(i + 1, Arg); |
| 7041 | } |
| 7042 | } |
| 7043 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 7044 | if (IsError) return true; |
| 7045 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 7046 | if (CheckFunctionCall(Method, TheCall.get())) |
| 7047 | return true; |
| 7048 | |
Douglas Gregor | e5e775b | 2010-04-13 15:50:39 +0000 | [diff] [blame] | 7049 | return MaybeBindToTemporary(TheCall.release()).result(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7052 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7053 | /// (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] | 7054 | /// @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] | 7055 | Sema::OwningExprResult |
| 7056 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 7057 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7058 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7059 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7060 | SourceLocation Loc = Base->getExprLoc(); |
| 7061 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7062 | // C++ [over.ref]p1: |
| 7063 | // |
| 7064 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 7065 | // for a class object x of type T if T::operator->() exists and if |
| 7066 | // the operator is selected as the best match function by the |
| 7067 | // overload resolution mechanism (13.3). |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7068 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7069 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7070 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7071 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7072 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 7073 | PDiag(diag::err_typecheck_incomplete_tag) |
| 7074 | << Base->getSourceRange())) |
| 7075 | return ExprError(); |
| 7076 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7077 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 7078 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 7079 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7080 | |
| 7081 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7082 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7083 | AddMethodCandidate(Oper.getPair(), Base->getType(), 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7084 | /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 7085 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7086 | |
| 7087 | // Perform overload resolution. |
| 7088 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7089 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7090 | case OR_Success: |
| 7091 | // Overload resolution succeeded; we'll build the call below. |
| 7092 | break; |
| 7093 | |
| 7094 | case OR_No_Viable_Function: |
| 7095 | if (CandidateSet.empty()) |
| 7096 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7097 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7098 | else |
| 7099 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7100 | << "operator->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7101 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7102 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7103 | |
| 7104 | case OR_Ambiguous: |
| 7105 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7106 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7107 | PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7108 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7109 | |
| 7110 | case OR_Deleted: |
| 7111 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7112 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 7113 | << "->" << Base->getSourceRange(); |
John McCall | ad90777 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 7114 | PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7115 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7116 | } |
| 7117 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7118 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7119 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7120 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7121 | // Convert the object parameter. |
| 7122 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7123 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 7124 | Best->FoundDecl, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7125 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 7126 | |
| 7127 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 7128 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7129 | |
| 7130 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 7131 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 7132 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7133 | UsualUnaryConversions(FnExpr); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 7134 | |
| 7135 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
| 7136 | ExprOwningPtr<CXXOperatorCallExpr> |
| 7137 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
| 7138 | &Base, 1, ResultTy, OpLoc)); |
| 7139 | |
| 7140 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall.get(), |
| 7141 | Method)) |
| 7142 | return ExprError(); |
| 7143 | return move(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 7144 | } |
| 7145 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7146 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 7147 | /// a C++ overloaded function (possibly with some parentheses and |
| 7148 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 7149 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7150 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7151 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7152 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7153 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7154 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 7155 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7156 | if (SubExpr == PE->getSubExpr()) |
| 7157 | return PE->Retain(); |
| 7158 | |
| 7159 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
| 7160 | } |
| 7161 | |
| 7162 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7163 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 7164 | Found, Fn); |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7165 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7166 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 7167 | "Implicit cast type cannot be determined from overload"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7168 | if (SubExpr == ICE->getSubExpr()) |
| 7169 | return ICE->Retain(); |
| 7170 | |
| 7171 | return new (Context) ImplicitCastExpr(ICE->getType(), |
| 7172 | ICE->getCastKind(), |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 7173 | SubExpr, CXXBaseSpecifierArray(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7174 | ICE->isLvalueCast()); |
| 7175 | } |
| 7176 | |
| 7177 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7178 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7179 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7180 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7181 | if (Method->isStatic()) { |
| 7182 | // Do nothing: static member functions aren't any different |
| 7183 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7184 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7185 | // Fix the sub expression, which really has to be an |
| 7186 | // UnresolvedLookupExpr holding an overloaded member function |
| 7187 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7188 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7189 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7190 | if (SubExpr == UnOp->getSubExpr()) |
| 7191 | return UnOp->Retain(); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7192 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7193 | assert(isa<DeclRefExpr>(SubExpr) |
| 7194 | && "fixed to something other than a decl ref"); |
| 7195 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 7196 | && "fixed to a member ref with no nested name qualifier"); |
| 7197 | |
| 7198 | // We have taken the address of a pointer to member |
| 7199 | // function. Perform the computation here so that we get the |
| 7200 | // appropriate pointer to member type. |
| 7201 | QualType ClassType |
| 7202 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 7203 | QualType MemPtrType |
| 7204 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 7205 | |
| 7206 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7207 | MemPtrType, UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 7208 | } |
| 7209 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7210 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 7211 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7212 | if (SubExpr == UnOp->getSubExpr()) |
| 7213 | return UnOp->Retain(); |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 7214 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7215 | return new (Context) UnaryOperator(SubExpr, UnaryOperator::AddrOf, |
| 7216 | Context.getPointerType(SubExpr->getType()), |
| 7217 | UnOp->getOperatorLoc()); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7218 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7219 | |
| 7220 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7221 | // FIXME: avoid copy. |
| 7222 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7223 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7224 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7225 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 7226 | } |
| 7227 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7228 | return DeclRefExpr::Create(Context, |
| 7229 | ULE->getQualifier(), |
| 7230 | ULE->getQualifierRange(), |
| 7231 | Fn, |
| 7232 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7233 | Fn->getType(), |
| 7234 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7235 | } |
| 7236 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 7237 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7238 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7239 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 7240 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 7241 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 7242 | TemplateArgs = &TemplateArgsBuffer; |
| 7243 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7244 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7245 | Expr *Base; |
| 7246 | |
| 7247 | // If we're filling in |
| 7248 | if (MemExpr->isImplicitAccess()) { |
| 7249 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 7250 | return DeclRefExpr::Create(Context, |
| 7251 | MemExpr->getQualifier(), |
| 7252 | MemExpr->getQualifierRange(), |
| 7253 | Fn, |
| 7254 | MemExpr->getMemberLoc(), |
| 7255 | Fn->getType(), |
| 7256 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 7257 | } else { |
| 7258 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 7259 | if (MemExpr->getQualifier()) |
| 7260 | Loc = MemExpr->getQualifierRange().getBegin(); |
| 7261 | Base = new (Context) CXXThisExpr(Loc, |
| 7262 | MemExpr->getBaseType(), |
| 7263 | /*isImplicit=*/true); |
| 7264 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7265 | } else |
| 7266 | Base = MemExpr->getBase()->Retain(); |
| 7267 | |
| 7268 | return MemberExpr::Create(Context, Base, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7269 | MemExpr->isArrow(), |
| 7270 | MemExpr->getQualifier(), |
| 7271 | MemExpr->getQualifierRange(), |
| 7272 | Fn, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7273 | Found, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7274 | MemExpr->getMemberLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 7275 | TemplateArgs, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7276 | Fn->getType()); |
| 7277 | } |
| 7278 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 7279 | assert(false && "Invalid reference to overloaded function"); |
| 7280 | return E->Retain(); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7281 | } |
| 7282 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7283 | Sema::OwningExprResult Sema::FixOverloadedFunctionReference(OwningExprResult E, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 7284 | DeclAccessPair Found, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7285 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7286 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7289 | } // end namespace clang |