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 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
| 16 | #include "clang/Sema/Initialization.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Template.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 2bbc026 | 2010-09-12 04:28:07 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
| 34 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 35 | using namespace sema; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 36 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 37 | /// A convenience routine for creating a decayed reference to a |
| 38 | /// function. |
| 39 | static Expr * |
| 40 | CreateFunctionRefExpr(Sema &S, FunctionDecl *Fn, |
| 41 | SourceLocation Loc = SourceLocation()) { |
| 42 | Expr *E = new (S.Context) DeclRefExpr(Fn, Fn->getType(), VK_LValue, Loc); |
| 43 | S.DefaultFunctionArrayConversion(E); |
| 44 | return E; |
| 45 | } |
| 46 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 47 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 48 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 49 | StandardConversionSequence &SCS, |
| 50 | bool CStyle); |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 51 | |
| 52 | static bool IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 53 | QualType &ToType, |
| 54 | bool InOverloadResolution, |
| 55 | StandardConversionSequence &SCS, |
| 56 | bool CStyle); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 57 | static OverloadingResult |
| 58 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 59 | UserDefinedConversionSequence& User, |
| 60 | OverloadCandidateSet& Conversions, |
| 61 | bool AllowExplicit); |
| 62 | |
| 63 | |
| 64 | static ImplicitConversionSequence::CompareKind |
| 65 | CompareStandardConversionSequences(Sema &S, |
| 66 | const StandardConversionSequence& SCS1, |
| 67 | const StandardConversionSequence& SCS2); |
| 68 | |
| 69 | static ImplicitConversionSequence::CompareKind |
| 70 | CompareQualificationConversions(Sema &S, |
| 71 | const StandardConversionSequence& SCS1, |
| 72 | const StandardConversionSequence& SCS2); |
| 73 | |
| 74 | static ImplicitConversionSequence::CompareKind |
| 75 | CompareDerivedToBaseConversions(Sema &S, |
| 76 | const StandardConversionSequence& SCS1, |
| 77 | const StandardConversionSequence& SCS2); |
| 78 | |
| 79 | |
| 80 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 81 | /// GetConversionCategory - Retrieve the implicit conversion |
| 82 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 84 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 85 | static const ImplicitConversionCategory |
| 86 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 87 | ICC_Identity, |
| 88 | ICC_Lvalue_Transformation, |
| 89 | ICC_Lvalue_Transformation, |
| 90 | ICC_Lvalue_Transformation, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 91 | ICC_Identity, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | ICC_Qualification_Adjustment, |
| 93 | ICC_Promotion, |
| 94 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 95 | ICC_Promotion, |
| 96 | ICC_Conversion, |
| 97 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | ICC_Conversion, |
| 99 | ICC_Conversion, |
| 100 | ICC_Conversion, |
| 101 | ICC_Conversion, |
| 102 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 103 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 104 | ICC_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 105 | ICC_Conversion, |
| 106 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 107 | ICC_Conversion |
| 108 | }; |
| 109 | return Category[(int)Kind]; |
| 110 | } |
| 111 | |
| 112 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 113 | /// corresponding to the given implicit conversion kind. |
| 114 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 115 | static const ImplicitConversionRank |
| 116 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 117 | ICR_Exact_Match, |
| 118 | ICR_Exact_Match, |
| 119 | ICR_Exact_Match, |
| 120 | ICR_Exact_Match, |
| 121 | ICR_Exact_Match, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 122 | ICR_Exact_Match, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 123 | ICR_Promotion, |
| 124 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 125 | ICR_Promotion, |
| 126 | ICR_Conversion, |
| 127 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 128 | ICR_Conversion, |
| 129 | ICR_Conversion, |
| 130 | ICR_Conversion, |
| 131 | ICR_Conversion, |
| 132 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 133 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 134 | ICR_Conversion, |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 135 | ICR_Conversion, |
| 136 | ICR_Conversion, |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 137 | ICR_Complex_Real_Conversion, |
| 138 | ICR_Conversion, |
| 139 | ICR_Conversion |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 140 | }; |
| 141 | return Rank[(int)Kind]; |
| 142 | } |
| 143 | |
| 144 | /// GetImplicitConversionName - Return the name of this kind of |
| 145 | /// implicit conversion. |
| 146 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
Nuno Lopes | cfca1f0 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 147 | static const char* const Name[(int)ICK_Num_Conversion_Kinds] = { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 148 | "No conversion", |
| 149 | "Lvalue-to-rvalue", |
| 150 | "Array-to-pointer", |
| 151 | "Function-to-pointer", |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 152 | "Noreturn adjustment", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | "Qualification", |
| 154 | "Integral promotion", |
| 155 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 156 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 157 | "Integral conversion", |
| 158 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 159 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 160 | "Floating-integral conversion", |
| 161 | "Pointer conversion", |
| 162 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 163 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 164 | "Compatible-types conversion", |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 165 | "Derived-to-base conversion", |
| 166 | "Vector conversion", |
| 167 | "Vector splat", |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 168 | "Complex-real conversion", |
| 169 | "Block Pointer conversion", |
| 170 | "Transparent Union Conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 171 | }; |
| 172 | return Name[Kind]; |
| 173 | } |
| 174 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 175 | /// StandardConversionSequence - Set the standard conversion |
| 176 | /// sequence to the identity conversion. |
| 177 | void StandardConversionSequence::setAsIdentityConversion() { |
| 178 | First = ICK_Identity; |
| 179 | Second = ICK_Identity; |
| 180 | Third = ICK_Identity; |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 181 | DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 182 | ReferenceBinding = false; |
| 183 | DirectBinding = false; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 184 | IsLvalueReference = true; |
| 185 | BindsToFunctionLvalue = false; |
| 186 | BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 187 | BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 188 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 191 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 192 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 193 | /// implicit conversions. |
| 194 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 195 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 196 | if (GetConversionRank(First) > Rank) |
| 197 | Rank = GetConversionRank(First); |
| 198 | if (GetConversionRank(Second) > Rank) |
| 199 | Rank = GetConversionRank(Second); |
| 200 | if (GetConversionRank(Third) > Rank) |
| 201 | Rank = GetConversionRank(Third); |
| 202 | return Rank; |
| 203 | } |
| 204 | |
| 205 | /// isPointerConversionToBool - Determines whether this conversion is |
| 206 | /// 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] | 207 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 208 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 210 | // Note that FromType has not necessarily been transformed by the |
| 211 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 212 | // check for their presence as well as checking whether FromType is |
| 213 | // a pointer. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 214 | if (getToType(1)->isBooleanType() && |
John McCall | 6d1116a | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 215 | (getFromType()->isPointerType() || |
| 216 | getFromType()->isObjCObjectPointerType() || |
| 217 | getFromType()->isBlockPointerType() || |
Anders Carlsson | 7da7cc5 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 218 | getFromType()->isNullPtrType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 219 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 220 | return true; |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 225 | /// isPointerConversionToVoidPointer - Determines whether this |
| 226 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 227 | /// used as part of the ranking of standard conversion sequences (C++ |
| 228 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 230 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 232 | QualType FromType = getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 233 | QualType ToType = getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 234 | |
| 235 | // Note that FromType has not necessarily been transformed by the |
| 236 | // array-to-pointer implicit conversion, so check for its presence |
| 237 | // and redo the conversion to get a pointer. |
| 238 | if (First == ICK_Array_To_Pointer) |
| 239 | FromType = Context.getArrayDecayedType(FromType); |
| 240 | |
John McCall | 75851b1 | 2010-10-26 06:40:27 +0000 | [diff] [blame] | 241 | if (Second == ICK_Pointer_Conversion && FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 242 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 243 | return ToPtrType->getPointeeType()->isVoidType(); |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 248 | /// DebugPrint - Print this standard conversion sequence to standard |
| 249 | /// error. Useful for debugging overloading issues. |
| 250 | void StandardConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 251 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 252 | bool PrintedSomething = false; |
| 253 | if (First != ICK_Identity) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 254 | OS << GetImplicitConversionName(First); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 255 | PrintedSomething = true; |
| 256 | } |
| 257 | |
| 258 | if (Second != ICK_Identity) { |
| 259 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 260 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 261 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 262 | OS << GetImplicitConversionName(Second); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 263 | |
| 264 | if (CopyConstructor) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 265 | OS << " (by copy constructor)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 266 | } else if (DirectBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 267 | OS << " (direct reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 268 | } else if (ReferenceBinding) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 269 | OS << " (reference binding)"; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 270 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 271 | PrintedSomething = true; |
| 272 | } |
| 273 | |
| 274 | if (Third != ICK_Identity) { |
| 275 | if (PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 276 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 277 | } |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 278 | OS << GetImplicitConversionName(Third); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 279 | PrintedSomething = true; |
| 280 | } |
| 281 | |
| 282 | if (!PrintedSomething) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 283 | OS << "No conversions required"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 288 | /// error. Useful for debugging overloading issues. |
| 289 | void UserDefinedConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 290 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 291 | if (Before.First || Before.Second || Before.Third) { |
| 292 | Before.DebugPrint(); |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 293 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 294 | } |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 295 | OS << '\'' << ConversionFunction << '\''; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 296 | if (After.First || After.Second || After.Third) { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 297 | OS << " -> "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 298 | After.DebugPrint(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 303 | /// error. Useful for debugging overloading issues. |
| 304 | void ImplicitConversionSequence::DebugPrint() const { |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 305 | llvm::raw_ostream &OS = llvm::errs(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 306 | switch (ConversionKind) { |
| 307 | case StandardConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 308 | OS << "Standard conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 309 | Standard.DebugPrint(); |
| 310 | break; |
| 311 | case UserDefinedConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 312 | OS << "User-defined conversion: "; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 313 | UserDefined.DebugPrint(); |
| 314 | break; |
| 315 | case EllipsisConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 316 | OS << "Ellipsis conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 317 | break; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 318 | case AmbiguousConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 319 | OS << "Ambiguous conversion"; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 320 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 321 | case BadConversion: |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 322 | OS << "Bad conversion"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | |
Daniel Dunbar | 42e3df0 | 2010-01-22 02:04:41 +0000 | [diff] [blame] | 326 | OS << "\n"; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 327 | } |
| 328 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 329 | void AmbiguousConversionSequence::construct() { |
| 330 | new (&conversions()) ConversionSet(); |
| 331 | } |
| 332 | |
| 333 | void AmbiguousConversionSequence::destruct() { |
| 334 | conversions().~ConversionSet(); |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | AmbiguousConversionSequence::copyFrom(const AmbiguousConversionSequence &O) { |
| 339 | FromTypePtr = O.FromTypePtr; |
| 340 | ToTypePtr = O.ToTypePtr; |
| 341 | new (&conversions()) ConversionSet(O.conversions()); |
| 342 | } |
| 343 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 344 | namespace { |
| 345 | // Structure used by OverloadCandidate::DeductionFailureInfo to store |
| 346 | // template parameter and template argument information. |
| 347 | struct DFIParamWithArguments { |
| 348 | TemplateParameter Param; |
| 349 | TemplateArgument FirstArg; |
| 350 | TemplateArgument SecondArg; |
| 351 | }; |
| 352 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 353 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 354 | /// \brief Convert from Sema's representation of template deduction information |
| 355 | /// to the form used in overload-candidate information. |
| 356 | OverloadCandidate::DeductionFailureInfo |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 357 | static MakeDeductionFailureInfo(ASTContext &Context, |
| 358 | Sema::TemplateDeductionResult TDK, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 359 | TemplateDeductionInfo &Info) { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 360 | OverloadCandidate::DeductionFailureInfo Result; |
| 361 | Result.Result = static_cast<unsigned>(TDK); |
| 362 | Result.Data = 0; |
| 363 | switch (TDK) { |
| 364 | case Sema::TDK_Success: |
| 365 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 366 | case Sema::TDK_TooManyArguments: |
| 367 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 368 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 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 | Result.Data = Info.Param.getOpaqueValue(); |
| 373 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 375 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 376 | case Sema::TDK_Underqualified: { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 377 | // FIXME: Should allocate from normal heap so that we can free this later. |
| 378 | DFIParamWithArguments *Saved = new (Context) DFIParamWithArguments; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 379 | Saved->Param = Info.Param; |
| 380 | Saved->FirstArg = Info.FirstArg; |
| 381 | Saved->SecondArg = Info.SecondArg; |
| 382 | Result.Data = Saved; |
| 383 | break; |
| 384 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 385 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 386 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 387 | Result.Data = Info.take(); |
| 388 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 390 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 391 | case Sema::TDK_FailedOverloadResolution: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 392 | break; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 393 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 395 | return Result; |
| 396 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 398 | void OverloadCandidate::DeductionFailureInfo::Destroy() { |
| 399 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 400 | case Sema::TDK_Success: |
| 401 | case Sema::TDK_InstantiationDepth: |
| 402 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 403 | case Sema::TDK_TooManyArguments: |
| 404 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 405 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 406 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 408 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 409 | case Sema::TDK_Underqualified: |
Douglas Gregor | b02d6b3 | 2010-05-08 20:20:05 +0000 | [diff] [blame] | 410 | // FIXME: Destroy the data? |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 411 | Data = 0; |
| 412 | break; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 413 | |
| 414 | case Sema::TDK_SubstitutionFailure: |
| 415 | // FIXME: Destroy the template arugment list? |
| 416 | Data = 0; |
| 417 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 419 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 420 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 421 | case Sema::TDK_FailedOverloadResolution: |
| 422 | break; |
| 423 | } |
| 424 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 425 | |
| 426 | TemplateParameter |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 427 | OverloadCandidate::DeductionFailureInfo::getTemplateParameter() { |
| 428 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 429 | case Sema::TDK_Success: |
| 430 | case Sema::TDK_InstantiationDepth: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 431 | case Sema::TDK_TooManyArguments: |
| 432 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 433 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 434 | return TemplateParameter(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 436 | case Sema::TDK_Incomplete: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 437 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 438 | return TemplateParameter::getFromOpaqueValue(Data); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 439 | |
| 440 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 441 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 442 | return static_cast<DFIParamWithArguments*>(Data)->Param; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 444 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 445 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 446 | case Sema::TDK_FailedOverloadResolution: |
| 447 | break; |
| 448 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 450 | return TemplateParameter(); |
| 451 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 453 | TemplateArgumentList * |
| 454 | OverloadCandidate::DeductionFailureInfo::getTemplateArgumentList() { |
| 455 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 456 | case Sema::TDK_Success: |
| 457 | case Sema::TDK_InstantiationDepth: |
| 458 | case Sema::TDK_TooManyArguments: |
| 459 | case Sema::TDK_TooFewArguments: |
| 460 | case Sema::TDK_Incomplete: |
| 461 | case Sema::TDK_InvalidExplicitArguments: |
| 462 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 463 | case Sema::TDK_Underqualified: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 464 | return 0; |
| 465 | |
| 466 | case Sema::TDK_SubstitutionFailure: |
| 467 | return static_cast<TemplateArgumentList*>(Data); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 469 | // Unhandled |
| 470 | case Sema::TDK_NonDeducedMismatch: |
| 471 | case Sema::TDK_FailedOverloadResolution: |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 478 | const TemplateArgument *OverloadCandidate::DeductionFailureInfo::getFirstArg() { |
| 479 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 480 | case Sema::TDK_Success: |
| 481 | case Sema::TDK_InstantiationDepth: |
| 482 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 483 | case Sema::TDK_TooManyArguments: |
| 484 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 485 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 486 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 487 | return 0; |
| 488 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 489 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 490 | case Sema::TDK_Underqualified: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | return &static_cast<DFIParamWithArguments*>(Data)->FirstArg; |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 493 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 494 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 495 | case Sema::TDK_FailedOverloadResolution: |
| 496 | break; |
| 497 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 499 | return 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | } |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 501 | |
| 502 | const TemplateArgument * |
| 503 | OverloadCandidate::DeductionFailureInfo::getSecondArg() { |
| 504 | switch (static_cast<Sema::TemplateDeductionResult>(Result)) { |
| 505 | case Sema::TDK_Success: |
| 506 | case Sema::TDK_InstantiationDepth: |
| 507 | case Sema::TDK_Incomplete: |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 508 | case Sema::TDK_TooManyArguments: |
| 509 | case Sema::TDK_TooFewArguments: |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 510 | case Sema::TDK_InvalidExplicitArguments: |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 511 | case Sema::TDK_SubstitutionFailure: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 514 | case Sema::TDK_Inconsistent: |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 515 | case Sema::TDK_Underqualified: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 516 | return &static_cast<DFIParamWithArguments*>(Data)->SecondArg; |
| 517 | |
Douglas Gregor | 461761d | 2010-05-08 18:20:53 +0000 | [diff] [blame] | 518 | // Unhandled |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 519 | case Sema::TDK_NonDeducedMismatch: |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 520 | case Sema::TDK_FailedOverloadResolution: |
| 521 | break; |
| 522 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | void OverloadCandidateSet::clear() { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 528 | inherited::clear(); |
| 529 | Functions.clear(); |
| 530 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 531 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | // IsOverload - Determine whether the given New declaration is an |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 533 | // overload of the declarations in Old. This routine returns false if |
| 534 | // New and Old cannot be overloaded, e.g., if New has the same |
| 535 | // signature as some function in Old (C++ 1.3.10) or if the Old |
| 536 | // declarations aren't functions (or function templates) at all. When |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 537 | // it does return false, MatchedDecl will point to the decl that New |
| 538 | // cannot be overloaded with. This decl may be a UsingShadowDecl on |
| 539 | // top of the underlying declaration. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 540 | // |
| 541 | // Example: Given the following input: |
| 542 | // |
| 543 | // void f(int, float); // #1 |
| 544 | // void f(int, int); // #2 |
| 545 | // int f(int, int); // #3 |
| 546 | // |
| 547 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 549 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 550 | // When we process #2, Old contains only the FunctionDecl for #1. By |
| 551 | // comparing the parameter types, we see that #1 and #2 are overloaded |
| 552 | // (since they have different signatures), so this routine returns |
| 553 | // false; MatchedDecl is unchanged. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 554 | // |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 555 | // When we process #3, Old is an overload set containing #1 and #2. We |
| 556 | // compare the signatures of #3 to #1 (they're overloaded, so we do |
| 557 | // nothing) and then #3 to #2. Since the signatures of #3 and #2 are |
| 558 | // identical (return types of functions are not part of the |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 559 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 560 | // point to the FunctionDecl for #2. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 561 | // |
| 562 | // 'NewIsUsingShadowDecl' indicates that 'New' is being introduced |
| 563 | // into a class by a using declaration. The rules for whether to hide |
| 564 | // shadow declarations ignore some properties which otherwise figure |
| 565 | // into a function template's signature. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 566 | Sema::OverloadKind |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 567 | Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, |
| 568 | NamedDecl *&Match, bool NewIsUsingDecl) { |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 569 | for (LookupResult::iterator I = Old.begin(), E = Old.end(); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 570 | I != E; ++I) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 571 | NamedDecl *OldD = *I; |
| 572 | |
| 573 | bool OldIsUsingDecl = false; |
| 574 | if (isa<UsingShadowDecl>(OldD)) { |
| 575 | OldIsUsingDecl = true; |
| 576 | |
| 577 | // We can always introduce two using declarations into the same |
| 578 | // context, even if they have identical signatures. |
| 579 | if (NewIsUsingDecl) continue; |
| 580 | |
| 581 | OldD = cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 582 | } |
| 583 | |
| 584 | // If either declaration was introduced by a using declaration, |
| 585 | // we'll need to use slightly different rules for matching. |
| 586 | // Essentially, these rules are the normal rules, except that |
| 587 | // function templates hide function templates with different |
| 588 | // return types or template parameter lists. |
| 589 | bool UseMemberUsingDeclRules = |
| 590 | (OldIsUsingDecl || NewIsUsingDecl) && CurContext->isRecord(); |
| 591 | |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 592 | if (FunctionTemplateDecl *OldT = dyn_cast<FunctionTemplateDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 593 | if (!IsOverload(New, OldT->getTemplatedDecl(), UseMemberUsingDeclRules)) { |
| 594 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 595 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 596 | continue; |
| 597 | } |
| 598 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 599 | Match = *I; |
| 600 | return Ovl_Match; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 601 | } |
John McCall | 3d988d9 | 2009-12-02 08:47:38 +0000 | [diff] [blame] | 602 | } else if (FunctionDecl *OldF = dyn_cast<FunctionDecl>(OldD)) { |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 603 | if (!IsOverload(New, OldF, UseMemberUsingDeclRules)) { |
| 604 | if (UseMemberUsingDeclRules && OldIsUsingDecl) { |
| 605 | HideUsingShadowDecl(S, cast<UsingShadowDecl>(*I)); |
| 606 | continue; |
| 607 | } |
| 608 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 609 | Match = *I; |
| 610 | return Ovl_Match; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 611 | } |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 612 | } else if (isa<UsingDecl>(OldD)) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 613 | // We can overload with these, which can show up when doing |
| 614 | // redeclaration checks for UsingDecls. |
| 615 | assert(Old.getLookupKind() == LookupUsingDeclName); |
John McCall | a8987a294 | 2010-11-10 03:01:53 +0000 | [diff] [blame] | 616 | } else if (isa<TagDecl>(OldD)) { |
| 617 | // We can always overload with tags by hiding them. |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 618 | } else if (isa<UnresolvedUsingValueDecl>(OldD)) { |
| 619 | // Optimistically assume that an unresolved using decl will |
| 620 | // overload; if it doesn't, we'll have to diagnose during |
| 621 | // template instantiation. |
| 622 | } else { |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 623 | // (C++ 13p1): |
| 624 | // Only function declarations can be overloaded; object and type |
| 625 | // declarations cannot be overloaded. |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 626 | Match = *I; |
| 627 | return Ovl_NonFunction; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 628 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 629 | } |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 630 | |
John McCall | daa3d6b | 2009-12-09 03:35:25 +0000 | [diff] [blame] | 631 | return Ovl_Overload; |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 632 | } |
| 633 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 634 | bool Sema::IsOverload(FunctionDecl *New, FunctionDecl *Old, |
| 635 | bool UseUsingDeclRules) { |
John McCall | 8246e35 | 2010-08-12 07:09:11 +0000 | [diff] [blame] | 636 | // If both of the functions are extern "C", then they are not |
| 637 | // overloads. |
| 638 | if (Old->isExternC() && New->isExternC()) |
| 639 | return false; |
| 640 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 641 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 642 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 643 | |
| 644 | // C++ [temp.fct]p2: |
| 645 | // A function template can be overloaded with other function templates |
| 646 | // and with normal (non-template) functions. |
| 647 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 648 | return true; |
| 649 | |
| 650 | // Is the function New an overload of the function Old? |
| 651 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 652 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 653 | |
| 654 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 655 | // determine whether they are overloads. If we find any mismatch |
| 656 | // in the signature, they are overloads. |
| 657 | |
| 658 | // If either of these functions is a K&R-style function (no |
| 659 | // prototype), then we consider them to have matching signatures. |
| 660 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 661 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 662 | return false; |
| 663 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 664 | const FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 665 | const FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 666 | |
| 667 | // The signature of a function includes the types of its |
| 668 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 669 | // of the ellipsis; see C++ DR 357). |
| 670 | if (OldQType != NewQType && |
| 671 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 672 | OldType->isVariadic() != NewType->isVariadic() || |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 673 | !FunctionArgTypesAreEqual(OldType, NewType))) |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 674 | return true; |
| 675 | |
| 676 | // C++ [temp.over.link]p4: |
| 677 | // The signature of a function template consists of its function |
| 678 | // signature, its return type and its template parameter list. The names |
| 679 | // of the template parameters are significant only for establishing the |
| 680 | // relationship between the template parameters and the rest of the |
| 681 | // signature. |
| 682 | // |
| 683 | // We check the return type and template parameter lists for function |
| 684 | // templates first; the remaining checks follow. |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 685 | // |
| 686 | // However, we don't consider either of these when deciding whether |
| 687 | // a member introduced by a shadow declaration is hidden. |
| 688 | if (!UseUsingDeclRules && NewTemplate && |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 689 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 690 | OldTemplate->getTemplateParameters(), |
| 691 | false, TPL_TemplateMatch) || |
| 692 | OldType->getResultType() != NewType->getResultType())) |
| 693 | return true; |
| 694 | |
| 695 | // If the function is a class member, its signature includes the |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 696 | // cv-qualifiers (if any) and ref-qualifier (if any) on the function itself. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 697 | // |
| 698 | // As part of this, also check whether one of the member functions |
| 699 | // is static, in which case they are not overloads (C++ |
| 700 | // 13.1p2). While not part of the definition of the signature, |
| 701 | // this check is important to determine whether these functions |
| 702 | // can be overloaded. |
| 703 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 704 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 705 | if (OldMethod && NewMethod && |
| 706 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | b2f8aa9 | 2011-01-26 17:47:49 +0000 | [diff] [blame] | 707 | (OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers() || |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 708 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier())) { |
| 709 | if (!UseUsingDeclRules && |
| 710 | OldMethod->getRefQualifier() != NewMethod->getRefQualifier() && |
| 711 | (OldMethod->getRefQualifier() == RQ_None || |
| 712 | NewMethod->getRefQualifier() == RQ_None)) { |
| 713 | // C++0x [over.load]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | // - Member function declarations with the same name and the same |
| 715 | // parameter-type-list as well as member function template |
| 716 | // declarations with the same name, the same parameter-type-list, and |
| 717 | // the same template parameter lists cannot be overloaded if any of |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 718 | // them, but not all, have a ref-qualifier (8.3.5). |
| 719 | Diag(NewMethod->getLocation(), diag::err_ref_qualifier_overload) |
| 720 | << NewMethod->getRefQualifier() << OldMethod->getRefQualifier(); |
| 721 | Diag(OldMethod->getLocation(), diag::note_previous_declaration); |
| 722 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 724 | return true; |
Douglas Gregor | c83f9865 | 2011-01-26 21:20:37 +0000 | [diff] [blame] | 725 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 726 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 727 | // The signatures match; this is not an overload. |
| 728 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 731 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 732 | /// from the given expression (Expr) to the given type (ToType). This |
| 733 | /// function returns an implicit conversion sequence that can be used |
| 734 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 735 | /// |
| 736 | /// void f(float f); |
| 737 | /// void g(int i) { f(i); } |
| 738 | /// |
| 739 | /// this routine would produce an implicit conversion sequence to |
| 740 | /// describe the initialization of f from i, which will be a standard |
| 741 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 742 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 743 | // |
| 744 | /// Note that this routine only determines how the conversion can be |
| 745 | /// performed; it does not actually perform the conversion. As such, |
| 746 | /// it will not produce any diagnostics if no conversion is available, |
| 747 | /// but will instead return an implicit conversion sequence of kind |
| 748 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 749 | /// |
| 750 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 751 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 752 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 753 | /// permitted. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 754 | static ImplicitConversionSequence |
| 755 | TryImplicitConversion(Sema &S, Expr *From, QualType ToType, |
| 756 | bool SuppressUserConversions, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | bool AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 758 | bool InOverloadResolution, |
| 759 | bool CStyle) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 760 | ImplicitConversionSequence ICS; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 761 | if (IsStandardConversion(S, From, ToType, InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 762 | ICS.Standard, CStyle)) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 763 | ICS.setStandard(); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 764 | return ICS; |
| 765 | } |
| 766 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 767 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 768 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 769 | return ICS; |
| 770 | } |
| 771 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 772 | // C++ [over.ics.user]p4: |
| 773 | // A conversion of an expression of class type to the same class |
| 774 | // type is given Exact Match rank, and a conversion of an |
| 775 | // expression of class type to a base class of that type is |
| 776 | // given Conversion rank, in spite of the fact that a copy/move |
| 777 | // constructor (i.e., a user-defined conversion function) is |
| 778 | // called for those cases. |
| 779 | QualType FromType = From->getType(); |
| 780 | if (ToType->getAs<RecordType>() && FromType->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 781 | (S.Context.hasSameUnqualifiedType(FromType, ToType) || |
| 782 | S.IsDerivedFrom(FromType, ToType))) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 783 | ICS.setStandard(); |
| 784 | ICS.Standard.setAsIdentityConversion(); |
| 785 | ICS.Standard.setFromType(FromType); |
| 786 | ICS.Standard.setAllToTypes(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 788 | // We don't actually check at this point whether there is a valid |
| 789 | // copy/move constructor, since overloading just assumes that it |
| 790 | // exists. When we actually perform initialization, we'll find the |
| 791 | // appropriate constructor to copy the returned object, if needed. |
| 792 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 794 | // Determine whether this is considered a derived-to-base conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 795 | if (!S.Context.hasSameUnqualifiedType(FromType, ToType)) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 796 | ICS.Standard.Second = ICK_Derived_To_Base; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 798 | return ICS; |
| 799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 801 | if (SuppressUserConversions) { |
| 802 | // We're not in the case above, so there is no conversion that |
| 803 | // we can perform. |
| 804 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 805 | return ICS; |
| 806 | } |
| 807 | |
| 808 | // Attempt user-defined conversion. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 809 | OverloadCandidateSet Conversions(From->getExprLoc()); |
| 810 | OverloadingResult UserDefResult |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 811 | = IsUserDefinedConversion(S, From, ToType, ICS.UserDefined, Conversions, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 812 | AllowExplicit); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 813 | |
| 814 | if (UserDefResult == OR_Success) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 815 | ICS.setUserDefined(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 816 | // C++ [over.ics.user]p4: |
| 817 | // A conversion of an expression of class type to the same class |
| 818 | // type is given Exact Match rank, and a conversion of an |
| 819 | // expression of class type to a base class of that type is |
| 820 | // given Conversion rank, in spite of the fact that a copy |
| 821 | // constructor (i.e., a user-defined conversion function) is |
| 822 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 824 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | QualType FromCanon |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 826 | = S.Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 827 | QualType ToCanon |
| 828 | = S.Context.getCanonicalType(ToType).getUnqualifiedType(); |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 829 | if (Constructor->isCopyConstructor() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 830 | (FromCanon == ToCanon || S.IsDerivedFrom(FromCanon, ToCanon))) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 831 | // Turn this into a "standard" conversion sequence, so that it |
| 832 | // gets ranked with standard conversion sequences. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 833 | ICS.setStandard(); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 834 | ICS.Standard.setAsIdentityConversion(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 835 | ICS.Standard.setFromType(From->getType()); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 836 | ICS.Standard.setAllToTypes(ToType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 837 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 838 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 839 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 840 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 841 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 842 | |
| 843 | // C++ [over.best.ics]p4: |
| 844 | // However, when considering the argument of a user-defined |
| 845 | // conversion function that is a candidate by 13.3.1.3 when |
| 846 | // invoked for the copying of the temporary in the second step |
| 847 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 848 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 849 | // ellipsis conversion sequences are allowed. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 850 | if (SuppressUserConversions && ICS.isUserDefined()) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 851 | ICS.setBad(BadConversionSequence::suppressed_user, From, ToType); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 852 | } |
John McCall | e8c8cd2 | 2010-01-13 22:30:33 +0000 | [diff] [blame] | 853 | } else if (UserDefResult == OR_Ambiguous && !SuppressUserConversions) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 854 | ICS.setAmbiguous(); |
| 855 | ICS.Ambiguous.setFromType(From->getType()); |
| 856 | ICS.Ambiguous.setToType(ToType); |
| 857 | for (OverloadCandidateSet::iterator Cand = Conversions.begin(); |
| 858 | Cand != Conversions.end(); ++Cand) |
| 859 | if (Cand->Viable) |
| 860 | ICS.Ambiguous.addConversion(Cand->Function); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 861 | } else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 862 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); |
Fariborz Jahanian | 21ccf06 | 2009-09-23 00:58:07 +0000 | [diff] [blame] | 863 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 864 | |
| 865 | return ICS; |
| 866 | } |
| 867 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 868 | bool Sema::TryImplicitConversion(InitializationSequence &Sequence, |
| 869 | const InitializedEntity &Entity, |
| 870 | Expr *Initializer, |
| 871 | bool SuppressUserConversions, |
| 872 | bool AllowExplicitConversions, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 873 | bool InOverloadResolution, |
| 874 | bool CStyle) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 875 | ImplicitConversionSequence ICS |
| 876 | = clang::TryImplicitConversion(*this, Initializer, Entity.getType(), |
| 877 | SuppressUserConversions, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 878 | AllowExplicitConversions, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 879 | InOverloadResolution, |
| 880 | CStyle); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 881 | if (ICS.isBad()) return true; |
| 882 | |
| 883 | // Perform the actual conversion. |
| 884 | Sequence.AddConversionSequenceStep(ICS, Entity.getType()); |
| 885 | return false; |
| 886 | } |
| 887 | |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 888 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 889 | /// expression From to the type ToType. Returns true if there was an |
| 890 | /// error, false otherwise. The expression From is replaced with the |
| 891 | /// converted expression. Flavor is the kind of conversion we're |
| 892 | /// performing, used in the error message. If @p AllowExplicit, |
| 893 | /// explicit user-defined conversions are permitted. |
| 894 | bool |
| 895 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 896 | AssignmentAction Action, bool AllowExplicit) { |
| 897 | ImplicitConversionSequence ICS; |
| 898 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, ICS); |
| 899 | } |
| 900 | |
| 901 | bool |
| 902 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 903 | AssignmentAction Action, bool AllowExplicit, |
| 904 | ImplicitConversionSequence& ICS) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 905 | ICS = clang::TryImplicitConversion(*this, From, ToType, |
| 906 | /*SuppressUserConversions=*/false, |
| 907 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 908 | /*InOverloadResolution=*/false, |
| 909 | /*CStyle=*/false); |
Douglas Gregor | ae4b5df | 2010-04-16 22:27:05 +0000 | [diff] [blame] | 910 | return PerformImplicitConversion(From, ToType, ICS, Action); |
| 911 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 912 | |
| 913 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 914 | /// conversion that strips "noreturn" off the nested function type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 915 | static bool IsNoReturnConversion(ASTContext &Context, QualType FromType, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 916 | QualType ToType, QualType &ResultTy) { |
| 917 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 918 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 919 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 920 | // Permit the conversion F(t __attribute__((noreturn))) -> F(t) |
| 921 | // where F adds one of the following at most once: |
| 922 | // - a pointer |
| 923 | // - a member pointer |
| 924 | // - a block pointer |
| 925 | CanQualType CanTo = Context.getCanonicalType(ToType); |
| 926 | CanQualType CanFrom = Context.getCanonicalType(FromType); |
| 927 | Type::TypeClass TyClass = CanTo->getTypeClass(); |
| 928 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 929 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) { |
| 930 | if (TyClass == Type::Pointer) { |
| 931 | CanTo = CanTo.getAs<PointerType>()->getPointeeType(); |
| 932 | CanFrom = CanFrom.getAs<PointerType>()->getPointeeType(); |
| 933 | } else if (TyClass == Type::BlockPointer) { |
| 934 | CanTo = CanTo.getAs<BlockPointerType>()->getPointeeType(); |
| 935 | CanFrom = CanFrom.getAs<BlockPointerType>()->getPointeeType(); |
| 936 | } else if (TyClass == Type::MemberPointer) { |
| 937 | CanTo = CanTo.getAs<MemberPointerType>()->getPointeeType(); |
| 938 | CanFrom = CanFrom.getAs<MemberPointerType>()->getPointeeType(); |
| 939 | } else { |
| 940 | return false; |
| 941 | } |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 942 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 943 | TyClass = CanTo->getTypeClass(); |
| 944 | if (TyClass != CanFrom->getTypeClass()) return false; |
| 945 | if (TyClass != Type::FunctionProto && TyClass != Type::FunctionNoProto) |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | const FunctionType *FromFn = cast<FunctionType>(CanFrom); |
| 950 | FunctionType::ExtInfo EInfo = FromFn->getExtInfo(); |
| 951 | if (!EInfo.getNoReturn()) return false; |
| 952 | |
| 953 | FromFn = Context.adjustFunctionType(FromFn, EInfo.withNoReturn(false)); |
| 954 | assert(QualType(FromFn, 0).isCanonical()); |
| 955 | if (QualType(FromFn, 0) != CanTo) return false; |
| 956 | |
| 957 | ResultTy = ToType; |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 958 | return true; |
| 959 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 961 | /// \brief Determine whether the conversion from FromType to ToType is a valid |
| 962 | /// vector conversion. |
| 963 | /// |
| 964 | /// \param ICK Will be set to the vector conversion kind, if this is a vector |
| 965 | /// conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 966 | static bool IsVectorConversion(ASTContext &Context, QualType FromType, |
| 967 | QualType ToType, ImplicitConversionKind &ICK) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 968 | // We need at least one of these types to be a vector type to have a vector |
| 969 | // conversion. |
| 970 | if (!ToType->isVectorType() && !FromType->isVectorType()) |
| 971 | return false; |
| 972 | |
| 973 | // Identical types require no conversions. |
| 974 | if (Context.hasSameUnqualifiedType(FromType, ToType)) |
| 975 | return false; |
| 976 | |
| 977 | // There are no conversions between extended vector types, only identity. |
| 978 | if (ToType->isExtVectorType()) { |
| 979 | // There are no conversions between extended vector types other than the |
| 980 | // identity conversion. |
| 981 | if (FromType->isExtVectorType()) |
| 982 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 984 | // Vector splat from any arithmetic type to a vector. |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 985 | if (FromType->isArithmeticType()) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 986 | ICK = ICK_Vector_Splat; |
| 987 | return true; |
| 988 | } |
| 989 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 990 | |
| 991 | // We can perform the conversion between vector types in the following cases: |
| 992 | // 1)vector types are equivalent AltiVec and GCC vector types |
| 993 | // 2)lax vector conversions are permitted and the vector types are of the |
| 994 | // same size |
| 995 | if (ToType->isVectorType() && FromType->isVectorType()) { |
| 996 | if (Context.areCompatibleVectorTypes(FromType, ToType) || |
Chandler Carruth | 9c524c1 | 2010-08-08 05:02:51 +0000 | [diff] [blame] | 997 | (Context.getLangOptions().LaxVectorConversions && |
| 998 | (Context.getTypeSize(FromType) == Context.getTypeSize(ToType)))) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 999 | ICK = ICK_Vector_Conversion; |
| 1000 | return true; |
| 1001 | } |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1002 | } |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1004 | return false; |
| 1005 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1007 | /// IsStandardConversion - Determines whether there is a standard |
| 1008 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 1009 | /// expression From to the type ToType. Standard conversion sequences |
| 1010 | /// only consider non-class types; for conversions that involve class |
| 1011 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 1012 | /// contain the standard conversion sequence required to perform this |
| 1013 | /// conversion and this routine will return true. Otherwise, this |
| 1014 | /// routine will return false and the value of SCS is unspecified. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1015 | static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, |
| 1016 | bool InOverloadResolution, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1017 | StandardConversionSequence &SCS, |
| 1018 | bool CStyle) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1019 | QualType FromType = From->getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1020 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1021 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1022 | SCS.setAsIdentityConversion(); |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1023 | SCS.DeprecatedStringLiteralToCharPtr = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1024 | SCS.IncompatibleObjC = false; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1025 | SCS.setFromType(FromType); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1026 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1028 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1030 | if (FromType->isRecordType() || ToType->isRecordType()) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1031 | if (S.getLangOptions().CPlusPlus) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1032 | return false; |
| 1033 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1037 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 1038 | // array-to-pointer conversion, or function-to-pointer conversion |
| 1039 | // (C++ 4p1). |
| 1040 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1041 | if (FromType == S.Context.OverloadTy) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1042 | DeclAccessPair AccessPair; |
| 1043 | if (FunctionDecl *Fn |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | = S.ResolveAddressOfOverloadedFunction(From, ToType, false, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1045 | AccessPair)) { |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1046 | // We were able to resolve the address of the overloaded function, |
| 1047 | // so we can convert to the type of that function. |
| 1048 | FromType = Fn->getType(); |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1049 | |
| 1050 | // we can sometimes resolve &foo<int> regardless of ToType, so check |
| 1051 | // if the type matches (identity) or we are converting to bool |
| 1052 | if (!S.Context.hasSameUnqualifiedType( |
| 1053 | S.ExtractUnqualifiedFunctionType(ToType), FromType)) { |
| 1054 | QualType resultTy; |
| 1055 | // if the function type matches except for [[noreturn]], it's ok |
| 1056 | if (!IsNoReturnConversion(S.Context, FromType, |
| 1057 | S.ExtractUnqualifiedFunctionType(ToType), resultTy)) |
| 1058 | // otherwise, only a boolean conversion is standard |
| 1059 | if (!ToType->isBooleanType()) |
| 1060 | return false; |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1061 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1062 | |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1063 | // Check if the "from" expression is taking the address of an overloaded |
| 1064 | // function and recompute the FromType accordingly. Take advantage of the |
| 1065 | // fact that non-static member functions *must* have such an address-of |
| 1066 | // expression. |
| 1067 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn); |
| 1068 | if (Method && !Method->isStatic()) { |
| 1069 | assert(isa<UnaryOperator>(From->IgnoreParens()) && |
| 1070 | "Non-unary operator on non-static member address"); |
| 1071 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() |
| 1072 | == UO_AddrOf && |
| 1073 | "Non-address-of operator on non-static member address"); |
| 1074 | const Type *ClassType |
| 1075 | = S.Context.getTypeDeclType(Method->getParent()).getTypePtr(); |
| 1076 | FromType = S.Context.getMemberPointerType(FromType, ClassType); |
Chandler Carruth | 7750f76 | 2011-03-29 18:38:10 +0000 | [diff] [blame] | 1077 | } else if (isa<UnaryOperator>(From->IgnoreParens())) { |
| 1078 | assert(cast<UnaryOperator>(From->IgnoreParens())->getOpcode() == |
| 1079 | UO_AddrOf && |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1080 | "Non-address-of operator for overloaded function expression"); |
| 1081 | FromType = S.Context.getPointerType(FromType); |
| 1082 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1084 | // Check that we've computed the proper type after overload resolution. |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 1085 | assert(S.Context.hasSameType( |
| 1086 | FromType, |
| 1087 | S.FixOverloadedFunctionReference(From, AccessPair, Fn)->getType())); |
Douglas Gregor | 980fb16 | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1088 | } else { |
| 1089 | return false; |
| 1090 | } |
Anders Carlsson | ba37e1e | 2010-11-04 05:28:09 +0000 | [diff] [blame] | 1091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1093 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 1094 | // converted to an rvalue. |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1095 | bool argIsLValue = From->isLValue(); |
| 1096 | if (argIsLValue && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1097 | !FromType->isFunctionType() && !FromType->isArrayType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1098 | S.Context.getCanonicalType(FromType) != S.Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1099 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1100 | |
| 1101 | // If T is a non-class type, the type of the rvalue is the |
| 1102 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1103 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 1104 | // just strip the qualifiers because they don't matter. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1105 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1106 | } else if (FromType->isArrayType()) { |
| 1107 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1108 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1109 | |
| 1110 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 1111 | // bound of T" can be converted to an rvalue of type "pointer to |
| 1112 | // T" (C++ 4.2p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1113 | FromType = S.Context.getArrayDecayedType(FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1114 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1115 | if (S.IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1116 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 1117 | SCS.DeprecatedStringLiteralToCharPtr = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1118 | |
| 1119 | // For the purpose of ranking in overload resolution |
| 1120 | // (13.3.3.1.1), this conversion is considered an |
| 1121 | // array-to-pointer conversion followed by a qualification |
| 1122 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1123 | SCS.Second = ICK_Identity; |
| 1124 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1125 | SCS.setAllToTypes(FromType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1126 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1127 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1128 | } else if (FromType->isFunctionType() && argIsLValue) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1129 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1130 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1131 | |
| 1132 | // An lvalue of function type T can be converted to an rvalue of |
| 1133 | // type "pointer to T." The result is a pointer to the |
| 1134 | // function. (C++ 4.3p1). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1135 | FromType = S.Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1136 | } else { |
| 1137 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1138 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1139 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1140 | SCS.setToType(0, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1141 | |
| 1142 | // The second conversion can be an integral promotion, floating |
| 1143 | // point promotion, integral conversion, floating point conversion, |
| 1144 | // floating-integral conversion, pointer conversion, |
| 1145 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1146 | // For overloading in C, this can also be a "compatible-type" |
| 1147 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1148 | bool IncompatibleObjC = false; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1149 | ImplicitConversionKind SecondICK = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1150 | if (S.Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1151 | // The unqualified versions of the types are the same: there's no |
| 1152 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1153 | SCS.Second = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1154 | } else if (S.IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1156 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1157 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1158 | } else if (S.IsFloatingPointPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1159 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1160 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1161 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1162 | } else if (S.IsComplexPromotion(FromType, ToType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1163 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1164 | SCS.Second = ICK_Complex_Promotion; |
| 1165 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1166 | } else if (ToType->isBooleanType() && |
| 1167 | (FromType->isArithmeticType() || |
| 1168 | FromType->isAnyPointerType() || |
| 1169 | FromType->isBlockPointerType() || |
| 1170 | FromType->isMemberPointerType() || |
| 1171 | FromType->isNullPtrType())) { |
| 1172 | // Boolean conversions (C++ 4.12). |
| 1173 | SCS.Second = ICK_Boolean_Conversion; |
| 1174 | FromType = S.Context.BoolTy; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1175 | } else if (FromType->isIntegralOrUnscopedEnumerationType() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1176 | ToType->isIntegralType(S.Context)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1177 | // Integral conversions (C++ 4.7). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1178 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1179 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1180 | } else if (FromType->isAnyComplexType() && ToType->isComplexType()) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1181 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1182 | SCS.Second = ICK_Complex_Conversion; |
| 1183 | FromType = ToType.getUnqualifiedType(); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1184 | } else if ((FromType->isAnyComplexType() && ToType->isArithmeticType()) || |
| 1185 | (ToType->isAnyComplexType() && FromType->isArithmeticType())) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1186 | // Complex-real conversions (C99 6.3.1.7) |
| 1187 | SCS.Second = ICK_Complex_Real; |
| 1188 | FromType = ToType.getUnqualifiedType(); |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1189 | } else if (FromType->isRealFloatingType() && ToType->isRealFloatingType()) { |
Chandler Carruth | 8fa1e7e | 2010-02-25 07:20:54 +0000 | [diff] [blame] | 1190 | // Floating point conversions (C++ 4.8). |
| 1191 | SCS.Second = ICK_Floating_Conversion; |
| 1192 | FromType = ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1193 | } else if ((FromType->isRealFloatingType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1194 | ToType->isIntegralType(S.Context)) || |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1195 | (FromType->isIntegralOrUnscopedEnumerationType() && |
Douglas Gregor | 49b4d73 | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1196 | ToType->isRealFloatingType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1197 | // Floating-integral conversions (C++ 4.9). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1198 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1199 | FromType = ToType.getUnqualifiedType(); |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1200 | } else if (S.IsBlockPointerConversion(FromType, ToType, FromType)) { |
| 1201 | SCS.Second = ICK_Block_Pointer_Conversion; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1202 | } else if (S.IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 1203 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1204 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1205 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1206 | SCS.IncompatibleObjC = IncompatibleObjC; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1207 | } else if (S.IsMemberPointerConversion(From, FromType, ToType, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1208 | InOverloadResolution, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1209 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1210 | SCS.Second = ICK_Pointer_Member; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1211 | } else if (IsVectorConversion(S.Context, FromType, ToType, SecondICK)) { |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1212 | SCS.Second = SecondICK; |
| 1213 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1214 | } else if (!S.getLangOptions().CPlusPlus && |
| 1215 | S.Context.typesAreCompatible(ToType, FromType)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1216 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1217 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 4618868 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 1218 | FromType = ToType.getUnqualifiedType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1219 | } else if (IsNoReturnConversion(S.Context, FromType, ToType, FromType)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1220 | // Treat a conversion that strips "noreturn" as an identity conversion. |
| 1221 | SCS.Second = ICK_NoReturn_Adjustment; |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1222 | } else if (IsTransparentUnionStandardConversion(S, From, ToType, |
| 1223 | InOverloadResolution, |
| 1224 | SCS, CStyle)) { |
| 1225 | SCS.Second = ICK_TransparentUnionConversion; |
| 1226 | FromType = ToType; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1227 | } else { |
| 1228 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1229 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1230 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1231 | SCS.setToType(1, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1232 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1233 | QualType CanonFrom; |
| 1234 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1235 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 1236 | if (S.IsQualificationConversion(FromType, ToType, CStyle)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1237 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1238 | FromType = ToType; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1239 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1240 | CanonTo = S.Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1241 | } else { |
| 1242 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1243 | SCS.Third = ICK_Identity; |
| 1244 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1246 | // [...] Any difference in top-level cv-qualification is |
| 1247 | // subsumed by the initialization itself and does not constitute |
| 1248 | // a conversion. [...] |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1249 | CanonFrom = S.Context.getCanonicalType(FromType); |
| 1250 | CanonTo = S.Context.getCanonicalType(ToType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | if (CanonFrom.getLocalUnqualifiedType() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1252 | == CanonTo.getLocalUnqualifiedType() && |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1253 | (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers() |
| 1254 | || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr())) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1255 | FromType = ToType; |
| 1256 | CanonFrom = CanonTo; |
| 1257 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1258 | } |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1259 | SCS.setToType(2, FromType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1260 | |
| 1261 | // If we have not converted the argument type to the parameter type, |
| 1262 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1263 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1264 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1266 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1267 | } |
Fariborz Jahanian | 16f92ce | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 1268 | |
| 1269 | static bool |
| 1270 | IsTransparentUnionStandardConversion(Sema &S, Expr* From, |
| 1271 | QualType &ToType, |
| 1272 | bool InOverloadResolution, |
| 1273 | StandardConversionSequence &SCS, |
| 1274 | bool CStyle) { |
| 1275 | |
| 1276 | const RecordType *UT = ToType->getAsUnionType(); |
| 1277 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 1278 | return false; |
| 1279 | // The field to initialize within the transparent union. |
| 1280 | RecordDecl *UD = UT->getDecl(); |
| 1281 | // It's compatible if the expression matches any of the fields. |
| 1282 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1283 | itend = UD->field_end(); |
| 1284 | it != itend; ++it) { |
| 1285 | if (IsStandardConversion(S, From, it->getType(), InOverloadResolution, SCS, CStyle)) { |
| 1286 | ToType = it->getType(); |
| 1287 | return true; |
| 1288 | } |
| 1289 | } |
| 1290 | return false; |
| 1291 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1292 | |
| 1293 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 1294 | /// expression From (whose potentially-adjusted type is FromType) to |
| 1295 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 1296 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1298 | const BuiltinType *To = ToType->getAs<BuiltinType>(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 1299 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1300 | if (!To) { |
| 1301 | return false; |
| 1302 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1303 | |
| 1304 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 1305 | // unsigned short int can be converted to an rvalue of type int if |
| 1306 | // int can represent all the values of the source type; otherwise, |
| 1307 | // the source rvalue can be converted to an rvalue of type unsigned |
| 1308 | // int (C++ 4.5p1). |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1309 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType() && |
| 1310 | !FromType->isEnumeralType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1311 | if (// We can promote any signed, promotable integer type to an int |
| 1312 | (FromType->isSignedIntegerType() || |
| 1313 | // We can promote any unsigned integer type whose size is |
| 1314 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1316 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1317 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1320 | return To->getKind() == BuiltinType::UInt; |
| 1321 | } |
| 1322 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1323 | // C++0x [conv.prom]p3: |
| 1324 | // A prvalue of an unscoped enumeration type whose underlying type is not |
| 1325 | // fixed (7.2) can be converted to an rvalue a prvalue of the first of the |
| 1326 | // following types that can represent all the values of the enumeration |
| 1327 | // (i.e., the values in the range bmin to bmax as described in 7.2): int, |
| 1328 | // unsigned int, long int, unsigned long int, long long int, or unsigned |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1329 | // long long int. If none of the types in that list can represent all the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1330 | // values of the enumeration, an rvalue a prvalue of an unscoped enumeration |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1331 | // type can be converted to an rvalue a prvalue of the extended integer type |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1332 | // with lowest integer conversion rank (4.13) greater than the rank of long |
| 1333 | // long in which all the values of the enumeration can be represented. If |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1334 | // there are two such extended types, the signed one is chosen. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1335 | if (const EnumType *FromEnumType = FromType->getAs<EnumType>()) { |
| 1336 | // C++0x 7.2p9: Note that this implicit enum to int conversion is not |
| 1337 | // provided for a scoped enumeration. |
| 1338 | if (FromEnumType->getDecl()->isScoped()) |
| 1339 | return false; |
| 1340 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1341 | // We have already pre-calculated the promotion type, so this is trivial. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1342 | if (ToType->isIntegerType() && |
Douglas Gregor | c87f4d4 | 2010-09-12 03:38:25 +0000 | [diff] [blame] | 1343 | !RequireCompleteType(From->getLocStart(), FromType, PDiag())) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1344 | return Context.hasSameUnqualifiedType(ToType, |
| 1345 | FromEnumType->getDecl()->getPromotionType()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1346 | } |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1347 | |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1348 | // C++0x [conv.prom]p2: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1349 | // A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted |
| 1350 | // to an rvalue a prvalue of the first of the following types that can |
| 1351 | // represent all the values of its underlying type: int, unsigned int, |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1352 | // long int, unsigned long int, long long int, or unsigned long long int. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1353 | // If none of the types in that list can represent all the values of its |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1354 | // underlying type, an rvalue a prvalue of type char16_t, char32_t, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1355 | // or wchar_t can be converted to an rvalue a prvalue of its underlying |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1356 | // type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1357 | if (FromType->isAnyCharacterType() && !FromType->isCharType() && |
Douglas Gregor | cd1d0b4 | 2010-10-21 18:04:08 +0000 | [diff] [blame] | 1358 | ToType->isIntegerType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1359 | // Determine whether the type we're converting from is signed or |
| 1360 | // unsigned. |
| 1361 | bool FromIsSigned; |
| 1362 | uint64_t FromSize = Context.getTypeSize(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1363 | |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 1364 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 1365 | FromIsSigned = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1366 | |
| 1367 | // The types we'll try to promote to, in the appropriate |
| 1368 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | QualType PromoteTypes[6] = { |
| 1370 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1371 | Context.LongTy, Context.UnsignedLongTy , |
| 1372 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1373 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 1374 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1375 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 1376 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1378 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 1379 | // We found the type that we can promote to. If this is the |
| 1380 | // type we wanted, we have a promotion. Otherwise, no |
| 1381 | // promotion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1382 | return Context.hasSameUnqualifiedType(ToType, PromoteTypes[Idx]); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 1388 | // rvalue of type int if int can represent all the values of the |
| 1389 | // bit-field; otherwise, it can be converted to unsigned int if |
| 1390 | // unsigned int can represent all the values of the bit-field. If |
| 1391 | // the bit-field is larger yet, no integral promotion applies to |
| 1392 | // it. If the bit-field has an enumerated type, it is treated as any |
| 1393 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1394 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 1395 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1396 | using llvm::APSInt; |
| 1397 | if (From) |
| 1398 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1399 | APSInt BitWidth; |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1400 | if (FromType->isIntegralType(Context) && |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1401 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 1402 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 1403 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1405 | // Are we promoting to an int from a bitfield that fits in an int? |
| 1406 | if (BitWidth < ToSize || |
| 1407 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 1408 | return To->getKind() == BuiltinType::Int; |
| 1409 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1411 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 1412 | // that fits into an unsigned int? |
| 1413 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 1414 | return To->getKind() == BuiltinType::UInt; |
| 1415 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1417 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1418 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1419 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1421 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 1422 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1423 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1424 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1425 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1426 | |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 1431 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 1432 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1434 | /// An rvalue of type float can be converted to an rvalue of type |
| 1435 | /// double. (C++ 4.6p1). |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1436 | if (const BuiltinType *FromBuiltin = FromType->getAs<BuiltinType>()) |
| 1437 | if (const BuiltinType *ToBuiltin = ToType->getAs<BuiltinType>()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1438 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 1439 | ToBuiltin->getKind() == BuiltinType::Double) |
| 1440 | return true; |
| 1441 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1442 | // C99 6.3.1.5p1: |
| 1443 | // When a float is promoted to double or long double, or a |
| 1444 | // double is promoted to long double [...]. |
| 1445 | if (!getLangOptions().CPlusPlus && |
| 1446 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 1447 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 1448 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 1449 | return true; |
| 1450 | } |
| 1451 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1452 | return false; |
| 1453 | } |
| 1454 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1455 | /// \brief Determine if a conversion is a complex promotion. |
| 1456 | /// |
| 1457 | /// A complex promotion is defined as a complex -> complex conversion |
| 1458 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1459 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1460 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1461 | const ComplexType *FromComplex = FromType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1462 | if (!FromComplex) |
| 1463 | return false; |
| 1464 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1465 | const ComplexType *ToComplex = ToType->getAs<ComplexType>(); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1466 | if (!ToComplex) |
| 1467 | return false; |
| 1468 | |
| 1469 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 1470 | ToComplex->getElementType()) || |
| 1471 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 1472 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1475 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 1476 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 1477 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 1478 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 1479 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | static QualType |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1481 | BuildSimilarlyQualifiedPointerType(const Type *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1482 | QualType ToPointee, QualType ToType, |
| 1483 | ASTContext &Context) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1484 | assert((FromPtr->getTypeClass() == Type::Pointer || |
| 1485 | FromPtr->getTypeClass() == Type::ObjCObjectPointer) && |
| 1486 | "Invalid similarly-qualified pointer type"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | c6bd1d3 | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 1488 | /// \brief Conversions to 'id' subsume cv-qualifier conversions. |
| 1489 | if (ToType->isObjCIdType() || ToType->isObjCQualifiedIdType()) |
| 1490 | return ToType.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1491 | |
| 1492 | QualType CanonFromPointee |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1493 | = Context.getCanonicalType(FromPtr->getPointeeType()); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1494 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1495 | Qualifiers Quals = CanonFromPointee.getQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
| 1497 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1498 | if (CanonToPointee.getLocalQualifiers() == Quals) { |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1499 | // ToType is exactly what we need. Return it. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1500 | if (!ToType.isNull()) |
Douglas Gregor | b9f907b | 2010-05-25 15:31:05 +0000 | [diff] [blame] | 1501 | return ToType.getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Build a pointer to ToPointee. It has the right qualifiers |
| 1504 | // already. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1505 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1506 | return Context.getObjCObjectPointerType(ToPointee); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1507 | return Context.getPointerType(ToPointee); |
| 1508 | } |
| 1509 | |
| 1510 | // Just build a canonical type that has the right qualifiers. |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1511 | QualType QualifiedCanonToPointee |
| 1512 | = Context.getQualifiedType(CanonToPointee.getLocalUnqualifiedType(), Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1513 | |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1514 | if (isa<ObjCObjectPointerType>(ToType)) |
| 1515 | return Context.getObjCObjectPointerType(QualifiedCanonToPointee); |
| 1516 | return Context.getPointerType(QualifiedCanonToPointee); |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1518 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1520 | bool InOverloadResolution, |
| 1521 | ASTContext &Context) { |
| 1522 | // Handle value-dependent integral null pointer constants correctly. |
| 1523 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 1524 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1525 | Expr->getType()->isIntegerType() && !Expr->getType()->isEnumeralType()) |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1526 | return !InOverloadResolution; |
| 1527 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1528 | return Expr->isNullPointerConstant(Context, |
| 1529 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 1530 | : Expr::NPC_ValueDependentIsNull); |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1531 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1533 | /// IsPointerConversion - Determines whether the conversion of the |
| 1534 | /// expression From, which has the (possibly adjusted) type FromType, |
| 1535 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 1536 | /// 4.10). If so, returns true and places the converted type (that |
| 1537 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 1538 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1539 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1540 | /// This routine also supports conversions to and from block pointers |
| 1541 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 1542 | /// pointers to interfaces. FIXME: Once we've determined the |
| 1543 | /// appropriate overloading rules for Objective-C, we may want to |
| 1544 | /// split the Objective-C checks into a different routine; however, |
| 1545 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1546 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 1547 | /// set if the conversion is an allowed Objective-C conversion that |
| 1548 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1549 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1550 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1551 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1553 | IncompatibleObjC = false; |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1554 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, |
| 1555 | IncompatibleObjC)) |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1556 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1557 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 1559 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1560 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 1561 | ConvertedType = ToType; |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1565 | // Blocks: Block pointers can be converted to void*. |
| 1566 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1567 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1568 | ConvertedType = ToType; |
| 1569 | return true; |
| 1570 | } |
| 1571 | // Blocks: A null pointer constant can be converted to a block |
| 1572 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1574 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 1575 | ConvertedType = ToType; |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1579 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 1580 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 1582 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1583 | ConvertedType = ToType; |
| 1584 | return true; |
| 1585 | } |
| 1586 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1587 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1588 | if (!ToTypePtr) |
| 1589 | return false; |
| 1590 | |
| 1591 | // 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] | 1592 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1593 | ConvertedType = ToType; |
| 1594 | return true; |
| 1595 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1596 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1597 | // Beyond this point, both types need to be pointers |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1598 | // , including objective-c pointers. |
| 1599 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 1600 | if (FromType->isObjCObjectPointerType() && ToPointeeType->isVoidType()) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1601 | ConvertedType = BuildSimilarlyQualifiedPointerType( |
| 1602 | FromType->getAs<ObjCObjectPointerType>(), |
| 1603 | ToPointeeType, |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1604 | ToType, Context); |
| 1605 | return true; |
Fariborz Jahanian | 01cbe44 | 2009-12-16 23:13:33 +0000 | [diff] [blame] | 1606 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1607 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1608 | if (!FromTypePtr) |
| 1609 | return false; |
| 1610 | |
| 1611 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1612 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1613 | // If the unqualified pointee types are the same, this can't be a |
Douglas Gregor | fb64086 | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 1614 | // pointer conversion, so don't do all of the work below. |
| 1615 | if (Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) |
| 1616 | return false; |
| 1617 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1618 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 1619 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 1620 | // 4.10p2). |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1621 | if (FromPointeeType->isIncompleteOrObjectType() && |
| 1622 | ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1624 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1625 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1626 | return true; |
| 1627 | } |
| 1628 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1629 | // When we're overloading in C, we allow a special kind of pointer |
| 1630 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1632 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1634 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1636 | return true; |
| 1637 | } |
| 1638 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1639 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1641 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 1642 | // can be converted to an rvalue of type "pointer to cv B," where |
| 1643 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 1644 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 1645 | // necessitates this conversion is ill-formed. The result of the |
| 1646 | // conversion is a pointer to the base class sub-object of the |
| 1647 | // derived class object. The null pointer value is converted to |
| 1648 | // the null pointer value of the destination type. |
| 1649 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1650 | // Note that we do not check for ambiguity or inaccessibility |
| 1651 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1652 | if (getLangOptions().CPlusPlus && |
| 1653 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | d28f041 | 2010-02-22 17:06:41 +0000 | [diff] [blame] | 1654 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType) && |
Douglas Gregor | e6fb91f | 2009-10-29 23:08:22 +0000 | [diff] [blame] | 1655 | !RequireCompleteType(From->getLocStart(), FromPointeeType, PDiag()) && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1656 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 1658 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1659 | ToType, Context); |
| 1660 | return true; |
| 1661 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1663 | return false; |
| 1664 | } |
| 1665 | |
| 1666 | /// isObjCPointerConversion - Determines whether this is an |
| 1667 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 1668 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1670 | QualType& ConvertedType, |
| 1671 | bool &IncompatibleObjC) { |
| 1672 | if (!getLangOptions().ObjC1) |
| 1673 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1675 | // First, we handle all conversions on ObjC object pointer types. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1676 | const ObjCObjectPointerType* ToObjCPtr = |
| 1677 | ToType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | const ObjCObjectPointerType *FromObjCPtr = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1679 | FromType->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1680 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1681 | if (ToObjCPtr && FromObjCPtr) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1682 | // If the pointee types are the same (ignoring qualifications), |
| 1683 | // then this is not a pointer conversion. |
| 1684 | if (Context.hasSameUnqualifiedType(ToObjCPtr->getPointeeType(), |
| 1685 | FromObjCPtr->getPointeeType())) |
| 1686 | return false; |
| 1687 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1688 | // 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] | 1689 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1690 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1691 | ConvertedType = ToType; |
| 1692 | return true; |
| 1693 | } |
| 1694 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1696 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1698 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1699 | ConvertedType = ToType; |
| 1700 | return true; |
| 1701 | } |
| 1702 | // Objective C++: We're able to convert from a pointer to an |
| 1703 | // interface to a pointer to a different interface. |
| 1704 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
Fariborz Jahanian | b397e43 | 2010-03-15 18:36:00 +0000 | [diff] [blame] | 1705 | const ObjCInterfaceType* LHS = ToObjCPtr->getInterfaceType(); |
| 1706 | const ObjCInterfaceType* RHS = FromObjCPtr->getInterfaceType(); |
| 1707 | if (getLangOptions().CPlusPlus && LHS && RHS && |
| 1708 | !ToObjCPtr->getPointeeType().isAtLeastAsQualifiedAs( |
| 1709 | FromObjCPtr->getPointeeType())) |
| 1710 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1711 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1712 | ToObjCPtr->getPointeeType(), |
| 1713 | ToType, Context); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1714 | return true; |
| 1715 | } |
| 1716 | |
| 1717 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1718 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1719 | // interfaces, which is permitted. However, we're going to |
| 1720 | // complain about it. |
| 1721 | IncompatibleObjC = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1722 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromObjCPtr, |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1723 | ToObjCPtr->getPointeeType(), |
| 1724 | ToType, Context); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1725 | return true; |
| 1726 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1728 | // 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] | 1729 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1730 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1731 | ToPointeeType = ToCPtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1732 | else if (const BlockPointerType *ToBlockPtr = |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1733 | ToType->getAs<BlockPointerType>()) { |
Fariborz Jahanian | 879cc73 | 2010-01-21 00:08:17 +0000 | [diff] [blame] | 1734 | // 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] | 1735 | // to a block pointer type. |
| 1736 | if (FromObjCPtr && FromObjCPtr->isObjCBuiltinType()) { |
| 1737 | ConvertedType = ToType; |
| 1738 | return true; |
| 1739 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1740 | ToPointeeType = ToBlockPtr->getPointeeType(); |
Fariborz Jahanian | 4efdec0 | 2010-01-20 22:54:38 +0000 | [diff] [blame] | 1741 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1742 | else if (FromType->getAs<BlockPointerType>() && |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1743 | ToObjCPtr && ToObjCPtr->isObjCBuiltinType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1744 | // 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] | 1745 | // pointer to any object. |
Fariborz Jahanian | e4951fd | 2010-01-21 00:05:09 +0000 | [diff] [blame] | 1746 | ConvertedType = ToType; |
| 1747 | return true; |
| 1748 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1749 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1750 | return false; |
| 1751 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1752 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1753 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1754 | FromPointeeType = FromCPtr->getPointeeType(); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 1755 | else if (const BlockPointerType *FromBlockPtr = |
| 1756 | FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1757 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1758 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1759 | return false; |
| 1760 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1761 | // If we have pointers to pointers, recursively check whether this |
| 1762 | // is an Objective-C conversion. |
| 1763 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1764 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1765 | IncompatibleObjC)) { |
| 1766 | // We always complain about this conversion. |
| 1767 | IncompatibleObjC = true; |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1768 | ConvertedType = Context.getPointerType(ConvertedType); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1769 | return true; |
| 1770 | } |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1771 | // Allow conversion of pointee being objective-c pointer to another one; |
| 1772 | // as in I* to id. |
| 1773 | if (FromPointeeType->getAs<ObjCObjectPointerType>() && |
| 1774 | ToPointeeType->getAs<ObjCObjectPointerType>() && |
| 1775 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1776 | IncompatibleObjC)) { |
Douglas Gregor | 8d6d067 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 1777 | ConvertedType = Context.getPointerType(ConvertedType); |
Fariborz Jahanian | 42ffdb3 | 2010-01-18 22:59:22 +0000 | [diff] [blame] | 1778 | return true; |
| 1779 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1781 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1782 | // differences in the argument and result types are in Objective-C |
| 1783 | // pointer conversions. If so, we permit the conversion (but |
| 1784 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | const FunctionProtoType *FromFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1786 | = FromPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1787 | const FunctionProtoType *ToFunctionType |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1788 | = ToPointeeType->getAs<FunctionProtoType>(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1789 | if (FromFunctionType && ToFunctionType) { |
| 1790 | // If the function types are exactly the same, this isn't an |
| 1791 | // Objective-C pointer conversion. |
| 1792 | if (Context.getCanonicalType(FromPointeeType) |
| 1793 | == Context.getCanonicalType(ToPointeeType)) |
| 1794 | return false; |
| 1795 | |
| 1796 | // Perform the quick checks that will tell us whether these |
| 1797 | // function types are obviously different. |
| 1798 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1799 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1800 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1801 | return false; |
| 1802 | |
| 1803 | bool HasObjCConversion = false; |
| 1804 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1805 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1806 | // Okay, the types match exactly. Nothing to do. |
| 1807 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1808 | ToFunctionType->getResultType(), |
| 1809 | ConvertedType, IncompatibleObjC)) { |
| 1810 | // Okay, we have an Objective-C pointer conversion. |
| 1811 | HasObjCConversion = true; |
| 1812 | } else { |
| 1813 | // Function types are too different. Abort. |
| 1814 | return false; |
| 1815 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1817 | // Check argument types. |
| 1818 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1819 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1820 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1821 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1822 | if (Context.getCanonicalType(FromArgType) |
| 1823 | == Context.getCanonicalType(ToArgType)) { |
| 1824 | // Okay, the types match exactly. Nothing to do. |
| 1825 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1826 | ConvertedType, IncompatibleObjC)) { |
| 1827 | // Okay, we have an Objective-C pointer conversion. |
| 1828 | HasObjCConversion = true; |
| 1829 | } else { |
| 1830 | // Argument types are too different. Abort. |
| 1831 | return false; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | if (HasObjCConversion) { |
| 1836 | // We had an Objective-C conversion. Allow this pointer |
| 1837 | // conversion, but complain about it. |
| 1838 | ConvertedType = ToType; |
| 1839 | IncompatibleObjC = true; |
| 1840 | return true; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1844 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1845 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1846 | |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1847 | bool Sema::IsBlockPointerConversion(QualType FromType, QualType ToType, |
| 1848 | QualType& ConvertedType) { |
| 1849 | QualType ToPointeeType; |
| 1850 | if (const BlockPointerType *ToBlockPtr = |
| 1851 | ToType->getAs<BlockPointerType>()) |
| 1852 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1853 | else |
| 1854 | return false; |
| 1855 | |
| 1856 | QualType FromPointeeType; |
| 1857 | if (const BlockPointerType *FromBlockPtr = |
| 1858 | FromType->getAs<BlockPointerType>()) |
| 1859 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1860 | else |
| 1861 | return false; |
| 1862 | // We have pointer to blocks, check whether the only |
| 1863 | // differences in the argument and result types are in Objective-C |
| 1864 | // pointer conversions. If so, we permit the conversion. |
| 1865 | |
| 1866 | const FunctionProtoType *FromFunctionType |
| 1867 | = FromPointeeType->getAs<FunctionProtoType>(); |
| 1868 | const FunctionProtoType *ToFunctionType |
| 1869 | = ToPointeeType->getAs<FunctionProtoType>(); |
| 1870 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1871 | if (!FromFunctionType || !ToFunctionType) |
| 1872 | return false; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1873 | |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1874 | if (Context.hasSameType(FromPointeeType, ToPointeeType)) |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1875 | return true; |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Perform the quick checks that will tell us whether these |
| 1878 | // function types are obviously different. |
| 1879 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1880 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic()) |
| 1881 | return false; |
| 1882 | |
| 1883 | FunctionType::ExtInfo FromEInfo = FromFunctionType->getExtInfo(); |
| 1884 | FunctionType::ExtInfo ToEInfo = ToFunctionType->getExtInfo(); |
| 1885 | if (FromEInfo != ToEInfo) |
| 1886 | return false; |
| 1887 | |
| 1888 | bool IncompatibleObjC = false; |
Fariborz Jahanian | 12834e1 | 2011-02-13 20:11:42 +0000 | [diff] [blame] | 1889 | if (Context.hasSameType(FromFunctionType->getResultType(), |
| 1890 | ToFunctionType->getResultType())) { |
Fariborz Jahanian | 4de45dc | 2011-02-13 20:01:48 +0000 | [diff] [blame] | 1891 | // Okay, the types match exactly. Nothing to do. |
| 1892 | } else { |
| 1893 | QualType RHS = FromFunctionType->getResultType(); |
| 1894 | QualType LHS = ToFunctionType->getResultType(); |
| 1895 | if ((!getLangOptions().CPlusPlus || !RHS->isRecordType()) && |
| 1896 | !RHS.hasQualifiers() && LHS.hasQualifiers()) |
| 1897 | LHS = LHS.getUnqualifiedType(); |
| 1898 | |
| 1899 | if (Context.hasSameType(RHS,LHS)) { |
| 1900 | // OK exact match. |
| 1901 | } else if (isObjCPointerConversion(RHS, LHS, |
| 1902 | ConvertedType, IncompatibleObjC)) { |
| 1903 | if (IncompatibleObjC) |
| 1904 | return false; |
| 1905 | // Okay, we have an Objective-C pointer conversion. |
| 1906 | } |
| 1907 | else |
| 1908 | return false; |
| 1909 | } |
| 1910 | |
| 1911 | // Check argument types. |
| 1912 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1913 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1914 | IncompatibleObjC = false; |
| 1915 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1916 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1917 | if (Context.hasSameType(FromArgType, ToArgType)) { |
| 1918 | // Okay, the types match exactly. Nothing to do. |
| 1919 | } else if (isObjCPointerConversion(ToArgType, FromArgType, |
| 1920 | ConvertedType, IncompatibleObjC)) { |
| 1921 | if (IncompatibleObjC) |
| 1922 | return false; |
| 1923 | // Okay, we have an Objective-C pointer conversion. |
| 1924 | } else |
| 1925 | // Argument types are too different. Abort. |
| 1926 | return false; |
| 1927 | } |
| 1928 | ConvertedType = ToType; |
| 1929 | return true; |
Fariborz Jahanian | 42455ea | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1932 | /// FunctionArgTypesAreEqual - This routine checks two function proto types |
| 1933 | /// for equlity of their argument types. Caller has already checked that |
| 1934 | /// they have same number of arguments. This routine assumes that Objective-C |
| 1935 | /// pointer types which only differ in their protocol qualifiers are equal. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1936 | bool Sema::FunctionArgTypesAreEqual(const FunctionProtoType *OldType, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1937 | const FunctionProtoType *NewType) { |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1938 | if (!getLangOptions().ObjC1) |
| 1939 | return std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1940 | NewType->arg_type_begin()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1941 | |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1942 | for (FunctionProtoType::arg_type_iterator O = OldType->arg_type_begin(), |
| 1943 | N = NewType->arg_type_begin(), |
| 1944 | E = OldType->arg_type_end(); O && (O != E); ++O, ++N) { |
| 1945 | QualType ToType = (*O); |
| 1946 | QualType FromType = (*N); |
| 1947 | if (ToType != FromType) { |
| 1948 | if (const PointerType *PTTo = ToType->getAs<PointerType>()) { |
| 1949 | if (const PointerType *PTFr = FromType->getAs<PointerType>()) |
Chandler Carruth | 27c9fe9 | 2010-05-06 00:15:06 +0000 | [diff] [blame] | 1950 | if ((PTTo->getPointeeType()->isObjCQualifiedIdType() && |
| 1951 | PTFr->getPointeeType()->isObjCQualifiedIdType()) || |
| 1952 | (PTTo->getPointeeType()->isObjCQualifiedClassType() && |
| 1953 | PTFr->getPointeeType()->isObjCQualifiedClassType())) |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1954 | continue; |
| 1955 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1956 | else if (const ObjCObjectPointerType *PTTo = |
| 1957 | ToType->getAs<ObjCObjectPointerType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1958 | if (const ObjCObjectPointerType *PTFr = |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1959 | FromType->getAs<ObjCObjectPointerType>()) |
| 1960 | if (PTTo->getInterfaceDecl() == PTFr->getInterfaceDecl()) |
| 1961 | continue; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1962 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1963 | return false; |
Fariborz Jahanian | 5e5998f | 2010-05-03 21:06:18 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | return true; |
| 1967 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1969 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1970 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1971 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1972 | /// conversions for which IsPointerConversion has already returned |
| 1973 | /// true. It returns true and produces a diagnostic if there was an |
| 1974 | /// error, or returns false otherwise. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1975 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1976 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1977 | CXXCastPath& BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1978 | bool IgnoreBaseAccess) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1979 | QualType FromType = From->getType(); |
Argyrios Kyrtzidis | d6ea6bd | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 1980 | bool IsCStyleOrFunctionalCast = IgnoreBaseAccess; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1981 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1982 | Kind = CK_BitCast; |
| 1983 | |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1984 | if (CXXBoolLiteralExpr* LitBool |
| 1985 | = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens())) |
Argyrios Kyrtzidis | d6ea6bd | 2010-09-28 14:54:11 +0000 | [diff] [blame] | 1986 | if (!IsCStyleOrFunctionalCast && LitBool->getValue() == false) |
Chandler Carruth | 477a999 | 2011-03-01 03:29:37 +0000 | [diff] [blame] | 1987 | DiagRuntimeBehavior(LitBool->getExprLoc(), From, |
| 1988 | PDiag(diag::warn_init_pointer_from_false) << ToType); |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 1989 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1990 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1991 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1992 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1993 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1994 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1995 | if (FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
| 1996 | !Context.hasSameUnqualifiedType(FromPointeeType, ToPointeeType)) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1997 | // We must have a derived-to-base conversion. Check an |
| 1998 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1999 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 2000 | From->getExprLoc(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2001 | From->getSourceRange(), &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2002 | IgnoreBaseAccess)) |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2003 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2004 | |
Anders Carlsson | 7ec8ccd | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2005 | // The conversion was successful. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2006 | Kind = CK_DerivedToBase; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | if (const ObjCObjectPointerType *FromPtrType = |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2010 | FromType->getAs<ObjCObjectPointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | if (const ObjCObjectPointerType *ToPtrType = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2012 | ToType->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2013 | // Objective-C++ conversions are always okay. |
| 2014 | // FIXME: We should have a different class of conversions for the |
| 2015 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2016 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2017 | return false; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2018 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2019 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2020 | |
| 2021 | // We shouldn't fall into this case unless it's valid for other |
| 2022 | // reasons. |
| 2023 | if (From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 2024 | Kind = CK_NullToPointer; |
| 2025 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2026 | return false; |
| 2027 | } |
| 2028 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2029 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 2030 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 2031 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 2032 | /// If so, returns true and places the converted type (that might differ from |
| 2033 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 2034 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2035 | QualType ToType, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2036 | bool InOverloadResolution, |
| 2037 | QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2038 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2039 | if (!ToTypePtr) |
| 2040 | return false; |
| 2041 | |
| 2042 | // 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] | 2043 | if (From->isNullPointerConstant(Context, |
| 2044 | InOverloadResolution? Expr::NPC_ValueDependentIsNotNull |
| 2045 | : Expr::NPC_ValueDependentIsNull)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2046 | ConvertedType = ToType; |
| 2047 | return true; |
| 2048 | } |
| 2049 | |
| 2050 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2051 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2052 | if (!FromTypePtr) |
| 2053 | return false; |
| 2054 | |
| 2055 | // A pointer to member of B can be converted to a pointer to member of D, |
| 2056 | // where D is derived from B (C++ 4.11p2). |
| 2057 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 2058 | QualType ToClass(ToTypePtr->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 7f6ae69 | 2010-12-21 21:40:41 +0000 | [diff] [blame] | 2060 | if (!Context.hasSameUnqualifiedType(FromClass, ToClass) && |
| 2061 | !RequireCompleteType(From->getLocStart(), ToClass, PDiag()) && |
| 2062 | IsDerivedFrom(ToClass, FromClass)) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2063 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 2064 | ToClass.getTypePtr()); |
| 2065 | return true; |
| 2066 | } |
| 2067 | |
| 2068 | return false; |
| 2069 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2070 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2071 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 2072 | /// expression From to the type ToType. This routine checks for ambiguous or |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2073 | /// virtual or inaccessible base-to-derived member pointer conversions |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2074 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 2075 | /// true and produces a diagnostic if there was an error, or returns false |
| 2076 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2078 | CastKind &Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2079 | CXXCastPath &BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2080 | bool IgnoreBaseAccess) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2081 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2082 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2083 | if (!FromPtrType) { |
| 2084 | // This must be a null pointer to member pointer conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2085 | assert(From->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2086 | Expr::NPC_ValueDependentIsNull) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2087 | "Expr must be null pointer constant!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2088 | Kind = CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2089 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2090 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2091 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2092 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2093 | assert(ToPtrType && "No member pointer cast has a target type " |
| 2094 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2095 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2096 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 2097 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2098 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2099 | // FIXME: What about dependent types? |
| 2100 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 2101 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2102 | |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2103 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2104 | /*DetectVirtual=*/true); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2105 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 2106 | assert(DerivationOkay && |
| 2107 | "Should not have been called if derivation isn't OK."); |
| 2108 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2109 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2110 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 2111 | getUnqualifiedType())) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2112 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 2113 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 2114 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 2115 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2116 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2117 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2118 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2119 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 2120 | << FromClass << ToClass << QualType(VBase, 0) |
| 2121 | << From->getSourceRange(); |
| 2122 | return true; |
| 2123 | } |
| 2124 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2125 | if (!IgnoreBaseAccess) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2126 | CheckBaseClassAccess(From->getExprLoc(), FromClass, ToClass, |
| 2127 | Paths.front(), |
| 2128 | diag::err_downcast_from_inaccessible_base); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 2129 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 2130 | // Must be a base to derived member conversion. |
Anders Carlsson | 7d3360f | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 2131 | BuildBasePathArray(Paths, BasePath); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2132 | Kind = CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 2133 | return false; |
| 2134 | } |
| 2135 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2136 | /// IsQualificationConversion - Determines whether the conversion from |
| 2137 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 2138 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | bool |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | Sema::IsQualificationConversion(QualType FromType, QualType ToType, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2141 | bool CStyle) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2142 | FromType = Context.getCanonicalType(FromType); |
| 2143 | ToType = Context.getCanonicalType(ToType); |
| 2144 | |
| 2145 | // If FromType and ToType are the same type, this is not a |
| 2146 | // qualification conversion. |
Sebastian Redl | cbdffb1 | 2010-02-03 19:36:07 +0000 | [diff] [blame] | 2147 | if (FromType.getUnqualifiedType() == ToType.getUnqualifiedType()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2148 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2150 | // (C++ 4.4p4): |
| 2151 | // A conversion can add cv-qualifiers at levels other than the first |
| 2152 | // in multi-level pointers, subject to the following rules: [...] |
| 2153 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2154 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2155 | while (Context.UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2156 | // Within each iteration of the loop, we check the qualifiers to |
| 2157 | // determine if this still looks like a qualification |
| 2158 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2159 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2160 | // until there are no more pointers or pointers-to-members left to |
| 2161 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2162 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2163 | |
| 2164 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 2165 | // 2,j, and similarly for volatile. |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2166 | if (!CStyle && !ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2167 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2169 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 2170 | // every cv for 0 < k < j. |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2171 | if (!CStyle && FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2172 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2173 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2175 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 2176 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2178 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2179 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2180 | |
| 2181 | // We are left with FromType and ToType being the pointee types |
| 2182 | // after unwrapping the original FromType and ToType the same number |
| 2183 | // of types. If we unwrapped any pointers, and if FromType and |
| 2184 | // ToType have the same unqualified type (since we checked |
| 2185 | // qualifiers above), then this is a qualification conversion. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2186 | return UnwrappedAnyPointer && Context.hasSameUnqualifiedType(FromType,ToType); |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2189 | /// Determines whether there is a user-defined conversion sequence |
| 2190 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 2191 | /// ToType. If such a conversion exists, User will contain the |
| 2192 | /// user-defined conversion sequence that performs such a conversion |
| 2193 | /// and this routine will return true. Otherwise, this routine returns |
| 2194 | /// false and User is unspecified. |
| 2195 | /// |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 2196 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 2197 | /// "explicit" conversion functions as well as non-explicit conversion |
| 2198 | /// functions (C++0x [class.conv.fct]p2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2199 | static OverloadingResult |
| 2200 | IsUserDefinedConversion(Sema &S, Expr *From, QualType ToType, |
| 2201 | UserDefinedConversionSequence& User, |
| 2202 | OverloadCandidateSet& CandidateSet, |
| 2203 | bool AllowExplicit) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2204 | // Whether we will only visit constructors. |
| 2205 | bool ConstructorsOnly = false; |
| 2206 | |
| 2207 | // If the type we are conversion to is a class type, enumerate its |
| 2208 | // constructors. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2209 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2210 | // C++ [over.match.ctor]p1: |
| 2211 | // When objects of class type are direct-initialized (8.5), or |
| 2212 | // copy-initialized from an expression of the same or a |
| 2213 | // derived class type (8.5), overload resolution selects the |
| 2214 | // constructor. [...] For copy-initialization, the candidate |
| 2215 | // functions are all the converting constructors (12.3.1) of |
| 2216 | // that class. The argument list is the expression-list within |
| 2217 | // the parentheses of the initializer. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2218 | if (S.Context.hasSameUnqualifiedType(ToType, From->getType()) || |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2219 | (From->getType()->getAs<RecordType>() && |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2220 | S.IsDerivedFrom(From->getType(), ToType))) |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2221 | ConstructorsOnly = true; |
| 2222 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2223 | if (S.RequireCompleteType(From->getLocStart(), ToType, S.PDiag())) { |
Douglas Gregor | 3ec1bf2 | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 2224 | // We're not going to find any constructors. |
| 2225 | } else if (CXXRecordDecl *ToRecordDecl |
| 2226 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2227 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2228 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(ToRecordDecl); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2229 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2230 | NamedDecl *D = *Con; |
| 2231 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2232 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2233 | // Find the constructor (which may be a template). |
| 2234 | CXXConstructorDecl *Constructor = 0; |
| 2235 | FunctionTemplateDecl *ConstructorTmpl |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2236 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2237 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2239 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 2240 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2241 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2242 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 2243 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2244 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2245 | if (ConstructorTmpl) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2246 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2247 | /*ExplicitArgs*/ 0, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2248 | &From, 1, CandidateSet, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2249 | /*SuppressUserConversions=*/ |
| 2250 | !ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2251 | else |
Fariborz Jahanian | b3c44f9 | 2009-10-01 20:39:51 +0000 | [diff] [blame] | 2252 | // Allow one user-defined conversion when user specifies a |
| 2253 | // From->ToType conversion via an static cast (c-style, etc). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2254 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2255 | &From, 1, CandidateSet, |
| 2256 | /*SuppressUserConversions=*/ |
| 2257 | !ConstructorsOnly); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2258 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 2259 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2263 | // Enumerate conversion functions, if we're allowed to. |
| 2264 | if (ConstructorsOnly) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2265 | } else if (S.RequireCompleteType(From->getLocStart(), From->getType(), |
| 2266 | S.PDiag(0) << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2267 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | } else if (const RecordType *FromRecordType |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2269 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2271 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 2272 | // Add all of the conversion functions as candidates. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2273 | const UnresolvedSetImpl *Conversions |
Fariborz Jahanian | f4061e3 | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 2274 | = FromRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2275 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2276 | E = Conversions->end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2277 | DeclAccessPair FoundDecl = I.getPair(); |
| 2278 | NamedDecl *D = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 2279 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2280 | if (isa<UsingShadowDecl>(D)) |
| 2281 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2282 | |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2283 | CXXConversionDecl *Conv; |
| 2284 | FunctionTemplateDecl *ConvTemplate; |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2285 | if ((ConvTemplate = dyn_cast<FunctionTemplateDecl>(D))) |
| 2286 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2287 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 2288 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2289 | |
| 2290 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2291 | if (ConvTemplate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2292 | S.AddTemplateConversionCandidate(ConvTemplate, FoundDecl, |
| 2293 | ActingContext, From, ToType, |
| 2294 | CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2295 | else |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2296 | S.AddConversionCandidate(Conv, FoundDecl, ActingContext, |
| 2297 | From, ToType, CandidateSet); |
Fariborz Jahanian | f9012a3 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2301 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2302 | |
| 2303 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2304 | switch (CandidateSet.BestViableFunction(S, From->getLocStart(), Best, true)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2305 | case OR_Success: |
| 2306 | // Record the standard conversion we used and the conversion function. |
| 2307 | if (CXXConstructorDecl *Constructor |
| 2308 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2309 | S.MarkDeclarationReferenced(From->getLocStart(), Constructor); |
| 2310 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2311 | // C++ [over.ics.user]p1: |
| 2312 | // If the user-defined conversion is specified by a |
| 2313 | // constructor (12.3.1), the initial standard conversion |
| 2314 | // sequence converts the source type to the type required by |
| 2315 | // the argument of the constructor. |
| 2316 | // |
| 2317 | QualType ThisType = Constructor->getThisType(S.Context); |
| 2318 | if (Best->Conversions[0].isEllipsis()) |
| 2319 | User.EllipsisConversion = true; |
| 2320 | else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2321 | User.Before = Best->Conversions[0].Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2322 | User.EllipsisConversion = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2323 | } |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2324 | User.ConversionFunction = Constructor; |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2325 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2326 | User.After.setAsIdentityConversion(); |
| 2327 | User.After.setFromType(ThisType->getAs<PointerType>()->getPointeeType()); |
| 2328 | User.After.setAllToTypes(ToType); |
| 2329 | return OR_Success; |
| 2330 | } else if (CXXConversionDecl *Conversion |
| 2331 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2332 | S.MarkDeclarationReferenced(From->getLocStart(), Conversion); |
| 2333 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2334 | // C++ [over.ics.user]p1: |
| 2335 | // |
| 2336 | // [...] If the user-defined conversion is specified by a |
| 2337 | // conversion function (12.3.2), the initial standard |
| 2338 | // conversion sequence converts the source type to the |
| 2339 | // implicit object parameter of the conversion function. |
| 2340 | User.Before = Best->Conversions[0].Standard; |
| 2341 | User.ConversionFunction = Conversion; |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2342 | User.FoundConversionFunction = Best->FoundDecl.getDecl(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2343 | User.EllipsisConversion = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2345 | // C++ [over.ics.user]p2: |
| 2346 | // The second standard conversion sequence converts the |
| 2347 | // result of the user-defined conversion to the target type |
| 2348 | // for the sequence. Since an implicit conversion sequence |
| 2349 | // is an initialization, the special rules for |
| 2350 | // initialization by user-defined conversion apply when |
| 2351 | // selecting the best user-defined conversion for a |
| 2352 | // user-defined conversion sequence (see 13.3.3 and |
| 2353 | // 13.3.3.1). |
| 2354 | User.After = Best->FinalConversion; |
| 2355 | return OR_Success; |
| 2356 | } else { |
| 2357 | llvm_unreachable("Not a constructor or conversion function?"); |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2358 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2361 | case OR_No_Viable_Function: |
| 2362 | return OR_No_Viable_Function; |
| 2363 | case OR_Deleted: |
| 2364 | // No conversion here! We're done. |
| 2365 | return OR_Deleted; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2366 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2367 | case OR_Ambiguous: |
| 2368 | return OR_Ambiguous; |
| 2369 | } |
| 2370 | |
Fariborz Jahanian | 3e6b57e | 2009-09-15 19:12:21 +0000 | [diff] [blame] | 2371 | return OR_No_Viable_Function; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2372 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2373 | |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2374 | bool |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2375 | Sema::DiagnoseMultipleUserDefinedConversion(Expr *From, QualType ToType) { |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2376 | ImplicitConversionSequence ICS; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2377 | OverloadCandidateSet CandidateSet(From->getExprLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2378 | OverloadingResult OvResult = |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2379 | IsUserDefinedConversion(*this, From, ToType, ICS.UserDefined, |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2380 | CandidateSet, false); |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 2381 | if (OvResult == OR_Ambiguous) |
| 2382 | Diag(From->getSourceRange().getBegin(), |
| 2383 | diag::err_typecheck_ambiguous_condition) |
| 2384 | << From->getType() << ToType << From->getSourceRange(); |
| 2385 | else if (OvResult == OR_No_Viable_Function && !CandidateSet.empty()) |
| 2386 | Diag(From->getSourceRange().getBegin(), |
| 2387 | diag::err_typecheck_nonviable_condition) |
| 2388 | << From->getType() << ToType << From->getSourceRange(); |
| 2389 | else |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2390 | return false; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2391 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &From, 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2392 | return true; |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 2393 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2394 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2395 | /// CompareImplicitConversionSequences - Compare two implicit |
| 2396 | /// conversion sequences to determine whether one is better than the |
| 2397 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2398 | static ImplicitConversionSequence::CompareKind |
| 2399 | CompareImplicitConversionSequences(Sema &S, |
| 2400 | const ImplicitConversionSequence& ICS1, |
| 2401 | const ImplicitConversionSequence& ICS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2402 | { |
| 2403 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 2404 | // conversion sequences (as defined in 13.3.3.1) |
| 2405 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 2406 | // conversion sequence than a user-defined conversion sequence or |
| 2407 | // an ellipsis conversion sequence, and |
| 2408 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 2409 | // conversion sequence than an ellipsis conversion sequence |
| 2410 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2411 | // |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2412 | // C++0x [over.best.ics]p10: |
| 2413 | // For the purpose of ranking implicit conversion sequences as |
| 2414 | // described in 13.3.3.2, the ambiguous conversion sequence is |
| 2415 | // treated as a user-defined sequence that is indistinguishable |
| 2416 | // from any other user-defined conversion sequence. |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2417 | if (ICS1.getKindRank() < ICS2.getKindRank()) |
| 2418 | return ImplicitConversionSequence::Better; |
| 2419 | else if (ICS2.getKindRank() < ICS1.getKindRank()) |
| 2420 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2421 | |
Benjamin Kramer | 98ff7f8 | 2010-04-18 12:05:54 +0000 | [diff] [blame] | 2422 | // The following checks require both conversion sequences to be of |
| 2423 | // the same kind. |
| 2424 | if (ICS1.getKind() != ICS2.getKind()) |
| 2425 | return ImplicitConversionSequence::Indistinguishable; |
| 2426 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2427 | // Two implicit conversion sequences of the same form are |
| 2428 | // indistinguishable conversion sequences unless one of the |
| 2429 | // following rules apply: (C++ 13.3.3.2p3): |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2430 | if (ICS1.isStandard()) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2431 | return CompareStandardConversionSequences(S, ICS1.Standard, ICS2.Standard); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2432 | else if (ICS1.isUserDefined()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2433 | // User-defined conversion sequence U1 is a better conversion |
| 2434 | // sequence than another user-defined conversion sequence U2 if |
| 2435 | // they contain the same user-defined conversion function or |
| 2436 | // constructor and if the second standard conversion sequence of |
| 2437 | // U1 is better than the second standard conversion sequence of |
| 2438 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2439 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2440 | ICS2.UserDefined.ConversionFunction) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2441 | return CompareStandardConversionSequences(S, |
| 2442 | ICS1.UserDefined.After, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2443 | ICS2.UserDefined.After); |
| 2444 | } |
| 2445 | |
| 2446 | return ImplicitConversionSequence::Indistinguishable; |
| 2447 | } |
| 2448 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2449 | static bool hasSimilarType(ASTContext &Context, QualType T1, QualType T2) { |
| 2450 | while (Context.UnwrapSimilarPointerTypes(T1, T2)) { |
| 2451 | Qualifiers Quals; |
| 2452 | T1 = Context.getUnqualifiedArrayType(T1, Quals); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2453 | T2 = Context.getUnqualifiedArrayType(T2, Quals); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2454 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2456 | return Context.hasSameUnqualifiedType(T1, T2); |
| 2457 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2458 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2459 | // Per 13.3.3.2p3, compare the given standard conversion sequences to |
| 2460 | // determine if one is a proper subset of the other. |
| 2461 | static ImplicitConversionSequence::CompareKind |
| 2462 | compareStandardConversionSubsets(ASTContext &Context, |
| 2463 | const StandardConversionSequence& SCS1, |
| 2464 | const StandardConversionSequence& SCS2) { |
| 2465 | ImplicitConversionSequence::CompareKind Result |
| 2466 | = ImplicitConversionSequence::Indistinguishable; |
| 2467 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2468 | // the identity conversion sequence is considered to be a subsequence of |
Douglas Gregor | e87561a | 2010-05-23 22:10:15 +0000 | [diff] [blame] | 2469 | // any non-identity conversion sequence |
| 2470 | if (SCS1.ReferenceBinding == SCS2.ReferenceBinding) { |
| 2471 | if (SCS1.isIdentityConversion() && !SCS2.isIdentityConversion()) |
| 2472 | return ImplicitConversionSequence::Better; |
| 2473 | else if (!SCS1.isIdentityConversion() && SCS2.isIdentityConversion()) |
| 2474 | return ImplicitConversionSequence::Worse; |
| 2475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2476 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2477 | if (SCS1.Second != SCS2.Second) { |
| 2478 | if (SCS1.Second == ICK_Identity) |
| 2479 | Result = ImplicitConversionSequence::Better; |
| 2480 | else if (SCS2.Second == ICK_Identity) |
| 2481 | Result = ImplicitConversionSequence::Worse; |
| 2482 | else |
| 2483 | return ImplicitConversionSequence::Indistinguishable; |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 2484 | } else if (!hasSimilarType(Context, SCS1.getToType(1), SCS2.getToType(1))) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2485 | return ImplicitConversionSequence::Indistinguishable; |
| 2486 | |
| 2487 | if (SCS1.Third == SCS2.Third) { |
| 2488 | return Context.hasSameType(SCS1.getToType(2), SCS2.getToType(2))? Result |
| 2489 | : ImplicitConversionSequence::Indistinguishable; |
| 2490 | } |
| 2491 | |
| 2492 | if (SCS1.Third == ICK_Identity) |
| 2493 | return Result == ImplicitConversionSequence::Worse |
| 2494 | ? ImplicitConversionSequence::Indistinguishable |
| 2495 | : ImplicitConversionSequence::Better; |
| 2496 | |
| 2497 | if (SCS2.Third == ICK_Identity) |
| 2498 | return Result == ImplicitConversionSequence::Better |
| 2499 | ? ImplicitConversionSequence::Indistinguishable |
| 2500 | : ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2501 | |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2502 | return ImplicitConversionSequence::Indistinguishable; |
| 2503 | } |
| 2504 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2505 | /// \brief Determine whether one of the given reference bindings is better |
| 2506 | /// than the other based on what kind of bindings they are. |
| 2507 | static bool isBetterReferenceBindingKind(const StandardConversionSequence &SCS1, |
| 2508 | const StandardConversionSequence &SCS2) { |
| 2509 | // C++0x [over.ics.rank]p3b4: |
| 2510 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 2511 | // implicit object parameter of a non-static member function declared |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2512 | // without a ref-qualifier, and *either* S1 binds an rvalue reference |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2513 | // to an rvalue and S2 binds an lvalue reference *or S1 binds an |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2514 | // lvalue reference to a function lvalue and S2 binds an rvalue |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2515 | // reference*. |
| 2516 | // |
| 2517 | // FIXME: Rvalue references. We're going rogue with the above edits, |
| 2518 | // because the semantics in the current C++0x working paper (N3225 at the |
| 2519 | // time of this writing) break the standard definition of std::forward |
| 2520 | // and std::reference_wrapper when dealing with references to functions. |
| 2521 | // Proposed wording changes submitted to CWG for consideration. |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 2522 | if (SCS1.BindsImplicitObjectArgumentWithoutRefQualifier || |
| 2523 | SCS2.BindsImplicitObjectArgumentWithoutRefQualifier) |
| 2524 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2525 | |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2526 | return (!SCS1.IsLvalueReference && SCS1.BindsToRvalue && |
| 2527 | SCS2.IsLvalueReference) || |
| 2528 | (SCS1.IsLvalueReference && SCS1.BindsToFunctionLvalue && |
| 2529 | !SCS2.IsLvalueReference); |
| 2530 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2531 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2532 | /// CompareStandardConversionSequences - Compare two standard |
| 2533 | /// conversion sequences to determine whether one is better than the |
| 2534 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2535 | static ImplicitConversionSequence::CompareKind |
| 2536 | CompareStandardConversionSequences(Sema &S, |
| 2537 | const StandardConversionSequence& SCS1, |
| 2538 | const StandardConversionSequence& SCS2) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2539 | { |
| 2540 | // Standard conversion sequence S1 is a better conversion sequence |
| 2541 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 2542 | |
| 2543 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 2544 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 2545 | // excluding any Lvalue Transformation; the identity conversion |
| 2546 | // sequence is considered to be a subsequence of any |
| 2547 | // non-identity conversion sequence) or, if not that, |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2548 | if (ImplicitConversionSequence::CompareKind CK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2549 | = compareStandardConversionSubsets(S.Context, SCS1, SCS2)) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2550 | return CK; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2551 | |
| 2552 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 2553 | // defined below), or, if not that, |
| 2554 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 2555 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 2556 | if (Rank1 < Rank2) |
| 2557 | return ImplicitConversionSequence::Better; |
| 2558 | else if (Rank2 < Rank1) |
| 2559 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2561 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 2562 | // are indistinguishable unless one of the following rules |
| 2563 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2565 | // A conversion that is not a conversion of a pointer, or |
| 2566 | // pointer to member, to bool is better than another conversion |
| 2567 | // that is such a conversion. |
| 2568 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 2569 | return SCS2.isPointerConversionToBool() |
| 2570 | ? ImplicitConversionSequence::Better |
| 2571 | : ImplicitConversionSequence::Worse; |
| 2572 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2573 | // C++ [over.ics.rank]p4b2: |
| 2574 | // |
| 2575 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2576 | // conversion of B* to A* is better than conversion of B* to |
| 2577 | // void*, and conversion of A* to void* is better than conversion |
| 2578 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | bool SCS1ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2580 | = SCS1.isPointerConversionToVoidPointer(S.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | bool SCS2ConvertsToVoid |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2582 | = SCS2.isPointerConversionToVoidPointer(S.Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2583 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 2584 | // Exactly one of the conversion sequences is a conversion to |
| 2585 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2586 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 2587 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2588 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 2589 | // Neither conversion sequence converts to a void pointer; compare |
| 2590 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2591 | if (ImplicitConversionSequence::CompareKind DerivedCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2592 | = CompareDerivedToBaseConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2593 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2594 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 2595 | // Both conversion sequences are conversions to void |
| 2596 | // pointers. Compare the source types to determine if there's an |
| 2597 | // inheritance relationship in their sources. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2598 | QualType FromType1 = SCS1.getFromType(); |
| 2599 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2600 | |
| 2601 | // Adjust the types we're converting from via the array-to-pointer |
| 2602 | // conversion, if we need to. |
| 2603 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2604 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2605 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2606 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2607 | |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2608 | QualType FromPointee1 |
| 2609 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
| 2610 | QualType FromPointee2 |
| 2611 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2612 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2613 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2614 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2615 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2616 | return ImplicitConversionSequence::Worse; |
| 2617 | |
| 2618 | // Objective-C++: If one interface is more specific than the |
| 2619 | // other, it is the better one. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2620 | const ObjCObjectType* FromIface1 = FromPointee1->getAs<ObjCObjectType>(); |
| 2621 | const ObjCObjectType* FromIface2 = FromPointee2->getAs<ObjCObjectType>(); |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2622 | if (FromIface1 && FromIface1) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2623 | if (S.Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2624 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2625 | else if (S.Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
Douglas Gregor | 1aa450a | 2009-12-13 21:37:05 +0000 | [diff] [blame] | 2626 | return ImplicitConversionSequence::Worse; |
| 2627 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2628 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2629 | |
| 2630 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 2631 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | if (ImplicitConversionSequence::CompareKind QualCK |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2633 | = CompareQualificationConversions(S, SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2634 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2636 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 2637 | // Check for a better reference binding based on the kind of bindings. |
| 2638 | if (isBetterReferenceBindingKind(SCS1, SCS2)) |
| 2639 | return ImplicitConversionSequence::Better; |
| 2640 | else if (isBetterReferenceBindingKind(SCS2, SCS1)) |
| 2641 | return ImplicitConversionSequence::Worse; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2642 | |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 2643 | // C++ [over.ics.rank]p3b4: |
| 2644 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 2645 | // which the references refer are the same type except for |
| 2646 | // top-level cv-qualifiers, and the type to which the reference |
| 2647 | // initialized by S2 refers is more cv-qualified than the type |
| 2648 | // to which the reference initialized by S1 refers. |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2649 | QualType T1 = SCS1.getToType(2); |
| 2650 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2651 | T1 = S.Context.getCanonicalType(T1); |
| 2652 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2653 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2654 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2655 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2656 | if (UnqualT1 == UnqualT2) { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 2657 | // If the type is an array type, promote the element qualifiers to the |
| 2658 | // type for comparison. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2659 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2660 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2661 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2662 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2663 | if (T2.isMoreQualifiedThan(T1)) |
| 2664 | return ImplicitConversionSequence::Better; |
| 2665 | else if (T1.isMoreQualifiedThan(T2)) |
| 2666 | return ImplicitConversionSequence::Worse; |
| 2667 | } |
| 2668 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2669 | |
| 2670 | return ImplicitConversionSequence::Indistinguishable; |
| 2671 | } |
| 2672 | |
| 2673 | /// CompareQualificationConversions - Compares two standard conversion |
| 2674 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 2676 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2677 | CompareQualificationConversions(Sema &S, |
| 2678 | const StandardConversionSequence& SCS1, |
| 2679 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 2680 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2681 | // -- S1 and S2 differ only in their qualification conversion and |
| 2682 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 2683 | // cv-qualification signature of type T1 is a proper subset of |
| 2684 | // the cv-qualification signature of type T2, and S1 is not the |
| 2685 | // deprecated string literal array-to-pointer conversion (4.2). |
| 2686 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 2687 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 2688 | return ImplicitConversionSequence::Indistinguishable; |
| 2689 | |
| 2690 | // FIXME: the example in the standard doesn't use a qualification |
| 2691 | // conversion (!) |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2692 | QualType T1 = SCS1.getToType(2); |
| 2693 | QualType T2 = SCS2.getToType(2); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2694 | T1 = S.Context.getCanonicalType(T1); |
| 2695 | T2 = S.Context.getCanonicalType(T2); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2696 | Qualifiers T1Quals, T2Quals; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2697 | QualType UnqualT1 = S.Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2698 | QualType UnqualT2 = S.Context.getUnqualifiedArrayType(T2, T2Quals); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2699 | |
| 2700 | // If the types are the same, we won't learn anything by unwrapped |
| 2701 | // them. |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2702 | if (UnqualT1 == UnqualT2) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2703 | return ImplicitConversionSequence::Indistinguishable; |
| 2704 | |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2705 | // If the type is an array type, promote the element qualifiers to the type |
| 2706 | // for comparison. |
| 2707 | if (isa<ArrayType>(T1) && T1Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2708 | T1 = S.Context.getQualifiedType(UnqualT1, T1Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2709 | if (isa<ArrayType>(T2) && T2Quals) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2710 | T2 = S.Context.getQualifiedType(UnqualT2, T2Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 2711 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2713 | = ImplicitConversionSequence::Indistinguishable; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2714 | while (S.Context.UnwrapSimilarPointerTypes(T1, T2)) { |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2715 | // Within each iteration of the loop, we check the qualifiers to |
| 2716 | // determine if this still looks like a qualification |
| 2717 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 2718 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2719 | // until there are no more pointers or pointers-to-members left |
| 2720 | // to unwrap. This essentially mimics what |
| 2721 | // IsQualificationConversion does, but here we're checking for a |
| 2722 | // strict subset of qualifiers. |
| 2723 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 2724 | // The qualifiers are the same, so this doesn't tell us anything |
| 2725 | // about how the sequences rank. |
| 2726 | ; |
| 2727 | else if (T2.isMoreQualifiedThan(T1)) { |
| 2728 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 2729 | if (Result == ImplicitConversionSequence::Worse) |
| 2730 | // Neither has qualifiers that are a subset of the other's |
| 2731 | // qualifiers. |
| 2732 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2734 | Result = ImplicitConversionSequence::Better; |
| 2735 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 2736 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 2737 | if (Result == ImplicitConversionSequence::Better) |
| 2738 | // Neither has qualifiers that are a subset of the other's |
| 2739 | // qualifiers. |
| 2740 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2742 | Result = ImplicitConversionSequence::Worse; |
| 2743 | } else { |
| 2744 | // Qualifiers are disjoint. |
| 2745 | return ImplicitConversionSequence::Indistinguishable; |
| 2746 | } |
| 2747 | |
| 2748 | // If the types after this point are equivalent, we're done. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2749 | if (S.Context.hasSameUnqualifiedType(T1, T2)) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2750 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2753 | // Check that the winning standard conversion sequence isn't using |
| 2754 | // the deprecated string literal array to pointer conversion. |
| 2755 | switch (Result) { |
| 2756 | case ImplicitConversionSequence::Better: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2757 | if (SCS1.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2758 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2759 | break; |
| 2760 | |
| 2761 | case ImplicitConversionSequence::Indistinguishable: |
| 2762 | break; |
| 2763 | |
| 2764 | case ImplicitConversionSequence::Worse: |
Douglas Gregor | e489a7d | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2765 | if (SCS2.DeprecatedStringLiteralToCharPtr) |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 2766 | Result = ImplicitConversionSequence::Indistinguishable; |
| 2767 | break; |
| 2768 | } |
| 2769 | |
| 2770 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2773 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 2774 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2775 | /// various kinds of derived-to-base conversions (C++ |
| 2776 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 2777 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2778 | ImplicitConversionSequence::CompareKind |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2779 | CompareDerivedToBaseConversions(Sema &S, |
| 2780 | const StandardConversionSequence& SCS1, |
| 2781 | const StandardConversionSequence& SCS2) { |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2782 | QualType FromType1 = SCS1.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2783 | QualType ToType1 = SCS1.getToType(1); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2784 | QualType FromType2 = SCS2.getFromType(); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 2785 | QualType ToType2 = SCS2.getToType(1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2786 | |
| 2787 | // Adjust the types we're converting from via the array-to-pointer |
| 2788 | // conversion, if we need to. |
| 2789 | if (SCS1.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2790 | FromType1 = S.Context.getArrayDecayedType(FromType1); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2791 | if (SCS2.First == ICK_Array_To_Pointer) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2792 | FromType2 = S.Context.getArrayDecayedType(FromType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2793 | |
| 2794 | // Canonicalize all of the types. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2795 | FromType1 = S.Context.getCanonicalType(FromType1); |
| 2796 | ToType1 = S.Context.getCanonicalType(ToType1); |
| 2797 | FromType2 = S.Context.getCanonicalType(FromType2); |
| 2798 | ToType2 = S.Context.getCanonicalType(ToType2); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2800 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2801 | // |
| 2802 | // If class B is derived directly or indirectly from class A and |
| 2803 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2804 | // |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2805 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 2807 | SCS2.Second == ICK_Pointer_Conversion && |
| 2808 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 2809 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 2810 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2812 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2814 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2815 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2816 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2817 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2818 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2820 | // -- 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] | 2821 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2822 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2823 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2824 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2825 | return ImplicitConversionSequence::Worse; |
| 2826 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2827 | |
| 2828 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 2829 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2830 | if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2831 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2832 | else if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2833 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } else if (SCS1.Second == ICK_Pointer_Conversion && |
| 2836 | SCS2.Second == ICK_Pointer_Conversion) { |
| 2837 | const ObjCObjectPointerType *FromPtr1 |
| 2838 | = FromType1->getAs<ObjCObjectPointerType>(); |
| 2839 | const ObjCObjectPointerType *FromPtr2 |
| 2840 | = FromType2->getAs<ObjCObjectPointerType>(); |
| 2841 | const ObjCObjectPointerType *ToPtr1 |
| 2842 | = ToType1->getAs<ObjCObjectPointerType>(); |
| 2843 | const ObjCObjectPointerType *ToPtr2 |
| 2844 | = ToType2->getAs<ObjCObjectPointerType>(); |
| 2845 | |
| 2846 | if (FromPtr1 && FromPtr2 && ToPtr1 && ToPtr2) { |
| 2847 | // Apply the same conversion ranking rules for Objective-C pointer types |
| 2848 | // that we do for C++ pointers to class types. However, we employ the |
| 2849 | // Objective-C pseudo-subtyping relationship used for assignment of |
| 2850 | // Objective-C pointer types. |
| 2851 | bool FromAssignLeft |
| 2852 | = S.Context.canAssignObjCInterfaces(FromPtr1, FromPtr2); |
| 2853 | bool FromAssignRight |
| 2854 | = S.Context.canAssignObjCInterfaces(FromPtr2, FromPtr1); |
| 2855 | bool ToAssignLeft |
| 2856 | = S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2); |
| 2857 | bool ToAssignRight |
| 2858 | = S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1); |
| 2859 | |
| 2860 | // A conversion to an a non-id object pointer type or qualified 'id' |
| 2861 | // type is better than a conversion to 'id'. |
| 2862 | if (ToPtr1->isObjCIdType() && |
| 2863 | (ToPtr2->isObjCQualifiedIdType() || ToPtr2->getInterfaceDecl())) |
| 2864 | return ImplicitConversionSequence::Worse; |
| 2865 | if (ToPtr2->isObjCIdType() && |
| 2866 | (ToPtr1->isObjCQualifiedIdType() || ToPtr1->getInterfaceDecl())) |
| 2867 | return ImplicitConversionSequence::Better; |
| 2868 | |
| 2869 | // A conversion to a non-id object pointer type is better than a |
| 2870 | // conversion to a qualified 'id' type |
| 2871 | if (ToPtr1->isObjCQualifiedIdType() && ToPtr2->getInterfaceDecl()) |
| 2872 | return ImplicitConversionSequence::Worse; |
| 2873 | if (ToPtr2->isObjCQualifiedIdType() && ToPtr1->getInterfaceDecl()) |
| 2874 | return ImplicitConversionSequence::Better; |
| 2875 | |
| 2876 | // A conversion to an a non-Class object pointer type or qualified 'Class' |
| 2877 | // type is better than a conversion to 'Class'. |
| 2878 | if (ToPtr1->isObjCClassType() && |
| 2879 | (ToPtr2->isObjCQualifiedClassType() || ToPtr2->getInterfaceDecl())) |
| 2880 | return ImplicitConversionSequence::Worse; |
| 2881 | if (ToPtr2->isObjCClassType() && |
| 2882 | (ToPtr1->isObjCQualifiedClassType() || ToPtr1->getInterfaceDecl())) |
| 2883 | return ImplicitConversionSequence::Better; |
| 2884 | |
| 2885 | // A conversion to a non-Class object pointer type is better than a |
| 2886 | // conversion to a qualified 'Class' type. |
| 2887 | if (ToPtr1->isObjCQualifiedClassType() && ToPtr2->getInterfaceDecl()) |
| 2888 | return ImplicitConversionSequence::Worse; |
| 2889 | if (ToPtr2->isObjCQualifiedClassType() && ToPtr1->getInterfaceDecl()) |
| 2890 | return ImplicitConversionSequence::Better; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2892 | // -- "conversion of C* to B* is better than conversion of C* to A*," |
| 2893 | if (S.Context.hasSameType(FromType1, FromType2) && |
| 2894 | !FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() && |
| 2895 | (ToAssignLeft != ToAssignRight)) |
| 2896 | return ToAssignLeft? ImplicitConversionSequence::Worse |
| 2897 | : ImplicitConversionSequence::Better; |
| 2898 | |
| 2899 | // -- "conversion of B* to A* is better than conversion of C* to A*," |
| 2900 | if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) && |
| 2901 | (FromAssignLeft != FromAssignRight)) |
| 2902 | return FromAssignLeft? ImplicitConversionSequence::Better |
| 2903 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2904 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2905 | } |
Douglas Gregor | 058d3de | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 2906 | |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2907 | // Ranking of member-pointer types. |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2908 | if (SCS1.Second == ICK_Pointer_Member && SCS2.Second == ICK_Pointer_Member && |
| 2909 | FromType1->isMemberPointerType() && FromType2->isMemberPointerType() && |
| 2910 | ToType1->isMemberPointerType() && ToType2->isMemberPointerType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2911 | const MemberPointerType * FromMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2912 | FromType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2913 | const MemberPointerType * ToMemPointer1 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2914 | ToType1->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2915 | const MemberPointerType * FromMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2916 | FromType2->getAs<MemberPointerType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2917 | const MemberPointerType * ToMemPointer2 = |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2918 | ToType2->getAs<MemberPointerType>(); |
| 2919 | const Type *FromPointeeType1 = FromMemPointer1->getClass(); |
| 2920 | const Type *ToPointeeType1 = ToMemPointer1->getClass(); |
| 2921 | const Type *FromPointeeType2 = FromMemPointer2->getClass(); |
| 2922 | const Type *ToPointeeType2 = ToMemPointer2->getClass(); |
| 2923 | QualType FromPointee1 = QualType(FromPointeeType1, 0).getUnqualifiedType(); |
| 2924 | QualType ToPointee1 = QualType(ToPointeeType1, 0).getUnqualifiedType(); |
| 2925 | QualType FromPointee2 = QualType(FromPointeeType2, 0).getUnqualifiedType(); |
| 2926 | QualType ToPointee2 = QualType(ToPointeeType2, 0).getUnqualifiedType(); |
Fariborz Jahanian | ac741ff | 2009-10-20 20:07:35 +0000 | [diff] [blame] | 2927 | // 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] | 2928 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2929 | if (S.IsDerivedFrom(ToPointee1, ToPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2930 | return ImplicitConversionSequence::Worse; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2931 | else if (S.IsDerivedFrom(ToPointee2, ToPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2932 | return ImplicitConversionSequence::Better; |
| 2933 | } |
| 2934 | // conversion of B::* to C::* is better than conversion of A::* to C::* |
| 2935 | if (ToPointee1 == ToPointee2 && FromPointee1 != FromPointee2) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2936 | if (S.IsDerivedFrom(FromPointee1, FromPointee2)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2937 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2938 | else if (S.IsDerivedFrom(FromPointee2, FromPointee1)) |
Fariborz Jahanian | 9a587b0 | 2009-10-20 20:04:46 +0000 | [diff] [blame] | 2939 | return ImplicitConversionSequence::Worse; |
| 2940 | } |
| 2941 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2942 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 2943 | if (SCS1.Second == ICK_Derived_To_Base) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2944 | // -- 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] | 2945 | // -- binding of an expression of type C to a reference of type |
| 2946 | // B& is better than binding an expression of type C to a |
| 2947 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2948 | if (S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2949 | !S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2950 | if (S.IsDerivedFrom(ToType1, ToType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2951 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2952 | else if (S.IsDerivedFrom(ToType2, ToType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2953 | return ImplicitConversionSequence::Worse; |
| 2954 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2955 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2956 | // -- 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] | 2957 | // -- binding of an expression of type B to a reference of type |
| 2958 | // A& is better than binding an expression of type C to a |
| 2959 | // reference of type A&, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2960 | if (!S.Context.hasSameUnqualifiedType(FromType1, FromType2) && |
| 2961 | S.Context.hasSameUnqualifiedType(ToType1, ToType2)) { |
| 2962 | if (S.IsDerivedFrom(FromType2, FromType1)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2963 | return ImplicitConversionSequence::Better; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2964 | else if (S.IsDerivedFrom(FromType1, FromType2)) |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2965 | return ImplicitConversionSequence::Worse; |
| 2966 | } |
| 2967 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 2969 | return ImplicitConversionSequence::Indistinguishable; |
| 2970 | } |
| 2971 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2972 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 2973 | /// determine whether they are reference-related, |
| 2974 | /// reference-compatible, reference-compatible with added |
| 2975 | /// qualification, or incompatible, for use in C++ initialization by |
| 2976 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 2977 | /// type, and the first type (T1) is the pointee type of the reference |
| 2978 | /// type being initialized. |
| 2979 | Sema::ReferenceCompareResult |
| 2980 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 2981 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2982 | bool &DerivedToBase, |
| 2983 | bool &ObjCConversion) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 2984 | assert(!OrigT1->isReferenceType() && |
| 2985 | "T1 must be the pointee type of the reference type"); |
| 2986 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
| 2987 | |
| 2988 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 2989 | QualType T2 = Context.getCanonicalType(OrigT2); |
| 2990 | Qualifiers T1Quals, T2Quals; |
| 2991 | QualType UnqualT1 = Context.getUnqualifiedArrayType(T1, T1Quals); |
| 2992 | QualType UnqualT2 = Context.getUnqualifiedArrayType(T2, T2Quals); |
| 2993 | |
| 2994 | // C++ [dcl.init.ref]p4: |
| 2995 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
| 2996 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
| 2997 | // T1 is a base class of T2. |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2998 | DerivedToBase = false; |
| 2999 | ObjCConversion = false; |
| 3000 | if (UnqualT1 == UnqualT2) { |
| 3001 | // Nothing to do. |
| 3002 | } else if (!RequireCompleteType(Loc, OrigT2, PDiag()) && |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3003 | IsDerivedFrom(UnqualT2, UnqualT1)) |
| 3004 | DerivedToBase = true; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3005 | else if (UnqualT1->isObjCObjectOrInterfaceType() && |
| 3006 | UnqualT2->isObjCObjectOrInterfaceType() && |
| 3007 | Context.canBindObjCObjectType(UnqualT1, UnqualT2)) |
| 3008 | ObjCConversion = true; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3009 | else |
| 3010 | return Ref_Incompatible; |
| 3011 | |
| 3012 | // At this point, we know that T1 and T2 are reference-related (at |
| 3013 | // least). |
| 3014 | |
| 3015 | // If the type is an array type, promote the element qualifiers to the type |
| 3016 | // for comparison. |
| 3017 | if (isa<ArrayType>(T1) && T1Quals) |
| 3018 | T1 = Context.getQualifiedType(UnqualT1, T1Quals); |
| 3019 | if (isa<ArrayType>(T2) && T2Quals) |
| 3020 | T2 = Context.getQualifiedType(UnqualT2, T2Quals); |
| 3021 | |
| 3022 | // C++ [dcl.init.ref]p4: |
| 3023 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
| 3024 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3025 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3026 | // overload resolution, cases for which cv1 is greater |
| 3027 | // cv-qualification than cv2 are identified as |
| 3028 | // reference-compatible with added qualification (see 13.3.3.2). |
| 3029 | if (T1Quals.getCVRQualifiers() == T2Quals.getCVRQualifiers()) |
| 3030 | return Ref_Compatible; |
| 3031 | else if (T1.isMoreQualifiedThan(T2)) |
| 3032 | return Ref_Compatible_With_Added_Qualification; |
| 3033 | else |
| 3034 | return Ref_Related; |
| 3035 | } |
| 3036 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3037 | /// \brief Look for a user-defined conversion to an value reference-compatible |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3038 | /// with DeclType. Return true if something definite is found. |
| 3039 | static bool |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3040 | FindConversionForRefInit(Sema &S, ImplicitConversionSequence &ICS, |
| 3041 | QualType DeclType, SourceLocation DeclLoc, |
| 3042 | Expr *Init, QualType T2, bool AllowRvalues, |
| 3043 | bool AllowExplicit) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3044 | assert(T2->isRecordType() && "Can only find conversions of record types."); |
| 3045 | CXXRecordDecl *T2RecordDecl |
| 3046 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
| 3047 | |
| 3048 | OverloadCandidateSet CandidateSet(DeclLoc); |
| 3049 | const UnresolvedSetImpl *Conversions |
| 3050 | = T2RecordDecl->getVisibleConversionFunctions(); |
| 3051 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 3052 | E = Conversions->end(); I != E; ++I) { |
| 3053 | NamedDecl *D = *I; |
| 3054 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3055 | if (isa<UsingShadowDecl>(D)) |
| 3056 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 3057 | |
| 3058 | FunctionTemplateDecl *ConvTemplate |
| 3059 | = dyn_cast<FunctionTemplateDecl>(D); |
| 3060 | CXXConversionDecl *Conv; |
| 3061 | if (ConvTemplate) |
| 3062 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3063 | else |
| 3064 | Conv = cast<CXXConversionDecl>(D); |
| 3065 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3066 | // If this is an explicit conversion, and we're not allowed to consider |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3067 | // explicit conversions, skip it. |
| 3068 | if (!AllowExplicit && Conv->isExplicit()) |
| 3069 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3071 | if (AllowRvalues) { |
| 3072 | bool DerivedToBase = false; |
| 3073 | bool ObjCConversion = false; |
| 3074 | if (!ConvTemplate && |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 3075 | S.CompareReferenceRelationship( |
| 3076 | DeclLoc, |
| 3077 | Conv->getConversionType().getNonReferenceType() |
| 3078 | .getUnqualifiedType(), |
| 3079 | DeclType.getNonReferenceType().getUnqualifiedType(), |
| 3080 | DerivedToBase, ObjCConversion) == |
| 3081 | Sema::Ref_Incompatible) |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3082 | continue; |
| 3083 | } else { |
| 3084 | // If the conversion function doesn't return a reference type, |
| 3085 | // it can't be considered for this conversion. An rvalue reference |
| 3086 | // is only acceptable if its referencee is a function type. |
| 3087 | |
| 3088 | const ReferenceType *RefType = |
| 3089 | Conv->getConversionType()->getAs<ReferenceType>(); |
| 3090 | if (!RefType || |
| 3091 | (!RefType->isLValueReferenceType() && |
| 3092 | !RefType->getPointeeType()->isFunctionType())) |
| 3093 | continue; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3095 | |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3096 | if (ConvTemplate) |
| 3097 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), ActingDC, |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3098 | Init, DeclType, CandidateSet); |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3099 | else |
| 3100 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, Init, |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3101 | DeclType, CandidateSet); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3105 | switch (CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3106 | case OR_Success: |
| 3107 | // C++ [over.ics.ref]p1: |
| 3108 | // |
| 3109 | // [...] If the parameter binds directly to the result of |
| 3110 | // applying a conversion function to the argument |
| 3111 | // expression, the implicit conversion sequence is a |
| 3112 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3113 | // second standard conversion sequence either an identity |
| 3114 | // conversion or, if the conversion function returns an |
| 3115 | // entity of a type that is a derived class of the parameter |
| 3116 | // type, a derived-to-base Conversion. |
| 3117 | if (!Best->FinalConversion.DirectBinding) |
| 3118 | return false; |
| 3119 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3120 | if (Best->Function) |
| 3121 | S.MarkDeclarationReferenced(DeclLoc, Best->Function); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3122 | ICS.setUserDefined(); |
| 3123 | ICS.UserDefined.Before = Best->Conversions[0].Standard; |
| 3124 | ICS.UserDefined.After = Best->FinalConversion; |
| 3125 | ICS.UserDefined.ConversionFunction = Best->Function; |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 3126 | ICS.UserDefined.FoundConversionFunction = Best->FoundDecl.getDecl(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3127 | ICS.UserDefined.EllipsisConversion = false; |
| 3128 | assert(ICS.UserDefined.After.ReferenceBinding && |
| 3129 | ICS.UserDefined.After.DirectBinding && |
| 3130 | "Expected a direct reference binding!"); |
| 3131 | return true; |
| 3132 | |
| 3133 | case OR_Ambiguous: |
| 3134 | ICS.setAmbiguous(); |
| 3135 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3136 | Cand != CandidateSet.end(); ++Cand) |
| 3137 | if (Cand->Viable) |
| 3138 | ICS.Ambiguous.addConversion(Cand->Function); |
| 3139 | return true; |
| 3140 | |
| 3141 | case OR_No_Viable_Function: |
| 3142 | case OR_Deleted: |
| 3143 | // There was no suitable conversion, or we found a deleted |
| 3144 | // conversion; continue with other checks. |
| 3145 | return false; |
| 3146 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3147 | |
Eric Christopher | aba9fb2 | 2010-06-30 18:36:32 +0000 | [diff] [blame] | 3148 | return false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3151 | /// \brief Compute an implicit conversion sequence for reference |
| 3152 | /// initialization. |
| 3153 | static ImplicitConversionSequence |
| 3154 | TryReferenceInit(Sema &S, Expr *&Init, QualType DeclType, |
| 3155 | SourceLocation DeclLoc, |
| 3156 | bool SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3157 | bool AllowExplicit) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3158 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3159 | |
| 3160 | // Most paths end in a failed conversion. |
| 3161 | ImplicitConversionSequence ICS; |
| 3162 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3163 | |
| 3164 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
| 3165 | QualType T2 = Init->getType(); |
| 3166 | |
| 3167 | // If the initializer is the address of an overloaded function, try |
| 3168 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3169 | // type of the resulting function. |
| 3170 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 3171 | DeclAccessPair Found; |
| 3172 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Init, DeclType, |
| 3173 | false, Found)) |
| 3174 | T2 = Fn->getType(); |
| 3175 | } |
| 3176 | |
| 3177 | // Compute some basic properties of the types and the initializer. |
| 3178 | bool isRValRef = DeclType->isRValueReferenceType(); |
| 3179 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3180 | bool ObjCConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3181 | Expr::Classification InitCategory = Init->Classify(S.Context); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3182 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3183 | = S.CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase, |
| 3184 | ObjCConversion); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3186 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3187 | // C++0x [dcl.init.ref]p5: |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3188 | // A reference to type "cv1 T1" is initialized by an expression |
| 3189 | // of type "cv2 T2" as follows: |
| 3190 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3191 | // -- If reference is an lvalue reference and the initializer expression |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3192 | if (!isRValRef) { |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3193 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3194 | // reference-compatible with "cv2 T2," or |
| 3195 | // |
| 3196 | // Per C++ [over.ics.ref]p4, we don't check the bit-field property here. |
| 3197 | if (InitCategory.isLValue() && |
| 3198 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3199 | // C++ [over.ics.ref]p1: |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3200 | // When a parameter of reference type binds directly (8.5.3) |
| 3201 | // to an argument expression, the implicit conversion sequence |
| 3202 | // is the identity conversion, unless the argument expression |
| 3203 | // has a type that is a derived class of the parameter type, |
| 3204 | // in which case the implicit conversion sequence is a |
| 3205 | // derived-to-base Conversion (13.3.3.1). |
| 3206 | ICS.setStandard(); |
| 3207 | ICS.Standard.First = ICK_Identity; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3208 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
| 3209 | : ObjCConversion? ICK_Compatible_Conversion |
| 3210 | : ICK_Identity; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3211 | ICS.Standard.Third = ICK_Identity; |
| 3212 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3213 | ICS.Standard.setToType(0, T2); |
| 3214 | ICS.Standard.setToType(1, T1); |
| 3215 | ICS.Standard.setToType(2, T1); |
| 3216 | ICS.Standard.ReferenceBinding = true; |
| 3217 | ICS.Standard.DirectBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3218 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3219 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3220 | ICS.Standard.BindsToRvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3221 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3222 | ICS.Standard.CopyConstructor = 0; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3223 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3224 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3225 | // derived-to-base conversions is suppressed when we're |
| 3226 | // computing the implicit conversion sequence (C++ |
| 3227 | // [over.best.ics]p2). |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3228 | return ICS; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3229 | } |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3230 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3231 | // -- has a class type (i.e., T2 is a class type), where T1 is |
| 3232 | // not reference-related to T2, and can be implicitly |
| 3233 | // converted to an lvalue of type "cv3 T3," where "cv1 T1" |
| 3234 | // is reference-compatible with "cv3 T3" 92) (this |
| 3235 | // conversion is selected by enumerating the applicable |
| 3236 | // conversion functions (13.3.1.6) and choosing the best |
| 3237 | // one through overload resolution (13.3)), |
| 3238 | if (!SuppressUserConversions && T2->isRecordType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3239 | !S.RequireCompleteType(DeclLoc, T2, 0) && |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3240 | RefRelationship == Sema::Ref_Incompatible) { |
Douglas Gregor | 836a7e8 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 3241 | if (FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3242 | Init, T2, /*AllowRvalues=*/false, |
| 3243 | AllowExplicit)) |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3244 | return ICS; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3245 | } |
| 3246 | } |
| 3247 | |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3248 | // -- Otherwise, the reference shall be an lvalue reference to a |
| 3249 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3250 | // shall be an rvalue reference. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3251 | // |
Douglas Gregor | 870f374 | 2010-04-18 09:22:00 +0000 | [diff] [blame] | 3252 | // We actually handle one oddity of C++ [over.ics.ref] at this |
| 3253 | // point, which is that, due to p2 (which short-circuits reference |
| 3254 | // binding by only attempting a simple conversion for non-direct |
| 3255 | // bindings) and p3's strange wording, we allow a const volatile |
| 3256 | // reference to bind to an rvalue. Hence the check for the presence |
| 3257 | // of "const" rather than checking for "const" being the only |
| 3258 | // qualifier. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3259 | // This is also the point where rvalue references and lvalue inits no longer |
| 3260 | // go together. |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3261 | if (!isRValRef && !T1.isConstQualified()) |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3262 | return ICS; |
| 3263 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3264 | // -- If the initializer expression |
| 3265 | // |
| 3266 | // -- is an xvalue, class prvalue, array prvalue or function |
| 3267 | // lvalue and "cv1T1" is reference-compatible with "cv2 T2", or |
| 3268 | if (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification && |
| 3269 | (InitCategory.isXValue() || |
| 3270 | (InitCategory.isPRValue() && (T2->isRecordType() || T2->isArrayType())) || |
| 3271 | (InitCategory.isLValue() && T2->isFunctionType()))) { |
| 3272 | ICS.setStandard(); |
| 3273 | ICS.Standard.First = ICK_Identity; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3274 | ICS.Standard.Second = DerivedToBase? ICK_Derived_To_Base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3275 | : ObjCConversion? ICK_Compatible_Conversion |
| 3276 | : ICK_Identity; |
| 3277 | ICS.Standard.Third = ICK_Identity; |
| 3278 | ICS.Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3279 | ICS.Standard.setToType(0, T2); |
| 3280 | ICS.Standard.setToType(1, T1); |
| 3281 | ICS.Standard.setToType(2, T1); |
| 3282 | ICS.Standard.ReferenceBinding = true; |
| 3283 | // In C++0x, this is always a direct binding. In C++98/03, it's a direct |
| 3284 | // binding unless we're binding to a class prvalue. |
| 3285 | // Note: Although xvalues wouldn't normally show up in C++98/03 code, we |
| 3286 | // allow the use of rvalue references in C++98/03 for the benefit of |
| 3287 | // standard library implementors; therefore, we need the xvalue check here. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | ICS.Standard.DirectBinding = |
| 3289 | S.getLangOptions().CPlusPlus0x || |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3290 | (InitCategory.isPRValue() && !T2->isRecordType()); |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3291 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3292 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3293 | ICS.Standard.BindsToRvalue = InitCategory.isRValue(); |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3294 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3295 | ICS.Standard.CopyConstructor = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | return ICS; |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3297 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3299 | // -- has a class type (i.e., T2 is a class type), where T1 is not |
| 3300 | // reference-related to T2, and can be implicitly converted to |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3301 | // an xvalue, class prvalue, or function lvalue of type |
| 3302 | // "cv3 T3", where "cv1 T1" is reference-compatible with |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3303 | // "cv3 T3", |
| 3304 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3305 | // then the reference is bound to the value of the initializer |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3306 | // expression in the first case and to the result of the conversion |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3307 | // in the second case (or, in either case, to an appropriate base |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3308 | // class subobject). |
| 3309 | if (!SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | T2->isRecordType() && !S.RequireCompleteType(DeclLoc, T2, 0) && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3311 | FindConversionForRefInit(S, ICS, DeclType, DeclLoc, |
| 3312 | Init, T2, /*AllowRvalues=*/true, |
| 3313 | AllowExplicit)) { |
| 3314 | // In the second case, if the reference is an rvalue reference |
| 3315 | // and the second standard conversion sequence of the |
| 3316 | // user-defined conversion sequence includes an lvalue-to-rvalue |
| 3317 | // conversion, the program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3318 | if (ICS.isUserDefined() && isRValRef && |
Douglas Gregor | f143cd5 | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 3319 | ICS.UserDefined.After.First == ICK_Lvalue_To_Rvalue) |
| 3320 | ICS.setBad(BadConversionSequence::no_conversion, Init, DeclType); |
| 3321 | |
Douglas Gregor | 95273c3 | 2011-01-21 16:36:05 +0000 | [diff] [blame] | 3322 | return ICS; |
Rafael Espindola | be468d9 | 2011-01-22 15:32:35 +0000 | [diff] [blame] | 3323 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3325 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
| 3326 | // initialized from the initializer expression using the |
| 3327 | // rules for a non-reference copy initialization (8.5). The |
| 3328 | // reference is then bound to the temporary. If T1 is |
| 3329 | // reference-related to T2, cv1 must be the same |
| 3330 | // cv-qualification as, or greater cv-qualification than, |
| 3331 | // cv2; otherwise, the program is ill-formed. |
| 3332 | if (RefRelationship == Sema::Ref_Related) { |
| 3333 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 3334 | // we would be reference-compatible or reference-compatible with |
| 3335 | // added qualification. But that wasn't the case, so the reference |
| 3336 | // initialization fails. |
| 3337 | return ICS; |
| 3338 | } |
| 3339 | |
| 3340 | // If at least one of the types is a class type, the types are not |
| 3341 | // related, and we aren't allowed any user conversions, the |
| 3342 | // reference binding fails. This case is important for breaking |
| 3343 | // recursion, since TryImplicitConversion below will attempt to |
| 3344 | // create a temporary through the use of a copy constructor. |
| 3345 | if (SuppressUserConversions && RefRelationship == Sema::Ref_Incompatible && |
| 3346 | (T1->isRecordType() || T2->isRecordType())) |
| 3347 | return ICS; |
| 3348 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3349 | // If T1 is reference-related to T2 and the reference is an rvalue |
| 3350 | // reference, the initializer expression shall not be an lvalue. |
| 3351 | if (RefRelationship >= Sema::Ref_Related && |
| 3352 | isRValRef && Init->Classify(S.Context).isLValue()) |
| 3353 | return ICS; |
| 3354 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3355 | // C++ [over.ics.ref]p2: |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3356 | // When a parameter of reference type is not bound directly to |
| 3357 | // an argument expression, the conversion sequence is the one |
| 3358 | // required to convert the argument expression to the |
| 3359 | // underlying type of the reference according to |
| 3360 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 3361 | // to copy-initializing a temporary of the underlying type with |
| 3362 | // the argument expression. Any difference in top-level |
| 3363 | // cv-qualification is subsumed by the initialization itself |
| 3364 | // and does not constitute a conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3365 | ICS = TryImplicitConversion(S, Init, T1, SuppressUserConversions, |
| 3366 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3367 | /*InOverloadResolution=*/false, |
| 3368 | /*CStyle=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3369 | |
| 3370 | // Of course, that's still a reference binding. |
| 3371 | if (ICS.isStandard()) { |
| 3372 | ICS.Standard.ReferenceBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3373 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3374 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3375 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3376 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3377 | } else if (ICS.isUserDefined()) { |
| 3378 | ICS.UserDefined.After.ReferenceBinding = true; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3379 | ICS.Standard.IsLvalueReference = !isRValRef; |
| 3380 | ICS.Standard.BindsToFunctionLvalue = T2->isFunctionType(); |
| 3381 | ICS.Standard.BindsToRvalue = true; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3382 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier = false; |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3383 | } |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3385 | return ICS; |
| 3386 | } |
| 3387 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3388 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 3389 | /// ToType from the expression From. Return the implicit conversion |
| 3390 | /// sequence required to pass this argument, which may be a bad |
| 3391 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3392 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Douglas Gregor | e81335c | 2010-04-16 18:00:29 +0000 | [diff] [blame] | 3393 | /// do not permit any user-defined conversion sequences. |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3394 | static ImplicitConversionSequence |
| 3395 | TryCopyInitialization(Sema &S, Expr *From, QualType ToType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | bool SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3397 | bool InOverloadResolution) { |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3398 | if (ToType->isReferenceType()) |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3399 | return TryReferenceInit(S, From, ToType, |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3400 | /*FIXME:*/From->getLocStart(), |
| 3401 | SuppressUserConversions, |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 3402 | /*AllowExplicit=*/false); |
Douglas Gregor | 38ae6ab | 2010-04-13 16:31:36 +0000 | [diff] [blame] | 3403 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3404 | return TryImplicitConversion(S, From, ToType, |
| 3405 | SuppressUserConversions, |
| 3406 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3407 | InOverloadResolution, |
| 3408 | /*CStyle=*/false); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3411 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 3412 | /// parameter of the given member function (@c Method) from the |
| 3413 | /// expression @p From. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3414 | static ImplicitConversionSequence |
| 3415 | TryObjectArgumentInitialization(Sema &S, QualType OrigFromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3416 | Expr::Classification FromClassification, |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3417 | CXXMethodDecl *Method, |
| 3418 | CXXRecordDecl *ActingContext) { |
| 3419 | QualType ClassType = S.Context.getTypeDeclType(ActingContext); |
Sebastian Redl | 931e0bd | 2009-11-18 20:55:52 +0000 | [diff] [blame] | 3420 | // [class.dtor]p2: A destructor can be invoked for a const, volatile or |
| 3421 | // const volatile object. |
| 3422 | unsigned Quals = isa<CXXDestructorDecl>(Method) ? |
| 3423 | Qualifiers::Const | Qualifiers::Volatile : Method->getTypeQualifiers(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3424 | QualType ImplicitParamType = S.Context.getCVRQualifiedType(ClassType, Quals); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3425 | |
| 3426 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 3427 | // to exit early. |
| 3428 | ImplicitConversionSequence ICS; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3429 | |
| 3430 | // We need to have an object of class type. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 3431 | QualType FromType = OrigFromType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3432 | if (const PointerType *PT = FromType->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3433 | FromType = PT->getPointeeType(); |
| 3434 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3435 | // When we had a pointer, it's implicitly dereferenced, so we |
| 3436 | // better have an lvalue. |
| 3437 | assert(FromClassification.isLValue()); |
| 3438 | } |
| 3439 | |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3440 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3442 | // C++0x [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3443 | // For non-static member functions, the type of the implicit object |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3444 | // parameter is |
| 3445 | // |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3446 | // - "lvalue reference to cv X" for functions declared without a |
| 3447 | // ref-qualifier or with the & ref-qualifier |
| 3448 | // - "rvalue reference to cv X" for functions declared with the && |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3449 | // ref-qualifier |
| 3450 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3451 | // where X is the class of which the function is a member and cv is the |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3452 | // cv-qualification on the member function declaration. |
| 3453 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | // However, when finding an implicit conversion sequence for the argument, we |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3455 | // are not allowed to create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3456 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 3457 | // reference binding here, that allows class rvalues to bind to |
| 3458 | // non-constant references. |
| 3459 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3460 | // First check the qualifiers. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3461 | QualType FromTypeCanon = S.Context.getCanonicalType(FromType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3462 | if (ImplicitParamType.getCVRQualifiers() |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3463 | != FromTypeCanon.getLocalCVRQualifiers() && |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3464 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromTypeCanon)) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3465 | ICS.setBad(BadConversionSequence::bad_qualifiers, |
| 3466 | OrigFromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3467 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3468 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3469 | |
| 3470 | // Check that we have either the same type or a derived type. It |
| 3471 | // affects the conversion rank. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3472 | QualType ClassTypeCanon = S.Context.getCanonicalType(ClassType); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3473 | ImplicitConversionKind SecondKind; |
| 3474 | if (ClassTypeCanon == FromTypeCanon.getLocalUnqualifiedType()) { |
| 3475 | SecondKind = ICK_Identity; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3476 | } else if (S.IsDerivedFrom(FromType, ClassType)) |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3477 | SecondKind = ICK_Derived_To_Base; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3478 | else { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3479 | ICS.setBad(BadConversionSequence::unrelated_class, |
| 3480 | FromType, ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3481 | return ICS; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3482 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3483 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3484 | // Check the ref-qualifier. |
| 3485 | switch (Method->getRefQualifier()) { |
| 3486 | case RQ_None: |
| 3487 | // Do nothing; we don't care about lvalueness or rvalueness. |
| 3488 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3490 | case RQ_LValue: |
| 3491 | if (!FromClassification.isLValue() && Quals != Qualifiers::Const) { |
| 3492 | // non-const lvalue reference cannot bind to an rvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | ICS.setBad(BadConversionSequence::lvalue_ref_to_rvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3494 | ImplicitParamType); |
| 3495 | return ICS; |
| 3496 | } |
| 3497 | break; |
| 3498 | |
| 3499 | case RQ_RValue: |
| 3500 | if (!FromClassification.isRValue()) { |
| 3501 | // rvalue reference cannot bind to an lvalue |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | ICS.setBad(BadConversionSequence::rvalue_ref_to_lvalue, FromType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3503 | ImplicitParamType); |
| 3504 | return ICS; |
| 3505 | } |
| 3506 | break; |
| 3507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3509 | // Success. Mark this as a reference binding. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3510 | ICS.setStandard(); |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3511 | ICS.Standard.setAsIdentityConversion(); |
| 3512 | ICS.Standard.Second = SecondKind; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3513 | ICS.Standard.setFromType(FromType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 3514 | ICS.Standard.setAllToTypes(ImplicitParamType); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3515 | ICS.Standard.ReferenceBinding = true; |
| 3516 | ICS.Standard.DirectBinding = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3517 | ICS.Standard.IsLvalueReference = Method->getRefQualifier() != RQ_RValue; |
Douglas Gregor | e696ebb | 2011-01-26 14:52:12 +0000 | [diff] [blame] | 3518 | ICS.Standard.BindsToFunctionLvalue = false; |
Douglas Gregor | e1a47c1 | 2011-01-26 19:41:18 +0000 | [diff] [blame] | 3519 | ICS.Standard.BindsToRvalue = FromClassification.isRValue(); |
| 3520 | ICS.Standard.BindsImplicitObjectArgumentWithoutRefQualifier |
| 3521 | = (Method->getRefQualifier() == RQ_None); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3522 | return ICS; |
| 3523 | } |
| 3524 | |
| 3525 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 3526 | /// the implicit object parameter for the given Method with the given |
| 3527 | /// expression. |
| 3528 | bool |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | Sema::PerformObjectArgumentInitialization(Expr *&From, |
| 3530 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3531 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3532 | CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3533 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3535 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3537 | Expr::Classification FromClassification; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3538 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3539 | FromRecordType = PT->getPointeeType(); |
| 3540 | DestType = Method->getThisType(Context); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3541 | FromClassification = Expr::Classification::makeSimpleLValue(); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3542 | } else { |
| 3543 | FromRecordType = From->getType(); |
| 3544 | DestType = ImplicitParamRecordType; |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3545 | FromClassification = From->Classify(Context); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3548 | // Note that we always use the true parent context when performing |
| 3549 | // the actual argument initialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3550 | ImplicitConversionSequence ICS |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3551 | = TryObjectArgumentInitialization(*this, From->getType(), FromClassification, |
| 3552 | Method, Method->getParent()); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3553 | if (ICS.isBad()) { |
| 3554 | if (ICS.Bad.Kind == BadConversionSequence::bad_qualifiers) { |
| 3555 | Qualifiers FromQs = FromRecordType.getQualifiers(); |
| 3556 | Qualifiers ToQs = DestType.getQualifiers(); |
| 3557 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 3558 | if (CVR) { |
| 3559 | Diag(From->getSourceRange().getBegin(), |
| 3560 | diag::err_member_function_call_bad_cvr) |
| 3561 | << Method->getDeclName() << FromRecordType << (CVR - 1) |
| 3562 | << From->getSourceRange(); |
| 3563 | Diag(Method->getLocation(), diag::note_previous_decl) |
| 3564 | << Method->getDeclName(); |
| 3565 | return true; |
| 3566 | } |
| 3567 | } |
| 3568 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3569 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3570 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 3571 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Argyrios Kyrtzidis | 9813d32 | 2010-11-16 08:04:45 +0000 | [diff] [blame] | 3572 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3574 | if (ICS.Standard.Second == ICK_Derived_To_Base) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3575 | return PerformObjectMemberConversion(From, Qualifier, FoundDecl, Method); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 3577 | if (!Context.hasSameType(From->getType(), DestType)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3578 | ImpCastExprToType(From, DestType, CK_NoOp, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3579 | From->getType()->isPointerType() ? VK_RValue : VK_LValue); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3580 | return false; |
| 3581 | } |
| 3582 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3583 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 3584 | /// expression From to bool (C++0x [conv]p3). |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3585 | static ImplicitConversionSequence |
| 3586 | TryContextuallyConvertToBool(Sema &S, Expr *From) { |
Douglas Gregor | 0bbe94d | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 3587 | // FIXME: This is pretty broken. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3588 | return TryImplicitConversion(S, From, S.Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 3589 | // FIXME: Are these flags correct? |
| 3590 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | /*AllowExplicit=*/true, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3592 | /*InOverloadResolution=*/false, |
| 3593 | /*CStyle=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 3597 | /// of the expression From to bool (C++0x [conv]p3). |
| 3598 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3599 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(*this, From); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3600 | if (!ICS.isBad()) |
| 3601 | return PerformImplicitConversion(From, Context.BoolTy, ICS, AA_Converting); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | |
Fariborz Jahanian | 7619741 | 2009-11-18 18:26:29 +0000 | [diff] [blame] | 3603 | if (!DiagnoseMultipleUserDefinedConversion(From, Context.BoolTy)) |
Fariborz Jahanian | f0647a5 | 2009-09-22 20:24:30 +0000 | [diff] [blame] | 3604 | return Diag(From->getSourceRange().getBegin(), |
| 3605 | diag::err_typecheck_bool_condition) |
| 3606 | << From->getType() << From->getSourceRange(); |
| 3607 | return true; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3608 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3609 | |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3610 | /// TryContextuallyConvertToObjCId - Attempt to contextually convert the |
| 3611 | /// expression From to 'id'. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3612 | static ImplicitConversionSequence |
| 3613 | TryContextuallyConvertToObjCId(Sema &S, Expr *From) { |
| 3614 | QualType Ty = S.Context.getObjCIdType(); |
| 3615 | return TryImplicitConversion(S, From, Ty, |
| 3616 | // FIXME: Are these flags correct? |
| 3617 | /*SuppressUserConversions=*/false, |
| 3618 | /*AllowExplicit=*/true, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3619 | /*InOverloadResolution=*/false, |
| 3620 | /*CStyle=*/false); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3621 | } |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3622 | |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3623 | /// PerformContextuallyConvertToObjCId - Perform a contextual conversion |
| 3624 | /// of the expression From to 'id'. |
| 3625 | bool Sema::PerformContextuallyConvertToObjCId(Expr *&From) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3626 | QualType Ty = Context.getObjCIdType(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3627 | ImplicitConversionSequence ICS = TryContextuallyConvertToObjCId(*this, From); |
Fariborz Jahanian | cac49a8 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 3628 | if (!ICS.isBad()) |
| 3629 | return PerformImplicitConversion(From, Ty, ICS, AA_Converting); |
| 3630 | return true; |
| 3631 | } |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3632 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3633 | /// \brief Attempt to convert the given expression to an integral or |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3634 | /// enumeration type. |
| 3635 | /// |
| 3636 | /// This routine will attempt to convert an expression of class type to an |
| 3637 | /// integral or enumeration type, if that class type only has a single |
| 3638 | /// conversion to an integral or enumeration type. |
| 3639 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3640 | /// \param Loc The source location of the construct that requires the |
| 3641 | /// conversion. |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3642 | /// |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3643 | /// \param FromE The expression we're converting from. |
| 3644 | /// |
| 3645 | /// \param NotIntDiag The diagnostic to be emitted if the expression does not |
| 3646 | /// have integral or enumeration type. |
| 3647 | /// |
| 3648 | /// \param IncompleteDiag The diagnostic to be emitted if the expression has |
| 3649 | /// incomplete class type. |
| 3650 | /// |
| 3651 | /// \param ExplicitConvDiag The diagnostic to be emitted if we're calling an |
| 3652 | /// explicit conversion function (because no implicit conversion functions |
| 3653 | /// were available). This is a recovery mode. |
| 3654 | /// |
| 3655 | /// \param ExplicitConvNote The note to be emitted with \p ExplicitConvDiag, |
| 3656 | /// showing which conversion was picked. |
| 3657 | /// |
| 3658 | /// \param AmbigDiag The diagnostic to be emitted if there is more than one |
| 3659 | /// conversion function that could convert to integral or enumeration type. |
| 3660 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | /// \param AmbigNote The note to be emitted with \p AmbigDiag for each |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3662 | /// usable conversion function. |
| 3663 | /// |
| 3664 | /// \param ConvDiag The diagnostic to be emitted if we are calling a conversion |
| 3665 | /// function, which may be an extension in this case. |
| 3666 | /// |
| 3667 | /// \returns The expression, converted to an integral or enumeration type if |
| 3668 | /// successful. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3669 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3670 | Sema::ConvertToIntegralOrEnumerationType(SourceLocation Loc, Expr *From, |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3671 | const PartialDiagnostic &NotIntDiag, |
| 3672 | const PartialDiagnostic &IncompleteDiag, |
| 3673 | const PartialDiagnostic &ExplicitConvDiag, |
| 3674 | const PartialDiagnostic &ExplicitConvNote, |
| 3675 | const PartialDiagnostic &AmbigDiag, |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3676 | const PartialDiagnostic &AmbigNote, |
| 3677 | const PartialDiagnostic &ConvDiag) { |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3678 | // We can't perform any more checking for type-dependent expressions. |
| 3679 | if (From->isTypeDependent()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3680 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3681 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3682 | // If the expression already has integral or enumeration type, we're golden. |
| 3683 | QualType T = From->getType(); |
| 3684 | if (T->isIntegralOrEnumerationType()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3685 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3686 | |
| 3687 | // FIXME: Check for missing '()' if T is a function type? |
| 3688 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3689 | // If we don't have a class type in C++, there's no way we can get an |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3690 | // expression of integral or enumeration type. |
| 3691 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 3692 | if (!RecordTy || !getLangOptions().CPlusPlus) { |
| 3693 | Diag(Loc, NotIntDiag) |
| 3694 | << T << From->getSourceRange(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3695 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3698 | // We must have a complete class type. |
| 3699 | if (RequireCompleteType(Loc, T, IncompleteDiag)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3700 | return Owned(From); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3701 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3702 | // Look for a conversion to an integral or enumeration type. |
| 3703 | UnresolvedSet<4> ViableConversions; |
| 3704 | UnresolvedSet<4> ExplicitConversions; |
| 3705 | const UnresolvedSetImpl *Conversions |
| 3706 | = cast<CXXRecordDecl>(RecordTy->getDecl())->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3707 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3708 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3709 | E = Conversions->end(); |
| 3710 | I != E; |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3711 | ++I) { |
| 3712 | if (CXXConversionDecl *Conversion |
| 3713 | = dyn_cast<CXXConversionDecl>((*I)->getUnderlyingDecl())) |
| 3714 | if (Conversion->getConversionType().getNonReferenceType() |
| 3715 | ->isIntegralOrEnumerationType()) { |
| 3716 | if (Conversion->isExplicit()) |
| 3717 | ExplicitConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3718 | else |
| 3719 | ViableConversions.addDecl(I.getDecl(), I.getAccess()); |
| 3720 | } |
| 3721 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3723 | switch (ViableConversions.size()) { |
| 3724 | case 0: |
| 3725 | if (ExplicitConversions.size() == 1) { |
| 3726 | DeclAccessPair Found = ExplicitConversions[0]; |
| 3727 | CXXConversionDecl *Conversion |
| 3728 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3729 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3730 | // The user probably meant to invoke the given explicit |
| 3731 | // conversion; use it. |
| 3732 | QualType ConvTy |
| 3733 | = Conversion->getConversionType().getNonReferenceType(); |
| 3734 | std::string TypeStr; |
| 3735 | ConvTy.getAsStringInternal(TypeStr, Context.PrintingPolicy); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3737 | Diag(Loc, ExplicitConvDiag) |
| 3738 | << T << ConvTy |
| 3739 | << FixItHint::CreateInsertion(From->getLocStart(), |
| 3740 | "static_cast<" + TypeStr + ">(") |
| 3741 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(From->getLocEnd()), |
| 3742 | ")"); |
| 3743 | Diag(Conversion->getLocation(), ExplicitConvNote) |
| 3744 | << ConvTy->isEnumeralType() << ConvTy; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | |
| 3746 | // If we aren't in a SFINAE context, build a call to the |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3747 | // explicit conversion function. |
| 3748 | if (isSFINAEContext()) |
| 3749 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3750 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3751 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3752 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, Conversion); |
| 3753 | if (Result.isInvalid()) |
| 3754 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3755 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3756 | From = Result.get(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3757 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3759 | // We'll complain below about a non-integral condition type. |
| 3760 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3761 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3762 | case 1: { |
| 3763 | // Apply this conversion. |
| 3764 | DeclAccessPair Found = ViableConversions[0]; |
| 3765 | CheckMemberOperatorAccess(From->getExprLoc(), From, 0, Found); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3767 | CXXConversionDecl *Conversion |
| 3768 | = cast<CXXConversionDecl>(Found->getUnderlyingDecl()); |
| 3769 | QualType ConvTy |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3771 | if (ConvDiag.getDiagID()) { |
| 3772 | if (isSFINAEContext()) |
| 3773 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3774 | |
Douglas Gregor | 4799d03 | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 3775 | Diag(Loc, ConvDiag) |
| 3776 | << T << ConvTy->isEnumeralType() << ConvTy << From->getSourceRange(); |
| 3777 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3779 | ExprResult Result = BuildCXXMemberCallExpr(From, Found, |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3780 | cast<CXXConversionDecl>(Found->getUnderlyingDecl())); |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3781 | if (Result.isInvalid()) |
| 3782 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 3784 | From = Result.get(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3785 | break; |
| 3786 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3787 | |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3788 | default: |
| 3789 | Diag(Loc, AmbigDiag) |
| 3790 | << T << From->getSourceRange(); |
| 3791 | for (unsigned I = 0, N = ViableConversions.size(); I != N; ++I) { |
| 3792 | CXXConversionDecl *Conv |
| 3793 | = cast<CXXConversionDecl>(ViableConversions[I]->getUnderlyingDecl()); |
| 3794 | QualType ConvTy = Conv->getConversionType().getNonReferenceType(); |
| 3795 | Diag(Conv->getLocation(), AmbigNote) |
| 3796 | << ConvTy->isEnumeralType() << ConvTy; |
| 3797 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3798 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | 5823da3 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 3801 | if (!From->getType()->isIntegralOrEnumerationType()) |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3802 | Diag(Loc, NotIntDiag) |
| 3803 | << From->getType() << From->getSourceRange(); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3804 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3805 | return Owned(From); |
Douglas Gregor | f4ea725 | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3808 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3809 | /// candidate functions, using the given function call arguments. If |
| 3810 | /// @p SuppressUserConversions, then don't allow user-defined |
| 3811 | /// conversions via constructors or conversion operators. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3812 | /// |
| 3813 | /// \para PartialOverloading true if we are performing "partial" overloading |
| 3814 | /// based on an incomplete set of function arguments. This feature is used by |
| 3815 | /// code completion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | void |
| 3817 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3818 | DeclAccessPair FoundDecl, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3819 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3820 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3821 | bool SuppressUserConversions, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3822 | bool PartialOverloading) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3823 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3824 | = dyn_cast<FunctionProtoType>(Function->getType()->getAs<FunctionType>()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3825 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | assert(!Function->getDescribedFunctionTemplate() && |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3827 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3828 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3829 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3830 | if (!isa<CXXConstructorDecl>(Method)) { |
| 3831 | // If we get here, it's because we're calling a member function |
| 3832 | // that is named without a member access expression (e.g., |
| 3833 | // "this->f") that was either written explicitly or created |
| 3834 | // implicitly. This can happen with a qualified call to a member |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3835 | // function, e.g., X::f(). We use an empty type for the implied |
| 3836 | // object argument (C++ [over.call.func]p3), and the acting context |
| 3837 | // is irrelevant. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3838 | AddMethodCandidate(Method, FoundDecl, Method->getParent(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3839 | QualType(), Expr::Classification::makeSimpleLValue(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3840 | Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3841 | SuppressUserConversions); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3842 | return; |
| 3843 | } |
| 3844 | // We treat a constructor like a non-member function, since its object |
| 3845 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 3848 | if (!CandidateSet.isNewCandidate(Function)) |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3849 | return; |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3850 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3851 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3852 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3853 | |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3854 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Function)){ |
| 3855 | // C++ [class.copy]p3: |
| 3856 | // A member function template is never instantiated to perform the copy |
| 3857 | // of a class object to an object of its class type. |
| 3858 | QualType ClassType = Context.getTypeDeclType(Constructor->getParent()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3859 | if (NumArgs == 1 && |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3860 | Constructor->isSpecializationCopyingObject() && |
Douglas Gregor | 901e717 | 2010-02-21 18:30:38 +0000 | [diff] [blame] | 3861 | (Context.hasSameUnqualifiedType(ClassType, Args[0]->getType()) || |
| 3862 | IsDerivedFrom(Args[0]->getType(), ClassType))) |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3863 | return; |
| 3864 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3865 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3866 | // Add this candidate |
| 3867 | CandidateSet.push_back(OverloadCandidate()); |
| 3868 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3869 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3870 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3871 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3872 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3873 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 3874 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3875 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3876 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3877 | |
| 3878 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 3879 | // parameters is viable only if it has an ellipsis in its parameter |
| 3880 | // list (8.3.5). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | if ((NumArgs + (PartialOverloading && NumArgs)) > NumArgsInProto && |
Douglas Gregor | 2a92001 | 2009-09-23 14:56:09 +0000 | [diff] [blame] | 3882 | !Proto->isVariadic()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3883 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3884 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3885 | return; |
| 3886 | } |
| 3887 | |
| 3888 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 3889 | // is viable only if the (m+1)st parameter has a default argument |
| 3890 | // (8.3.6). For the purposes of overload resolution, the |
| 3891 | // parameter list is truncated on the right, so that there are |
| 3892 | // exactly m parameters. |
| 3893 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3894 | if (NumArgs < MinRequiredArgs && !PartialOverloading) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3895 | // Not enough arguments. |
| 3896 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3897 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3898 | return; |
| 3899 | } |
| 3900 | |
| 3901 | // Determine the implicit conversion sequences for each of the |
| 3902 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3903 | Candidate.Conversions.resize(NumArgs); |
| 3904 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 3905 | if (ArgIdx < NumArgsInProto) { |
| 3906 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 3907 | // exist for each argument an implicit conversion sequence |
| 3908 | // (13.3.3.1) that converts that argument to the corresponding |
| 3909 | // parameter of F. |
| 3910 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 3912 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | SuppressUserConversions, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 3914 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3915 | if (Candidate.Conversions[ArgIdx].isBad()) { |
| 3916 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 3917 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3918 | break; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3919 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3920 | } else { |
| 3921 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 3922 | // argument for which there is no corresponding parameter is |
| 3923 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3924 | Candidate.Conversions[ArgIdx].setEllipsis(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | } |
| 3928 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3929 | /// \brief Add all of the function declarations in the given function set to |
| 3930 | /// the overload canddiate set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3931 | void Sema::AddFunctionCandidates(const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3932 | Expr **Args, unsigned NumArgs, |
| 3933 | OverloadCandidateSet& CandidateSet, |
| 3934 | bool SuppressUserConversions) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 3935 | for (UnresolvedSetIterator F = Fns.begin(), E = Fns.end(); F != E; ++F) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3936 | NamedDecl *D = F.getDecl()->getUnderlyingDecl(); |
| 3937 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3938 | if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3939 | AddMethodCandidate(cast<CXXMethodDecl>(FD), F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3940 | cast<CXXMethodDecl>(FD)->getParent(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3941 | Args[0]->getType(), Args[0]->Classify(Context), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3942 | Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3943 | CandidateSet, SuppressUserConversions); |
| 3944 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3945 | AddOverloadCandidate(FD, F.getPair(), Args, NumArgs, CandidateSet, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3946 | SuppressUserConversions); |
| 3947 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3948 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3949 | if (isa<CXXMethodDecl>(FunTmpl->getTemplatedDecl()) && |
| 3950 | !cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())->isStatic()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3951 | AddMethodTemplateCandidate(FunTmpl, F.getPair(), |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3952 | cast<CXXRecordDecl>(FunTmpl->getDeclContext()), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3953 | /*FIXME: explicit args */ 0, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | Args[0]->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3955 | Args[0]->Classify(Context), |
| 3956 | Args + 1, NumArgs - 1, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3957 | CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3958 | SuppressUserConversions); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3959 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3960 | AddTemplateOverloadCandidate(FunTmpl, F.getPair(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3961 | /*FIXME: explicit args */ 0, |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 3962 | Args, NumArgs, CandidateSet, |
| 3963 | SuppressUserConversions); |
| 3964 | } |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3965 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3968 | /// AddMethodCandidate - Adds a named decl (which is some kind of |
| 3969 | /// method) as a method candidate to the given overload set. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3970 | void Sema::AddMethodCandidate(DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3971 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3972 | Expr::Classification ObjectClassification, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3973 | Expr **Args, unsigned NumArgs, |
| 3974 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3975 | bool SuppressUserConversions) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3976 | NamedDecl *Decl = FoundDecl.getDecl(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 3977 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(Decl->getDeclContext()); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3978 | |
| 3979 | if (isa<UsingShadowDecl>(Decl)) |
| 3980 | Decl = cast<UsingShadowDecl>(Decl)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3981 | |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3982 | if (FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(Decl)) { |
| 3983 | assert(isa<CXXMethodDecl>(TD->getTemplatedDecl()) && |
| 3984 | "Expected a member function template"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3985 | AddMethodTemplateCandidate(TD, FoundDecl, ActingContext, |
| 3986 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3987 | ObjectType, ObjectClassification, Args, NumArgs, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3988 | CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3989 | SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3990 | } else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3991 | AddMethodCandidate(cast<CXXMethodDecl>(Decl), FoundDecl, ActingContext, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 3992 | ObjectType, ObjectClassification, Args, NumArgs, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 3993 | CandidateSet, SuppressUserConversions); |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 3994 | } |
| 3995 | } |
| 3996 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3997 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 3998 | /// of candidate functions, using the given function call arguments |
| 3999 | /// and the object argument (@c Object). For example, in a call |
| 4000 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 4001 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 4002 | /// allow user-defined conversions via constructors or conversion |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4003 | /// operators. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | void |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4005 | Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4006 | CXXRecordDecl *ActingContext, QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4007 | Expr::Classification ObjectClassification, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4008 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4009 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4010 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | const FunctionProtoType* Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4012 | = dyn_cast<FunctionProtoType>(Method->getType()->getAs<FunctionType>()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4013 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4014 | assert(!isa<CXXConstructorDecl>(Method) && |
| 4015 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4016 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4017 | if (!CandidateSet.isNewCandidate(Method)) |
| 4018 | return; |
| 4019 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4020 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4021 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4022 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4023 | // Add this candidate |
| 4024 | CandidateSet.push_back(OverloadCandidate()); |
| 4025 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4026 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4027 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4028 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4029 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4030 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4031 | |
| 4032 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4033 | |
| 4034 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4035 | // parameters is viable only if it has an ellipsis in its parameter |
| 4036 | // list (8.3.5). |
| 4037 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4038 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4039 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4040 | return; |
| 4041 | } |
| 4042 | |
| 4043 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 4044 | // is viable only if the (m+1)st parameter has a default argument |
| 4045 | // (8.3.6). For the purposes of overload resolution, the |
| 4046 | // parameter list is truncated on the right, so that there are |
| 4047 | // exactly m parameters. |
| 4048 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 4049 | if (NumArgs < MinRequiredArgs) { |
| 4050 | // Not enough arguments. |
| 4051 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4052 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4053 | return; |
| 4054 | } |
| 4055 | |
| 4056 | Candidate.Viable = true; |
| 4057 | Candidate.Conversions.resize(NumArgs + 1); |
| 4058 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4059 | if (Method->isStatic() || ObjectType.isNull()) |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4060 | // The implicit object argument is ignored. |
| 4061 | Candidate.IgnoreObjectArgument = true; |
| 4062 | else { |
| 4063 | // Determine the implicit conversion sequence for the object |
| 4064 | // parameter. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4065 | Candidate.Conversions[0] |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4066 | = TryObjectArgumentInitialization(*this, ObjectType, ObjectClassification, |
| 4067 | Method, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4068 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4069 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4070 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4071 | return; |
| 4072 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | // Determine the implicit conversion sequences for each of the |
| 4076 | // arguments. |
| 4077 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4078 | if (ArgIdx < NumArgsInProto) { |
| 4079 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4080 | // exist for each argument an implicit conversion sequence |
| 4081 | // (13.3.3.1) that converts that argument to the corresponding |
| 4082 | // parameter of F. |
| 4083 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4085 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4086 | SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 4087 | /*InOverloadResolution=*/true); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4088 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4089 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4090 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4091 | break; |
| 4092 | } |
| 4093 | } else { |
| 4094 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4095 | // argument for which there is no corresponding parameter is |
| 4096 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4097 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4098 | } |
| 4099 | } |
| 4100 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4102 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 4103 | /// set, using template argument deduction to produce an appropriate member |
| 4104 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4106 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4107 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4108 | CXXRecordDecl *ActingContext, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4109 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4110 | QualType ObjectType, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4111 | Expr::Classification ObjectClassification, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4112 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4113 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4114 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4115 | if (!CandidateSet.isNewCandidate(MethodTmpl)) |
| 4116 | return; |
| 4117 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4118 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4120 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | // 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] | 4122 | // candidate functions in the usual way.113) A given name can refer to one |
| 4123 | // or more function templates and also to a set of overloaded non-template |
| 4124 | // functions. In such a case, the candidate functions generated from each |
| 4125 | // function template are combined with the set of non-template candidate |
| 4126 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4127 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4128 | FunctionDecl *Specialization = 0; |
| 4129 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4130 | = DeduceTemplateArguments(MethodTmpl, ExplicitTemplateArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4131 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4132 | CandidateSet.push_back(OverloadCandidate()); |
| 4133 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4134 | Candidate.FoundDecl = FoundDecl; |
| 4135 | Candidate.Function = MethodTmpl->getTemplatedDecl(); |
| 4136 | Candidate.Viable = false; |
| 4137 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4138 | Candidate.IsSurrogate = false; |
| 4139 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4140 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4142 | Info); |
| 4143 | return; |
| 4144 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4145 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4146 | // Add the function template specialization produced by template argument |
| 4147 | // deduction as a candidate. |
| 4148 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4150 | "Specialization is not a member function?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4151 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), FoundDecl, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4152 | ActingContext, ObjectType, ObjectClassification, |
| 4153 | Args, NumArgs, CandidateSet, SuppressUserConversions); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4156 | /// \brief Add a C++ function template specialization as a candidate |
| 4157 | /// in the candidate set, using template argument deduction to produce |
| 4158 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4159 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4160 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4161 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 4162 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4163 | Expr **Args, unsigned NumArgs, |
| 4164 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4165 | bool SuppressUserConversions) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4166 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4167 | return; |
| 4168 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4169 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4171 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | // 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] | 4173 | // candidate functions in the usual way.113) A given name can refer to one |
| 4174 | // or more function templates and also to a set of overloaded non-template |
| 4175 | // functions. In such a case, the candidate functions generated from each |
| 4176 | // function template are combined with the set of non-template candidate |
| 4177 | // functions. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4178 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4179 | FunctionDecl *Specialization = 0; |
| 4180 | if (TemplateDeductionResult Result |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4181 | = DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4182 | Args, NumArgs, Specialization, Info)) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4183 | CandidateSet.push_back(OverloadCandidate()); |
| 4184 | OverloadCandidate &Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4185 | Candidate.FoundDecl = FoundDecl; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4186 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4187 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4188 | Candidate.FailureKind = ovl_fail_bad_deduction; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 4189 | Candidate.IsSurrogate = false; |
| 4190 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4191 | Candidate.ExplicitCallArguments = NumArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4193 | Info); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4194 | return; |
| 4195 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4197 | // Add the function template specialization produced by template argument |
| 4198 | // deduction as a candidate. |
| 4199 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4200 | AddOverloadCandidate(Specialization, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | f1e4669 | 2010-04-16 17:33:27 +0000 | [diff] [blame] | 4201 | SuppressUserConversions); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4202 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4204 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4206 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | /// 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] | 4208 | /// (which may or may not be the same type as the type that the |
| 4209 | /// conversion function produces). |
| 4210 | void |
| 4211 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4212 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4213 | CXXRecordDecl *ActingContext, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4214 | Expr *From, QualType ToType, |
| 4215 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4216 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 4217 | "Conversion function templates use AddTemplateConversionCandidate"); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4218 | QualType ConvType = Conversion->getConversionType().getNonReferenceType(); |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4219 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4220 | return; |
| 4221 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4222 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4223 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4224 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4225 | // Add this candidate |
| 4226 | CandidateSet.push_back(OverloadCandidate()); |
| 4227 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4228 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4229 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4230 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4231 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4232 | Candidate.FinalConversion.setAsIdentityConversion(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4233 | Candidate.FinalConversion.setFromType(ConvType); |
Douglas Gregor | 3edc4d5 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 4234 | Candidate.FinalConversion.setAllToTypes(ToType); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4235 | Candidate.Viable = true; |
| 4236 | Candidate.Conversions.resize(1); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4237 | Candidate.ExplicitCallArguments = 1; |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4238 | |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4239 | // C++ [over.match.funcs]p4: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4240 | // For conversion functions, the function is considered to be a member of |
| 4241 | // the class of the implicit implied object argument for the purpose of |
Douglas Gregor | 6affc78 | 2010-08-19 15:37:02 +0000 | [diff] [blame] | 4242 | // defining the type of the implicit object parameter. |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4243 | // |
| 4244 | // Determine the implicit conversion sequence for the implicit |
| 4245 | // object parameter. |
| 4246 | QualType ImplicitParamType = From->getType(); |
| 4247 | if (const PointerType *FromPtrType = ImplicitParamType->getAs<PointerType>()) |
| 4248 | ImplicitParamType = FromPtrType->getPointeeType(); |
| 4249 | CXXRecordDecl *ConversionContext |
| 4250 | = cast<CXXRecordDecl>(ImplicitParamType->getAs<RecordType>()->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4252 | Candidate.Conversions[0] |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | = TryObjectArgumentInitialization(*this, From->getType(), |
| 4254 | From->Classify(Context), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4255 | Conversion, ConversionContext); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4256 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4257 | if (Candidate.Conversions[0].isBad()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4258 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4259 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4260 | return; |
| 4261 | } |
Douglas Gregor | c9ed468 | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 4262 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | // We won't go through a user-define type conversion function to convert a |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4264 | // derived to base as such conversions are given Conversion Rank. They only |
| 4265 | // go through a copy constructor. 13.3.3.1.2-p4 [over.ics.user] |
| 4266 | QualType FromCanon |
| 4267 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 4268 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 4269 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
| 4270 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4271 | Candidate.FailureKind = ovl_fail_trivial_conversion; |
Fariborz Jahanian | 996a6aa | 2009-10-19 19:18:20 +0000 | [diff] [blame] | 4272 | return; |
| 4273 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4275 | // To determine what the conversion from the result of calling the |
| 4276 | // conversion function to the type we're eventually trying to |
| 4277 | // convert to (ToType), we need to synthesize a call to the |
| 4278 | // conversion function and attempt copy initialization from it. This |
| 4279 | // makes sure that we get the right semantics with respect to |
| 4280 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 4281 | // call on the stack and we don't need its arguments to be |
| 4282 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4284 | VK_LValue, From->getLocStart()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4285 | ImplicitCastExpr ConversionFn(ImplicitCastExpr::OnStack, |
| 4286 | Context.getPointerType(Conversion->getType()), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4287 | CK_FunctionToPointerDecay, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4288 | &ConversionRef, VK_RValue); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | 72ebdab | 2010-11-13 19:36:57 +0000 | [diff] [blame] | 4290 | QualType CallResultType |
| 4291 | = Conversion->getConversionType().getNonLValueExprType(Context); |
| 4292 | if (RequireCompleteType(From->getLocStart(), CallResultType, 0)) { |
| 4293 | Candidate.Viable = false; |
| 4294 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4295 | return; |
| 4296 | } |
| 4297 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4298 | ExprValueKind VK = Expr::getValueKindForType(Conversion->getConversionType()); |
| 4299 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | // 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] | 4301 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 4302 | // allocator). |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4303 | CallExpr Call(Context, &ConversionFn, 0, 0, CallResultType, VK, |
Douglas Gregor | e8f08012 | 2009-11-17 21:16:22 +0000 | [diff] [blame] | 4304 | From->getLocStart()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | ImplicitConversionSequence ICS = |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4306 | TryCopyInitialization(*this, &Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4307 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4308 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4310 | switch (ICS.getKind()) { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4311 | case ImplicitConversionSequence::StandardConversion: |
| 4312 | Candidate.FinalConversion = ICS.Standard; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4314 | // C++ [over.ics.user]p3: |
| 4315 | // If the user-defined conversion is specified by a specialization of a |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | // conversion function template, the second standard conversion sequence |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 4317 | // shall have exact match rank. |
| 4318 | if (Conversion->getPrimaryTemplate() && |
| 4319 | GetConversionRank(ICS.Standard.Second) != ICR_Exact_Match) { |
| 4320 | Candidate.Viable = false; |
| 4321 | Candidate.FailureKind = ovl_fail_final_conversion_not_exact; |
| 4322 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4324 | // C++0x [dcl.init.ref]p5: |
| 4325 | // In the second case, if the reference is an rvalue reference and |
| 4326 | // the second standard conversion sequence of the user-defined |
| 4327 | // conversion sequence includes an lvalue-to-rvalue conversion, the |
| 4328 | // program is ill-formed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | if (ToType->isRValueReferenceType() && |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 4330 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4331 | Candidate.Viable = false; |
| 4332 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
| 4333 | } |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4334 | break; |
| 4335 | |
| 4336 | case ImplicitConversionSequence::BadConversion: |
| 4337 | Candidate.Viable = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4338 | Candidate.FailureKind = ovl_fail_bad_final_conversion; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4339 | break; |
| 4340 | |
| 4341 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4342 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 4343 | "Can only end up with a standard conversion sequence or failure"); |
| 4344 | } |
| 4345 | } |
| 4346 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4347 | /// \brief Adds a conversion function template specialization |
| 4348 | /// candidate to the overload set, using template argument deduction |
| 4349 | /// to deduce the template arguments of the conversion function |
| 4350 | /// template from the type that we are converting to (C++ |
| 4351 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4352 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4353 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4354 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4355 | CXXRecordDecl *ActingDC, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4356 | Expr *From, QualType ToType, |
| 4357 | OverloadCandidateSet &CandidateSet) { |
| 4358 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 4359 | "Only conversion function templates permitted here"); |
| 4360 | |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4361 | if (!CandidateSet.isNewCandidate(FunctionTemplate)) |
| 4362 | return; |
| 4363 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4364 | TemplateDeductionInfo Info(Context, CandidateSet.getLocation()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4365 | CXXConversionDecl *Specialization = 0; |
| 4366 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4368 | Specialization, Info)) { |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4369 | CandidateSet.push_back(OverloadCandidate()); |
| 4370 | OverloadCandidate &Candidate = CandidateSet.back(); |
| 4371 | Candidate.FoundDecl = FoundDecl; |
| 4372 | Candidate.Function = FunctionTemplate->getTemplatedDecl(); |
| 4373 | Candidate.Viable = false; |
| 4374 | Candidate.FailureKind = ovl_fail_bad_deduction; |
| 4375 | Candidate.IsSurrogate = false; |
| 4376 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4377 | Candidate.ExplicitCallArguments = 1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result, |
Douglas Gregor | 90cf2c9 | 2010-05-08 20:18:54 +0000 | [diff] [blame] | 4379 | Info); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4380 | return; |
| 4381 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4382 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4383 | // Add the conversion function template specialization produced by |
| 4384 | // template argument deduction as a candidate. |
| 4385 | assert(Specialization && "Missing function template specialization?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4386 | AddConversionCandidate(Specialization, FoundDecl, ActingDC, From, ToType, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4387 | CandidateSet); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4390 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 4391 | /// converts the given @c Object to a function pointer via the |
| 4392 | /// conversion function @c Conversion, and then attempts to call it |
| 4393 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 4394 | /// the type of function that we'll eventually be calling. |
| 4395 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4396 | DeclAccessPair FoundDecl, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4397 | CXXRecordDecl *ActingContext, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4398 | const FunctionProtoType *Proto, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4399 | Expr *Object, |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4400 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4401 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 5b0f2a2 | 2009-09-28 04:47:19 +0000 | [diff] [blame] | 4402 | if (!CandidateSet.isNewCandidate(Conversion)) |
| 4403 | return; |
| 4404 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4405 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4406 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4408 | CandidateSet.push_back(OverloadCandidate()); |
| 4409 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4410 | Candidate.FoundDecl = FoundDecl; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4411 | Candidate.Function = 0; |
| 4412 | Candidate.Surrogate = Conversion; |
| 4413 | Candidate.Viable = true; |
| 4414 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4415 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4416 | Candidate.Conversions.resize(NumArgs + 1); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4417 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4418 | |
| 4419 | // Determine the implicit conversion sequence for the implicit |
| 4420 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4421 | ImplicitConversionSequence ObjectInit |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | = TryObjectArgumentInitialization(*this, Object->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4423 | Object->Classify(Context), |
| 4424 | Conversion, ActingContext); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4425 | if (ObjectInit.isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4426 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4427 | Candidate.FailureKind = ovl_fail_bad_conversion; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 4428 | Candidate.Conversions[0] = ObjectInit; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4429 | return; |
| 4430 | } |
| 4431 | |
| 4432 | // The first conversion is actually a user-defined conversion whose |
| 4433 | // first conversion is ObjectInit's standard conversion (which is |
| 4434 | // effectively a reference binding). Record it as such. |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4435 | Candidate.Conversions[0].setUserDefined(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4436 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
Fariborz Jahanian | 5582451 | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 4437 | Candidate.Conversions[0].UserDefined.EllipsisConversion = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4438 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | Candidate.Conversions[0].UserDefined.FoundConversionFunction |
Douglas Gregor | 2bbfba0 | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 4440 | = FoundDecl.getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4441 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4442 | = Candidate.Conversions[0].UserDefined.Before; |
| 4443 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 4444 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4446 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4447 | |
| 4448 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 4449 | // parameters is viable only if it has an ellipsis in its parameter |
| 4450 | // list (8.3.5). |
| 4451 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 4452 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4453 | Candidate.FailureKind = ovl_fail_too_many_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4454 | return; |
| 4455 | } |
| 4456 | |
| 4457 | // Function types don't have any default arguments, so just check if |
| 4458 | // we have enough arguments. |
| 4459 | if (NumArgs < NumArgsInProto) { |
| 4460 | // Not enough arguments. |
| 4461 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4462 | Candidate.FailureKind = ovl_fail_too_few_arguments; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4463 | return; |
| 4464 | } |
| 4465 | |
| 4466 | // Determine the implicit conversion sequences for each of the |
| 4467 | // arguments. |
| 4468 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 4469 | if (ArgIdx < NumArgsInProto) { |
| 4470 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 4471 | // exist for each argument an implicit conversion sequence |
| 4472 | // (13.3.3.1) that converts that argument to the corresponding |
| 4473 | // parameter of F. |
| 4474 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | Candidate.Conversions[ArgIdx + 1] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4476 | = TryCopyInitialization(*this, Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4477 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4478 | /*InOverloadResolution=*/false); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4479 | if (Candidate.Conversions[ArgIdx + 1].isBad()) { |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4480 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4481 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4482 | break; |
| 4483 | } |
| 4484 | } else { |
| 4485 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 4486 | // argument for which there is no corresponding parameter is |
| 4487 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4488 | Candidate.Conversions[ArgIdx + 1].setEllipsis(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | } |
| 4492 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4493 | /// \brief Add overload candidates for overloaded operators that are |
| 4494 | /// member functions. |
| 4495 | /// |
| 4496 | /// Add the overloaded operator candidates that are member functions |
| 4497 | /// for the operator Op that was used in an operator expression such |
| 4498 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 4499 | /// CandidateSet will store the added overload candidates. (C++ |
| 4500 | /// [over.match.oper]). |
| 4501 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 4502 | SourceLocation OpLoc, |
| 4503 | Expr **Args, unsigned NumArgs, |
| 4504 | OverloadCandidateSet& CandidateSet, |
| 4505 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4506 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4507 | |
| 4508 | // C++ [over.match.oper]p3: |
| 4509 | // For a unary operator @ with an operand of a type whose |
| 4510 | // cv-unqualified version is T1, and for a binary operator @ with |
| 4511 | // a left operand of a type whose cv-unqualified version is T1 and |
| 4512 | // a right operand of a type whose cv-unqualified version is T2, |
| 4513 | // three sets of candidate functions, designated member |
| 4514 | // candidates, non-member candidates and built-in candidates, are |
| 4515 | // constructed as follows: |
| 4516 | QualType T1 = Args[0]->getType(); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4517 | |
| 4518 | // -- If T1 is a class type, the set of member candidates is the |
| 4519 | // result of the qualified lookup of T1::operator@ |
| 4520 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 4521 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4522 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4523 | // Complete the type if it can be completed. Otherwise, we're done. |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4524 | if (RequireCompleteType(OpLoc, T1, PDiag())) |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4525 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4527 | LookupResult Operators(*this, OpName, OpLoc, LookupOrdinaryName); |
| 4528 | LookupQualifiedName(Operators, T1Rec->getDecl()); |
| 4529 | Operators.suppressDiagnostics(); |
| 4530 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4531 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 4532 | OperEnd = Operators.end(); |
| 4533 | Oper != OperEnd; |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4534 | ++Oper) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4535 | AddMethodCandidate(Oper.getPair(), Args[0]->getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4536 | Args[0]->Classify(Context), Args + 1, NumArgs - 1, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 4537 | CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 4538 | /* SuppressUserConversions = */ false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4539 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4542 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 4543 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 4544 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4545 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 4546 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4547 | /// operator. NumContextualBoolArguments is the number of arguments |
| 4548 | /// (at the beginning of the argument list) that will be contextually |
| 4549 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4550 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4551 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4552 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4553 | bool IsAssignmentOperator, |
| 4554 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4555 | // Overload resolution is always an unevaluated context. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4556 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4557 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4558 | // Add this candidate |
| 4559 | CandidateSet.push_back(OverloadCandidate()); |
| 4560 | OverloadCandidate& Candidate = CandidateSet.back(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4561 | Candidate.FoundDecl = DeclAccessPair::make(0, AS_none); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4562 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 4563 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4564 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4565 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 4566 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4567 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 4568 | |
| 4569 | // Determine the implicit conversion sequences for each of the |
| 4570 | // arguments. |
| 4571 | Candidate.Viable = true; |
| 4572 | Candidate.Conversions.resize(NumArgs); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 4573 | Candidate.ExplicitCallArguments = NumArgs; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4574 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4575 | // C++ [over.match.oper]p4: |
| 4576 | // For the built-in assignment operators, conversions of the |
| 4577 | // left operand are restricted as follows: |
| 4578 | // -- no temporaries are introduced to hold the left operand, and |
| 4579 | // -- no user-defined conversions are applied to the left |
| 4580 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 4582 | // |
| 4583 | // We block these conversions by turning off user-defined |
| 4584 | // conversions, since that is the only way that initialization of |
| 4585 | // a reference to a non-class type can occur from something that |
| 4586 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4587 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4589 | "Contextual conversion to bool requires bool type"); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4590 | Candidate.Conversions[ArgIdx] |
| 4591 | = TryContextuallyConvertToBool(*this, Args[ArgIdx]); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4592 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | Candidate.Conversions[ArgIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 4594 | = TryCopyInitialization(*this, Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 4595 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 4596 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4597 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4598 | if (Candidate.Conversions[ArgIdx].isBad()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4599 | Candidate.Viable = false; |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 4600 | Candidate.FailureKind = ovl_fail_bad_conversion; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 4601 | break; |
| 4602 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 4607 | /// candidate operator functions for built-in operators (C++ |
| 4608 | /// [over.built]). The types are separated into pointer types and |
| 4609 | /// enumeration types. |
| 4610 | class BuiltinCandidateTypeSet { |
| 4611 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4612 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4613 | |
| 4614 | /// PointerTypes - The set of pointer types that will be used in the |
| 4615 | /// built-in candidates. |
| 4616 | TypeSet PointerTypes; |
| 4617 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4618 | /// MemberPointerTypes - The set of member pointer types that will be |
| 4619 | /// used in the built-in candidates. |
| 4620 | TypeSet MemberPointerTypes; |
| 4621 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4622 | /// EnumerationTypes - The set of enumeration types that will be |
| 4623 | /// used in the built-in candidates. |
| 4624 | TypeSet EnumerationTypes; |
| 4625 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4626 | /// \brief The set of vector types that will be used in the built-in |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4627 | /// candidates. |
| 4628 | TypeSet VectorTypes; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4629 | |
| 4630 | /// \brief A flag indicating non-record types are viable candidates |
| 4631 | bool HasNonRecordTypes; |
| 4632 | |
| 4633 | /// \brief A flag indicating whether either arithmetic or enumeration types |
| 4634 | /// were present in the candidate set. |
| 4635 | bool HasArithmeticOrEnumeralTypes; |
| 4636 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4637 | /// Sema - The semantic analysis instance where we are building the |
| 4638 | /// candidate type set. |
| 4639 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4641 | /// Context - The AST context in which we will build the type sets. |
| 4642 | ASTContext &Context; |
| 4643 | |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4644 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4645 | const Qualifiers &VisibleQuals); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4646 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4647 | |
| 4648 | public: |
| 4649 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4650 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4651 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4653 | : HasNonRecordTypes(false), |
| 4654 | HasArithmeticOrEnumeralTypes(false), |
| 4655 | SemaRef(SemaRef), |
| 4656 | Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4657 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | void AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4659 | SourceLocation Loc, |
| 4660 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4661 | bool AllowExplicitConversions, |
| 4662 | const Qualifiers &VisibleTypeConversionsQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4663 | |
| 4664 | /// pointer_begin - First pointer type found; |
| 4665 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 4666 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4667 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4668 | iterator pointer_end() { return PointerTypes.end(); } |
| 4669 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4670 | /// member_pointer_begin - First member pointer type found; |
| 4671 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 4672 | |
| 4673 | /// member_pointer_end - Past the last member pointer type found; |
| 4674 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 4675 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4676 | /// enumeration_begin - First enumeration type found; |
| 4677 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 4678 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4679 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4680 | iterator enumeration_end() { return EnumerationTypes.end(); } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4682 | iterator vector_begin() { return VectorTypes.begin(); } |
| 4683 | iterator vector_end() { return VectorTypes.end(); } |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4684 | |
| 4685 | bool hasNonRecordTypes() { return HasNonRecordTypes; } |
| 4686 | bool hasArithmeticOrEnumeralTypes() { return HasArithmeticOrEnumeralTypes; } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4687 | }; |
| 4688 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4689 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4690 | /// the set of pointer types along with any more-qualified variants of |
| 4691 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4692 | /// will add "int const *", "int const volatile *", "int const |
| 4693 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4694 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4695 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4696 | /// |
| 4697 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4698 | bool |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4699 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, |
| 4700 | const Qualifiers &VisibleQuals) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4701 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4702 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4703 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4704 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4705 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4706 | QualType PointeeTy; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4707 | const PointerType *PointerTy = Ty->getAs<PointerType>(); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4708 | bool buildObjCPtr = false; |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4709 | if (!PointerTy) { |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4710 | if (const ObjCObjectPointerType *PTy = Ty->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4711 | PointeeTy = PTy->getPointeeType(); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4712 | buildObjCPtr = true; |
| 4713 | } |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4714 | else |
| 4715 | assert(false && "type was not a pointer type!"); |
| 4716 | } |
| 4717 | else |
| 4718 | PointeeTy = PointerTy->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4719 | |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4720 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4721 | // (the qualifier would sink to the element type), and for another, the |
| 4722 | // only overload situation where it matters is subscript or pointer +- int, |
| 4723 | // and those shouldn't have qualifier variants anyway. |
| 4724 | if (PointeeTy->isArrayType()) |
| 4725 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4726 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
Douglas Gregor | 4ef1d40 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4727 | if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) |
Fariborz Jahanian | facfdd4 | 2009-11-09 21:02:05 +0000 | [diff] [blame] | 4728 | BaseCVR = Array->getElementType().getCVRQualifiers(); |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4729 | bool hasVolatile = VisibleQuals.hasVolatile(); |
| 4730 | bool hasRestrict = VisibleQuals.hasRestrict(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4732 | // Iterate through all strict supersets of BaseCVR. |
| 4733 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4734 | if ((CVR | BaseCVR) != CVR) continue; |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4735 | // Skip over Volatile/Restrict if no Volatile/Restrict found anywhere |
| 4736 | // in the types. |
| 4737 | if ((CVR & Qualifiers::Volatile) && !hasVolatile) continue; |
| 4738 | if ((CVR & Qualifiers::Restrict) && !hasRestrict) continue; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4739 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Fariborz Jahanian | f2afc80 | 2010-08-21 17:11:09 +0000 | [diff] [blame] | 4740 | if (!buildObjCPtr) |
| 4741 | PointerTypes.insert(Context.getPointerType(QPointeeTy)); |
| 4742 | else |
| 4743 | PointerTypes.insert(Context.getObjCObjectPointerType(QPointeeTy)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | return true; |
| 4747 | } |
| 4748 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4749 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 4750 | /// to the set of pointer types along with any more-qualified variants of |
| 4751 | /// that type. For example, if @p Ty is "int const *", this routine |
| 4752 | /// will add "int const *", "int const volatile *", "int const |
| 4753 | /// restrict *", and "int const volatile restrict *" to the set of |
| 4754 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 4755 | /// false otherwise. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4756 | /// |
| 4757 | /// FIXME: what to do about extended qualifiers? |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4758 | bool |
| 4759 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 4760 | QualType Ty) { |
| 4761 | // Insert this type. |
| 4762 | if (!MemberPointerTypes.insert(Ty)) |
| 4763 | return false; |
| 4764 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4765 | const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>(); |
| 4766 | assert(PointerTy && "type was not a member pointer type!"); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4767 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4768 | QualType PointeeTy = PointerTy->getPointeeType(); |
Sebastian Redl | 4990a63 | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 4769 | // Don't add qualified variants of arrays. For one, they're not allowed |
| 4770 | // (the qualifier would sink to the element type), and for another, the |
| 4771 | // only overload situation where it matters is subscript or pointer +- int, |
| 4772 | // and those shouldn't have qualifier variants anyway. |
| 4773 | if (PointeeTy->isArrayType()) |
| 4774 | return true; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4775 | const Type *ClassTy = PointerTy->getClass(); |
| 4776 | |
| 4777 | // Iterate through all strict supersets of the pointee type's CVR |
| 4778 | // qualifiers. |
| 4779 | unsigned BaseCVR = PointeeTy.getCVRQualifiers(); |
| 4780 | for (unsigned CVR = BaseCVR+1; CVR <= Qualifiers::CVRMask; ++CVR) { |
| 4781 | if ((CVR | BaseCVR) != CVR) continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4782 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4783 | QualType QPointeeTy = Context.getCVRQualifiedType(PointeeTy, CVR); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 4784 | MemberPointerTypes.insert( |
| 4785 | Context.getMemberPointerType(QPointeeTy, ClassTy)); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | return true; |
| 4789 | } |
| 4790 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4791 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 4792 | /// 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] | 4793 | /// primarily interested in pointer types and enumeration types. We also |
| 4794 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4795 | /// AllowUserConversions is true if we should look at the conversion |
| 4796 | /// functions of a class type, and AllowExplicitConversions if we |
| 4797 | /// should also include the explicit conversion functions of a class |
| 4798 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4799 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4800 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4801 | SourceLocation Loc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4802 | bool AllowUserConversions, |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4803 | bool AllowExplicitConversions, |
| 4804 | const Qualifiers &VisibleQuals) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4805 | // Only deal with canonical types. |
| 4806 | Ty = Context.getCanonicalType(Ty); |
| 4807 | |
| 4808 | // Look through reference types; they aren't part of the type of an |
| 4809 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4810 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4811 | Ty = RefTy->getPointeeType(); |
| 4812 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4813 | // If we're dealing with an array type, decay to the pointer. |
| 4814 | if (Ty->isArrayType()) |
| 4815 | Ty = SemaRef.Context.getArrayDecayedType(Ty); |
| 4816 | |
| 4817 | // Otherwise, we don't care about qualifiers on the type. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4818 | Ty = Ty.getLocalUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4819 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4820 | // Flag if we ever add a non-record type. |
| 4821 | const RecordType *TyRec = Ty->getAs<RecordType>(); |
| 4822 | HasNonRecordTypes = HasNonRecordTypes || !TyRec; |
| 4823 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4824 | // Flag if we encounter an arithmetic type. |
| 4825 | HasArithmeticOrEnumeralTypes = |
| 4826 | HasArithmeticOrEnumeralTypes || Ty->isArithmeticType(); |
| 4827 | |
Fariborz Jahanian | e4151b5 | 2010-08-21 00:10:36 +0000 | [diff] [blame] | 4828 | if (Ty->isObjCIdType() || Ty->isObjCClassType()) |
| 4829 | PointerTypes.insert(Ty); |
| 4830 | else if (Ty->getAs<PointerType>() || Ty->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4831 | // Insert our type, and its more-qualified variants, into the set |
| 4832 | // of types. |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4833 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty, VisibleQuals)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4834 | return; |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 4835 | } else if (Ty->isMemberPointerType()) { |
| 4836 | // Member pointers are far easier, since the pointee can't be converted. |
| 4837 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 4838 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4839 | } else if (Ty->isEnumeralType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4840 | HasArithmeticOrEnumeralTypes = true; |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 4841 | EnumerationTypes.insert(Ty); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4842 | } else if (Ty->isVectorType()) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4843 | // We treat vector types as arithmetic types in many contexts as an |
| 4844 | // extension. |
| 4845 | HasArithmeticOrEnumeralTypes = true; |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 4846 | VectorTypes.insert(Ty); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4847 | } else if (AllowUserConversions && TyRec) { |
| 4848 | // No conversion functions in incomplete types. |
| 4849 | if (SemaRef.RequireCompleteType(Loc, Ty, 0)) |
| 4850 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4852 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 4853 | const UnresolvedSetImpl *Conversions |
| 4854 | = ClassDecl->getVisibleConversionFunctions(); |
| 4855 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
| 4856 | E = Conversions->end(); I != E; ++I) { |
| 4857 | NamedDecl *D = I.getDecl(); |
| 4858 | if (isa<UsingShadowDecl>(D)) |
| 4859 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4860 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4861 | // Skip conversion function templates; they don't tell us anything |
| 4862 | // about which builtin types we can convert to. |
| 4863 | if (isa<FunctionTemplateDecl>(D)) |
| 4864 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4865 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4866 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
| 4867 | if (AllowExplicitConversions || !Conv->isExplicit()) { |
| 4868 | AddTypesConvertedFrom(Conv->getConversionType(), Loc, false, false, |
| 4869 | VisibleQuals); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4870 | } |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4875 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 4876 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 4877 | /// given type to the candidate set. |
| 4878 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 4879 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4881 | unsigned NumArgs, |
| 4882 | OverloadCandidateSet &CandidateSet) { |
| 4883 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4885 | // T& operator=(T&, T) |
| 4886 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 4887 | ParamTypes[1] = T; |
| 4888 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 4889 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4891 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 4892 | // volatile T& operator=(volatile T&, T) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4893 | ParamTypes[0] |
| 4894 | = S.Context.getLValueReferenceType(S.Context.getVolatileType(T)); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4895 | ParamTypes[1] = T; |
| 4896 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4897 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 4898 | } |
| 4899 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4900 | |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4901 | /// CollectVRQualifiers - This routine returns Volatile/Restrict qualifiers, |
| 4902 | /// 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] | 4903 | static Qualifiers CollectVRQualifiers(ASTContext &Context, Expr* ArgExpr) { |
| 4904 | Qualifiers VRQuals; |
| 4905 | const RecordType *TyRec; |
| 4906 | if (const MemberPointerType *RHSMPType = |
| 4907 | ArgExpr->getType()->getAs<MemberPointerType>()) |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4908 | TyRec = RHSMPType->getClass()->getAs<RecordType>(); |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4909 | else |
| 4910 | TyRec = ArgExpr->getType()->getAs<RecordType>(); |
| 4911 | if (!TyRec) { |
Fariborz Jahanian | b06ec05 | 2009-10-16 22:08:05 +0000 | [diff] [blame] | 4912 | // Just to be safe, assume the worst case. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4913 | VRQuals.addVolatile(); |
| 4914 | VRQuals.addRestrict(); |
| 4915 | return VRQuals; |
| 4916 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4917 | |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4918 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4919 | if (!ClassDecl->hasDefinition()) |
| 4920 | return VRQuals; |
| 4921 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4922 | const UnresolvedSetImpl *Conversions = |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 4923 | ClassDecl->getVisibleConversionFunctions(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 4925 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4926 | E = Conversions->end(); I != E; ++I) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4927 | NamedDecl *D = I.getDecl(); |
| 4928 | if (isa<UsingShadowDecl>(D)) |
| 4929 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4930 | if (CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(D)) { |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4931 | QualType CanTy = Context.getCanonicalType(Conv->getConversionType()); |
| 4932 | if (const ReferenceType *ResTypeRef = CanTy->getAs<ReferenceType>()) |
| 4933 | CanTy = ResTypeRef->getPointeeType(); |
| 4934 | // Need to go down the pointer/mempointer chain and add qualifiers |
| 4935 | // as see them. |
| 4936 | bool done = false; |
| 4937 | while (!done) { |
| 4938 | if (const PointerType *ResTypePtr = CanTy->getAs<PointerType>()) |
| 4939 | CanTy = ResTypePtr->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | else if (const MemberPointerType *ResTypeMPtr = |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 4941 | CanTy->getAs<MemberPointerType>()) |
| 4942 | CanTy = ResTypeMPtr->getPointeeType(); |
| 4943 | else |
| 4944 | done = true; |
| 4945 | if (CanTy.isVolatileQualified()) |
| 4946 | VRQuals.addVolatile(); |
| 4947 | if (CanTy.isRestrictQualified()) |
| 4948 | VRQuals.addRestrict(); |
| 4949 | if (VRQuals.hasRestrict() && VRQuals.hasVolatile()) |
| 4950 | return VRQuals; |
| 4951 | } |
| 4952 | } |
| 4953 | } |
| 4954 | return VRQuals; |
| 4955 | } |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4956 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 4957 | namespace { |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4958 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 4959 | /// \brief Helper class to manage the addition of builtin operator overload |
| 4960 | /// candidates. It provides shared state and utility methods used throughout |
| 4961 | /// the process, as well as a helper method to add each group of builtin |
| 4962 | /// operator overloads from the standard to a candidate set. |
| 4963 | class BuiltinOperatorOverloadBuilder { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4964 | // Common instance state available to all overload candidate addition methods. |
| 4965 | Sema &S; |
| 4966 | Expr **Args; |
| 4967 | unsigned NumArgs; |
| 4968 | Qualifiers VisibleTypeConversionsQuals; |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 4969 | bool HasArithmeticOrEnumeralCandidateType; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4970 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes; |
| 4971 | OverloadCandidateSet &CandidateSet; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 4972 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4973 | // Define some constants used to index and iterate over the arithemetic types |
| 4974 | // provided via the getArithmeticType() method below. |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4975 | // The "promoted arithmetic types" are the arithmetic |
| 4976 | // types are that preserved by promotion (C++ [over.built]p2). |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4977 | static const unsigned FirstIntegralType = 3; |
| 4978 | static const unsigned LastIntegralType = 18; |
| 4979 | static const unsigned FirstPromotedIntegralType = 3, |
| 4980 | LastPromotedIntegralType = 9; |
| 4981 | static const unsigned FirstPromotedArithmeticType = 0, |
| 4982 | LastPromotedArithmeticType = 9; |
| 4983 | static const unsigned NumArithmeticTypes = 18; |
| 4984 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4985 | /// \brief Get the canonical type for a given arithmetic type index. |
| 4986 | CanQualType getArithmeticType(unsigned index) { |
| 4987 | assert(index < NumArithmeticTypes); |
| 4988 | static CanQualType ASTContext::* const |
| 4989 | ArithmeticTypes[NumArithmeticTypes] = { |
| 4990 | // Start of promoted types. |
| 4991 | &ASTContext::FloatTy, |
| 4992 | &ASTContext::DoubleTy, |
| 4993 | &ASTContext::LongDoubleTy, |
John McCall | 5287298 | 2010-11-13 05:51:15 +0000 | [diff] [blame] | 4994 | |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 4995 | // Start of integral types. |
| 4996 | &ASTContext::IntTy, |
| 4997 | &ASTContext::LongTy, |
| 4998 | &ASTContext::LongLongTy, |
| 4999 | &ASTContext::UnsignedIntTy, |
| 5000 | &ASTContext::UnsignedLongTy, |
| 5001 | &ASTContext::UnsignedLongLongTy, |
| 5002 | // End of promoted types. |
| 5003 | |
| 5004 | &ASTContext::BoolTy, |
| 5005 | &ASTContext::CharTy, |
| 5006 | &ASTContext::WCharTy, |
| 5007 | &ASTContext::Char16Ty, |
| 5008 | &ASTContext::Char32Ty, |
| 5009 | &ASTContext::SignedCharTy, |
| 5010 | &ASTContext::ShortTy, |
| 5011 | &ASTContext::UnsignedCharTy, |
| 5012 | &ASTContext::UnsignedShortTy, |
| 5013 | // End of integral types. |
| 5014 | // FIXME: What about complex? |
| 5015 | }; |
| 5016 | return S.Context.*ArithmeticTypes[index]; |
| 5017 | } |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5018 | |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5019 | /// \brief Gets the canonical type resulting from the usual arithemetic |
| 5020 | /// converions for the given arithmetic types. |
| 5021 | CanQualType getUsualArithmeticConversions(unsigned L, unsigned R) { |
| 5022 | // Accelerator table for performing the usual arithmetic conversions. |
| 5023 | // The rules are basically: |
| 5024 | // - if either is floating-point, use the wider floating-point |
| 5025 | // - if same signedness, use the higher rank |
| 5026 | // - if same size, use unsigned of the higher rank |
| 5027 | // - use the larger type |
| 5028 | // These rules, together with the axiom that higher ranks are |
| 5029 | // never smaller, are sufficient to precompute all of these results |
| 5030 | // *except* when dealing with signed types of higher rank. |
| 5031 | // (we could precompute SLL x UI for all known platforms, but it's |
| 5032 | // better not to make any assumptions). |
| 5033 | enum PromotedType { |
| 5034 | Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL, Dep=-1 |
| 5035 | }; |
| 5036 | static PromotedType ConversionsTable[LastPromotedArithmeticType] |
| 5037 | [LastPromotedArithmeticType] = { |
| 5038 | /* Flt*/ { Flt, Dbl, LDbl, Flt, Flt, Flt, Flt, Flt, Flt }, |
| 5039 | /* Dbl*/ { Dbl, Dbl, LDbl, Dbl, Dbl, Dbl, Dbl, Dbl, Dbl }, |
| 5040 | /*LDbl*/ { LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl, LDbl }, |
| 5041 | /* SI*/ { Flt, Dbl, LDbl, SI, SL, SLL, UI, UL, ULL }, |
| 5042 | /* SL*/ { Flt, Dbl, LDbl, SL, SL, SLL, Dep, UL, ULL }, |
| 5043 | /* SLL*/ { Flt, Dbl, LDbl, SLL, SLL, SLL, Dep, Dep, ULL }, |
| 5044 | /* UI*/ { Flt, Dbl, LDbl, UI, Dep, Dep, UI, UL, ULL }, |
| 5045 | /* UL*/ { Flt, Dbl, LDbl, UL, UL, Dep, UL, UL, ULL }, |
| 5046 | /* ULL*/ { Flt, Dbl, LDbl, ULL, ULL, ULL, ULL, ULL, ULL }, |
| 5047 | }; |
| 5048 | |
| 5049 | assert(L < LastPromotedArithmeticType); |
| 5050 | assert(R < LastPromotedArithmeticType); |
| 5051 | int Idx = ConversionsTable[L][R]; |
| 5052 | |
| 5053 | // Fast path: the table gives us a concrete answer. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5054 | if (Idx != Dep) return getArithmeticType(Idx); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5055 | |
| 5056 | // Slow path: we need to compare widths. |
| 5057 | // An invariant is that the signed type has higher rank. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5058 | CanQualType LT = getArithmeticType(L), |
| 5059 | RT = getArithmeticType(R); |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5060 | unsigned LW = S.Context.getIntWidth(LT), |
| 5061 | RW = S.Context.getIntWidth(RT); |
| 5062 | |
| 5063 | // If they're different widths, use the signed type. |
| 5064 | if (LW > RW) return LT; |
| 5065 | else if (LW < RW) return RT; |
| 5066 | |
| 5067 | // Otherwise, use the unsigned type of the signed type's rank. |
| 5068 | if (L == SL || R == SL) return S.Context.UnsignedLongTy; |
| 5069 | assert(L == SLL || R == SLL); |
| 5070 | return S.Context.UnsignedLongLongTy; |
| 5071 | } |
| 5072 | |
Chandler Carruth | 5659c0c | 2010-12-12 09:22:45 +0000 | [diff] [blame] | 5073 | /// \brief Helper method to factor out the common pattern of adding overloads |
| 5074 | /// for '++' and '--' builtin operators. |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5075 | void addPlusPlusMinusMinusStyleOverloads(QualType CandidateTy, |
| 5076 | bool HasVolatile) { |
| 5077 | QualType ParamTypes[2] = { |
| 5078 | S.Context.getLValueReferenceType(CandidateTy), |
| 5079 | S.Context.IntTy |
| 5080 | }; |
| 5081 | |
| 5082 | // Non-volatile version. |
| 5083 | if (NumArgs == 1) |
| 5084 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5085 | else |
| 5086 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5087 | |
| 5088 | // Use a heuristic to reduce number of builtin candidates in the set: |
| 5089 | // add volatile version only if there are conversions to a volatile type. |
| 5090 | if (HasVolatile) { |
| 5091 | ParamTypes[0] = |
| 5092 | S.Context.getLValueReferenceType( |
| 5093 | S.Context.getVolatileType(CandidateTy)); |
| 5094 | if (NumArgs == 1) |
| 5095 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 5096 | else |
| 5097 | S.AddBuiltinCandidate(CandidateTy, ParamTypes, Args, 2, CandidateSet); |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | public: |
| 5102 | BuiltinOperatorOverloadBuilder( |
| 5103 | Sema &S, Expr **Args, unsigned NumArgs, |
| 5104 | Qualifiers VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5105 | bool HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5106 | llvm::SmallVectorImpl<BuiltinCandidateTypeSet> &CandidateTypes, |
| 5107 | OverloadCandidateSet &CandidateSet) |
| 5108 | : S(S), Args(Args), NumArgs(NumArgs), |
| 5109 | VisibleTypeConversionsQuals(VisibleTypeConversionsQuals), |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5110 | HasArithmeticOrEnumeralCandidateType( |
| 5111 | HasArithmeticOrEnumeralCandidateType), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5112 | CandidateTypes(CandidateTypes), |
| 5113 | CandidateSet(CandidateSet) { |
| 5114 | // Validate some of our static helper constants in debug builds. |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5115 | assert(getArithmeticType(FirstPromotedIntegralType) == S.Context.IntTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5116 | "Invalid first promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5117 | assert(getArithmeticType(LastPromotedIntegralType - 1) |
| 5118 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5119 | "Invalid last promoted integral type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5120 | assert(getArithmeticType(FirstPromotedArithmeticType) |
| 5121 | == S.Context.FloatTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5122 | "Invalid first promoted arithmetic type"); |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5123 | assert(getArithmeticType(LastPromotedArithmeticType - 1) |
| 5124 | == S.Context.UnsignedLongLongTy && |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5125 | "Invalid last promoted arithmetic type"); |
| 5126 | } |
| 5127 | |
| 5128 | // C++ [over.built]p3: |
| 5129 | // |
| 5130 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 5131 | // is either volatile or empty, there exist candidate operator |
| 5132 | // functions of the form |
| 5133 | // |
| 5134 | // VQ T& operator++(VQ T&); |
| 5135 | // T operator++(VQ T&, int); |
| 5136 | // |
| 5137 | // C++ [over.built]p4: |
| 5138 | // |
| 5139 | // For every pair (T, VQ), where T is an arithmetic type other |
| 5140 | // than bool, and VQ is either volatile or empty, there exist |
| 5141 | // candidate operator functions of the form |
| 5142 | // |
| 5143 | // VQ T& operator--(VQ T&); |
| 5144 | // T operator--(VQ T&, int); |
| 5145 | void addPlusPlusMinusMinusArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5146 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5147 | return; |
| 5148 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5149 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
| 5150 | Arith < NumArithmeticTypes; ++Arith) { |
| 5151 | addPlusPlusMinusMinusStyleOverloads( |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5152 | getArithmeticType(Arith), |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5153 | VisibleTypeConversionsQuals.hasVolatile()); |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | // C++ [over.built]p5: |
| 5158 | // |
| 5159 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5160 | // cv-unqualified object type, and VQ is either volatile or |
| 5161 | // empty, there exist candidate operator functions of the form |
| 5162 | // |
| 5163 | // T*VQ& operator++(T*VQ&); |
| 5164 | // T*VQ& operator--(T*VQ&); |
| 5165 | // T* operator++(T*VQ&, int); |
| 5166 | // T* operator--(T*VQ&, int); |
| 5167 | void addPlusPlusMinusMinusPointerOverloads() { |
| 5168 | for (BuiltinCandidateTypeSet::iterator |
| 5169 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5170 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5171 | Ptr != PtrEnd; ++Ptr) { |
| 5172 | // Skip pointer types that aren't pointers to object types. |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5173 | if (!(*Ptr)->getPointeeType()->isObjectType()) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5174 | continue; |
| 5175 | |
| 5176 | addPlusPlusMinusMinusStyleOverloads(*Ptr, |
| 5177 | (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5178 | VisibleTypeConversionsQuals.hasVolatile())); |
| 5179 | } |
| 5180 | } |
| 5181 | |
| 5182 | // C++ [over.built]p6: |
| 5183 | // For every cv-qualified or cv-unqualified object type T, there |
| 5184 | // exist candidate operator functions of the form |
| 5185 | // |
| 5186 | // T& operator*(T*); |
| 5187 | // |
| 5188 | // C++ [over.built]p7: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5189 | // For every function type T that does not have cv-qualifiers or a |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5190 | // ref-qualifier, there exist candidate operator functions of the form |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5191 | // T& operator*(T*); |
| 5192 | void addUnaryStarPointerOverloads() { |
| 5193 | for (BuiltinCandidateTypeSet::iterator |
| 5194 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5195 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5196 | Ptr != PtrEnd; ++Ptr) { |
| 5197 | QualType ParamTy = *Ptr; |
| 5198 | QualType PointeeTy = ParamTy->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5199 | if (!PointeeTy->isObjectType() && !PointeeTy->isFunctionType()) |
| 5200 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5201 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 5202 | if (const FunctionProtoType *Proto =PointeeTy->getAs<FunctionProtoType>()) |
| 5203 | if (Proto->getTypeQuals() || Proto->getRefQualifier()) |
| 5204 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5205 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5206 | S.AddBuiltinCandidate(S.Context.getLValueReferenceType(PointeeTy), |
| 5207 | &ParamTy, Args, 1, CandidateSet); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | // C++ [over.built]p9: |
| 5212 | // For every promoted arithmetic type T, there exist candidate |
| 5213 | // operator functions of the form |
| 5214 | // |
| 5215 | // T operator+(T); |
| 5216 | // T operator-(T); |
| 5217 | void addUnaryPlusOrMinusArithmeticOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5218 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5219 | return; |
| 5220 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5221 | for (unsigned Arith = FirstPromotedArithmeticType; |
| 5222 | Arith < LastPromotedArithmeticType; ++Arith) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5223 | QualType ArithTy = getArithmeticType(Arith); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5224 | S.AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 5225 | } |
| 5226 | |
| 5227 | // Extension: We also add these operators for vector types. |
| 5228 | for (BuiltinCandidateTypeSet::iterator |
| 5229 | Vec = CandidateTypes[0].vector_begin(), |
| 5230 | VecEnd = CandidateTypes[0].vector_end(); |
| 5231 | Vec != VecEnd; ++Vec) { |
| 5232 | QualType VecTy = *Vec; |
| 5233 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5234 | } |
| 5235 | } |
| 5236 | |
| 5237 | // C++ [over.built]p8: |
| 5238 | // For every type T, there exist candidate operator functions of |
| 5239 | // the form |
| 5240 | // |
| 5241 | // T* operator+(T*); |
| 5242 | void addUnaryPlusPointerOverloads() { |
| 5243 | for (BuiltinCandidateTypeSet::iterator |
| 5244 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5245 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5246 | Ptr != PtrEnd; ++Ptr) { |
| 5247 | QualType ParamTy = *Ptr; |
| 5248 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 5249 | } |
| 5250 | } |
| 5251 | |
| 5252 | // C++ [over.built]p10: |
| 5253 | // For every promoted integral type T, there exist candidate |
| 5254 | // operator functions of the form |
| 5255 | // |
| 5256 | // T operator~(T); |
| 5257 | void addUnaryTildePromotedIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5258 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5259 | return; |
| 5260 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5261 | for (unsigned Int = FirstPromotedIntegralType; |
| 5262 | Int < LastPromotedIntegralType; ++Int) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5263 | QualType IntTy = getArithmeticType(Int); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5264 | S.AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 5265 | } |
| 5266 | |
| 5267 | // Extension: We also add this operator for vector types. |
| 5268 | for (BuiltinCandidateTypeSet::iterator |
| 5269 | Vec = CandidateTypes[0].vector_begin(), |
| 5270 | VecEnd = CandidateTypes[0].vector_end(); |
| 5271 | Vec != VecEnd; ++Vec) { |
| 5272 | QualType VecTy = *Vec; |
| 5273 | S.AddBuiltinCandidate(VecTy, &VecTy, Args, 1, CandidateSet); |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | // C++ [over.match.oper]p16: |
| 5278 | // For every pointer to member type T, there exist candidate operator |
| 5279 | // functions of the form |
| 5280 | // |
| 5281 | // bool operator==(T,T); |
| 5282 | // bool operator!=(T,T); |
| 5283 | void addEqualEqualOrNotEqualMemberPointerOverloads() { |
| 5284 | /// Set of (canonical) types that we've already handled. |
| 5285 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5286 | |
| 5287 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5288 | for (BuiltinCandidateTypeSet::iterator |
| 5289 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5290 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5291 | MemPtr != MemPtrEnd; |
| 5292 | ++MemPtr) { |
| 5293 | // Don't add the same builtin candidate twice. |
| 5294 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5295 | continue; |
| 5296 | |
| 5297 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5298 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5299 | CandidateSet); |
| 5300 | } |
| 5301 | } |
| 5302 | } |
| 5303 | |
| 5304 | // C++ [over.built]p15: |
| 5305 | // |
| 5306 | // For every pointer or enumeration type T, there exist |
| 5307 | // candidate operator functions of the form |
| 5308 | // |
| 5309 | // bool operator<(T, T); |
| 5310 | // bool operator>(T, T); |
| 5311 | // bool operator<=(T, T); |
| 5312 | // bool operator>=(T, T); |
| 5313 | // bool operator==(T, T); |
| 5314 | // bool operator!=(T, T); |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5315 | void addRelationalPointerOrEnumeralOverloads() { |
| 5316 | // C++ [over.built]p1: |
| 5317 | // If there is a user-written candidate with the same name and parameter |
| 5318 | // types as a built-in candidate operator function, the built-in operator |
| 5319 | // function is hidden and is not included in the set of candidate |
| 5320 | // functions. |
| 5321 | // |
| 5322 | // The text is actually in a note, but if we don't implement it then we end |
| 5323 | // up with ambiguities when the user provides an overloaded operator for |
| 5324 | // an enumeration type. Note that only enumeration types have this problem, |
| 5325 | // so we track which enumeration types we've seen operators for. Also, the |
| 5326 | // only other overloaded operator with enumeration argumenst, operator=, |
| 5327 | // cannot be overloaded for enumeration types, so this is the only place |
| 5328 | // where we must suppress candidates like this. |
| 5329 | llvm::DenseSet<std::pair<CanQualType, CanQualType> > |
| 5330 | UserDefinedBinaryOperators; |
| 5331 | |
| 5332 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5333 | if (CandidateTypes[ArgIdx].enumeration_begin() != |
| 5334 | CandidateTypes[ArgIdx].enumeration_end()) { |
| 5335 | for (OverloadCandidateSet::iterator C = CandidateSet.begin(), |
| 5336 | CEnd = CandidateSet.end(); |
| 5337 | C != CEnd; ++C) { |
| 5338 | if (!C->Viable || !C->Function || C->Function->getNumParams() != 2) |
| 5339 | continue; |
| 5340 | |
| 5341 | QualType FirstParamType = |
| 5342 | C->Function->getParamDecl(0)->getType().getUnqualifiedType(); |
| 5343 | QualType SecondParamType = |
| 5344 | C->Function->getParamDecl(1)->getType().getUnqualifiedType(); |
| 5345 | |
| 5346 | // Skip if either parameter isn't of enumeral type. |
| 5347 | if (!FirstParamType->isEnumeralType() || |
| 5348 | !SecondParamType->isEnumeralType()) |
| 5349 | continue; |
| 5350 | |
| 5351 | // Add this operator to the set of known user-defined operators. |
| 5352 | UserDefinedBinaryOperators.insert( |
| 5353 | std::make_pair(S.Context.getCanonicalType(FirstParamType), |
| 5354 | S.Context.getCanonicalType(SecondParamType))); |
| 5355 | } |
| 5356 | } |
| 5357 | } |
| 5358 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5359 | /// Set of (canonical) types that we've already handled. |
| 5360 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5361 | |
| 5362 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5363 | for (BuiltinCandidateTypeSet::iterator |
| 5364 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5365 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5366 | Ptr != PtrEnd; ++Ptr) { |
| 5367 | // Don't add the same builtin candidate twice. |
| 5368 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5369 | continue; |
| 5370 | |
| 5371 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5372 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5373 | CandidateSet); |
| 5374 | } |
| 5375 | for (BuiltinCandidateTypeSet::iterator |
| 5376 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5377 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5378 | Enum != EnumEnd; ++Enum) { |
| 5379 | CanQualType CanonType = S.Context.getCanonicalType(*Enum); |
| 5380 | |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5381 | // Don't add the same builtin candidate twice, or if a user defined |
| 5382 | // candidate exists. |
| 5383 | if (!AddedTypes.insert(CanonType) || |
| 5384 | UserDefinedBinaryOperators.count(std::make_pair(CanonType, |
| 5385 | CanonType))) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5386 | continue; |
| 5387 | |
| 5388 | QualType ParamTypes[2] = { *Enum, *Enum }; |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 5389 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, |
| 5390 | CandidateSet); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5391 | } |
| 5392 | } |
| 5393 | } |
| 5394 | |
| 5395 | // C++ [over.built]p13: |
| 5396 | // |
| 5397 | // For every cv-qualified or cv-unqualified object type T |
| 5398 | // there exist candidate operator functions of the form |
| 5399 | // |
| 5400 | // T* operator+(T*, ptrdiff_t); |
| 5401 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 5402 | // T* operator-(T*, ptrdiff_t); |
| 5403 | // T* operator+(ptrdiff_t, T*); |
| 5404 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 5405 | // |
| 5406 | // C++ [over.built]p14: |
| 5407 | // |
| 5408 | // For every T, where T is a pointer to object type, there |
| 5409 | // exist candidate operator functions of the form |
| 5410 | // |
| 5411 | // ptrdiff_t operator-(T, T); |
| 5412 | void addBinaryPlusOrMinusPointerOverloads(OverloadedOperatorKind Op) { |
| 5413 | /// Set of (canonical) types that we've already handled. |
| 5414 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5415 | |
| 5416 | for (int Arg = 0; Arg < 2; ++Arg) { |
| 5417 | QualType AsymetricParamTypes[2] = { |
| 5418 | S.Context.getPointerDiffType(), |
| 5419 | S.Context.getPointerDiffType(), |
| 5420 | }; |
| 5421 | for (BuiltinCandidateTypeSet::iterator |
| 5422 | Ptr = CandidateTypes[Arg].pointer_begin(), |
| 5423 | PtrEnd = CandidateTypes[Arg].pointer_end(); |
| 5424 | Ptr != PtrEnd; ++Ptr) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5425 | QualType PointeeTy = (*Ptr)->getPointeeType(); |
| 5426 | if (!PointeeTy->isObjectType()) |
| 5427 | continue; |
| 5428 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5429 | AsymetricParamTypes[Arg] = *Ptr; |
| 5430 | if (Arg == 0 || Op == OO_Plus) { |
| 5431 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 5432 | // T* operator+(ptrdiff_t, T*); |
| 5433 | S.AddBuiltinCandidate(*Ptr, AsymetricParamTypes, Args, 2, |
| 5434 | CandidateSet); |
| 5435 | } |
| 5436 | if (Op == OO_Minus) { |
| 5437 | // ptrdiff_t operator-(T, T); |
| 5438 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5439 | continue; |
| 5440 | |
| 5441 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5442 | S.AddBuiltinCandidate(S.Context.getPointerDiffType(), ParamTypes, |
| 5443 | Args, 2, CandidateSet); |
| 5444 | } |
| 5445 | } |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | // C++ [over.built]p12: |
| 5450 | // |
| 5451 | // For every pair of promoted arithmetic types L and R, there |
| 5452 | // exist candidate operator functions of the form |
| 5453 | // |
| 5454 | // LR operator*(L, R); |
| 5455 | // LR operator/(L, R); |
| 5456 | // LR operator+(L, R); |
| 5457 | // LR operator-(L, R); |
| 5458 | // bool operator<(L, R); |
| 5459 | // bool operator>(L, R); |
| 5460 | // bool operator<=(L, R); |
| 5461 | // bool operator>=(L, R); |
| 5462 | // bool operator==(L, R); |
| 5463 | // bool operator!=(L, R); |
| 5464 | // |
| 5465 | // where LR is the result of the usual arithmetic conversions |
| 5466 | // between types L and R. |
| 5467 | // |
| 5468 | // C++ [over.built]p24: |
| 5469 | // |
| 5470 | // For every pair of promoted arithmetic types L and R, there exist |
| 5471 | // candidate operator functions of the form |
| 5472 | // |
| 5473 | // LR operator?(bool, L, R); |
| 5474 | // |
| 5475 | // where LR is the result of the usual arithmetic conversions |
| 5476 | // between types L and R. |
| 5477 | // Our candidates ignore the first parameter. |
| 5478 | void addGenericBinaryArithmeticOverloads(bool isComparison) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5479 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5480 | return; |
| 5481 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5482 | for (unsigned Left = FirstPromotedArithmeticType; |
| 5483 | Left < LastPromotedArithmeticType; ++Left) { |
| 5484 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5485 | Right < LastPromotedArithmeticType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5486 | QualType LandR[2] = { getArithmeticType(Left), |
| 5487 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5488 | QualType Result = |
| 5489 | isComparison ? S.Context.BoolTy |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5490 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5491 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | // Extension: Add the binary operators ==, !=, <, <=, >=, >, *, /, and the |
| 5496 | // conditional operator for vector types. |
| 5497 | for (BuiltinCandidateTypeSet::iterator |
| 5498 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5499 | Vec1End = CandidateTypes[0].vector_end(); |
| 5500 | Vec1 != Vec1End; ++Vec1) { |
| 5501 | for (BuiltinCandidateTypeSet::iterator |
| 5502 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5503 | Vec2End = CandidateTypes[1].vector_end(); |
| 5504 | Vec2 != Vec2End; ++Vec2) { |
| 5505 | QualType LandR[2] = { *Vec1, *Vec2 }; |
| 5506 | QualType Result = S.Context.BoolTy; |
| 5507 | if (!isComparison) { |
| 5508 | if ((*Vec1)->isExtVectorType() || !(*Vec2)->isExtVectorType()) |
| 5509 | Result = *Vec1; |
| 5510 | else |
| 5511 | Result = *Vec2; |
| 5512 | } |
| 5513 | |
| 5514 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5515 | } |
| 5516 | } |
| 5517 | } |
| 5518 | |
| 5519 | // C++ [over.built]p17: |
| 5520 | // |
| 5521 | // For every pair of promoted integral types L and R, there |
| 5522 | // exist candidate operator functions of the form |
| 5523 | // |
| 5524 | // LR operator%(L, R); |
| 5525 | // LR operator&(L, R); |
| 5526 | // LR operator^(L, R); |
| 5527 | // LR operator|(L, R); |
| 5528 | // L operator<<(L, R); |
| 5529 | // L operator>>(L, R); |
| 5530 | // |
| 5531 | // where LR is the result of the usual arithmetic conversions |
| 5532 | // between types L and R. |
| 5533 | void addBinaryBitwiseArithmeticOverloads(OverloadedOperatorKind Op) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5534 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5535 | return; |
| 5536 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5537 | for (unsigned Left = FirstPromotedIntegralType; |
| 5538 | Left < LastPromotedIntegralType; ++Left) { |
| 5539 | for (unsigned Right = FirstPromotedIntegralType; |
| 5540 | Right < LastPromotedIntegralType; ++Right) { |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5541 | QualType LandR[2] = { getArithmeticType(Left), |
| 5542 | getArithmeticType(Right) }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5543 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 5544 | ? LandR[0] |
Chandler Carruth | 3b35b78d | 2010-12-12 09:59:53 +0000 | [diff] [blame] | 5545 | : getUsualArithmeticConversions(Left, Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5546 | S.AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 5547 | } |
| 5548 | } |
| 5549 | } |
| 5550 | |
| 5551 | // C++ [over.built]p20: |
| 5552 | // |
| 5553 | // For every pair (T, VQ), where T is an enumeration or |
| 5554 | // pointer to member type and VQ is either volatile or |
| 5555 | // empty, there exist candidate operator functions of the form |
| 5556 | // |
| 5557 | // VQ T& operator=(VQ T&, T); |
| 5558 | void addAssignmentMemberPointerOrEnumeralOverloads() { |
| 5559 | /// Set of (canonical) types that we've already handled. |
| 5560 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5561 | |
| 5562 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5563 | for (BuiltinCandidateTypeSet::iterator |
| 5564 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5565 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5566 | Enum != EnumEnd; ++Enum) { |
| 5567 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5568 | continue; |
| 5569 | |
| 5570 | AddBuiltinAssignmentOperatorCandidates(S, *Enum, Args, 2, |
| 5571 | CandidateSet); |
| 5572 | } |
| 5573 | |
| 5574 | for (BuiltinCandidateTypeSet::iterator |
| 5575 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5576 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5577 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5578 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5579 | continue; |
| 5580 | |
| 5581 | AddBuiltinAssignmentOperatorCandidates(S, *MemPtr, Args, 2, |
| 5582 | CandidateSet); |
| 5583 | } |
| 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | // C++ [over.built]p19: |
| 5588 | // |
| 5589 | // For every pair (T, VQ), where T is any type and VQ is either |
| 5590 | // volatile or empty, there exist candidate operator functions |
| 5591 | // of the form |
| 5592 | // |
| 5593 | // T*VQ& operator=(T*VQ&, T*); |
| 5594 | // |
| 5595 | // C++ [over.built]p21: |
| 5596 | // |
| 5597 | // For every pair (T, VQ), where T is a cv-qualified or |
| 5598 | // cv-unqualified object type and VQ is either volatile or |
| 5599 | // empty, there exist candidate operator functions of the form |
| 5600 | // |
| 5601 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 5602 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 5603 | void addAssignmentPointerOverloads(bool isEqualOp) { |
| 5604 | /// Set of (canonical) types that we've already handled. |
| 5605 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5606 | |
| 5607 | for (BuiltinCandidateTypeSet::iterator |
| 5608 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5609 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5610 | Ptr != PtrEnd; ++Ptr) { |
| 5611 | // If this is operator=, keep track of the builtin candidates we added. |
| 5612 | if (isEqualOp) |
| 5613 | AddedTypes.insert(S.Context.getCanonicalType(*Ptr)); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5614 | else if (!(*Ptr)->getPointeeType()->isObjectType()) |
| 5615 | continue; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5616 | |
| 5617 | // non-volatile version |
| 5618 | QualType ParamTypes[2] = { |
| 5619 | S.Context.getLValueReferenceType(*Ptr), |
| 5620 | isEqualOp ? *Ptr : S.Context.getPointerDiffType(), |
| 5621 | }; |
| 5622 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5623 | /*IsAssigmentOperator=*/ isEqualOp); |
| 5624 | |
| 5625 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5626 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5627 | // volatile version |
| 5628 | ParamTypes[0] = |
| 5629 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
| 5630 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5631 | /*IsAssigmentOperator=*/isEqualOp); |
| 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | if (isEqualOp) { |
| 5636 | for (BuiltinCandidateTypeSet::iterator |
| 5637 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5638 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5639 | Ptr != PtrEnd; ++Ptr) { |
| 5640 | // Make sure we don't add the same candidate twice. |
| 5641 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5642 | continue; |
| 5643 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5644 | QualType ParamTypes[2] = { |
| 5645 | S.Context.getLValueReferenceType(*Ptr), |
| 5646 | *Ptr, |
| 5647 | }; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5648 | |
| 5649 | // non-volatile version |
| 5650 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5651 | /*IsAssigmentOperator=*/true); |
| 5652 | |
| 5653 | if (!S.Context.getCanonicalType(*Ptr).isVolatileQualified() && |
| 5654 | VisibleTypeConversionsQuals.hasVolatile()) { |
| 5655 | // volatile version |
| 5656 | ParamTypes[0] = |
| 5657 | S.Context.getLValueReferenceType(S.Context.getVolatileType(*Ptr)); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5658 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5659 | CandidateSet, /*IsAssigmentOperator=*/true); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5660 | } |
| 5661 | } |
| 5662 | } |
| 5663 | } |
| 5664 | |
| 5665 | // C++ [over.built]p18: |
| 5666 | // |
| 5667 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 5668 | // VQ is either volatile or empty, and R is a promoted |
| 5669 | // arithmetic type, there exist candidate operator functions of |
| 5670 | // the form |
| 5671 | // |
| 5672 | // VQ L& operator=(VQ L&, R); |
| 5673 | // VQ L& operator*=(VQ L&, R); |
| 5674 | // VQ L& operator/=(VQ L&, R); |
| 5675 | // VQ L& operator+=(VQ L&, R); |
| 5676 | // VQ L& operator-=(VQ L&, R); |
| 5677 | void addAssignmentArithmeticOverloads(bool isEqualOp) { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5678 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5679 | return; |
| 5680 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5681 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
| 5682 | for (unsigned Right = FirstPromotedArithmeticType; |
| 5683 | Right < LastPromotedArithmeticType; ++Right) { |
| 5684 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5685 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5686 | |
| 5687 | // Add this built-in operator as a candidate (VQ is empty). |
| 5688 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5689 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5690 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5691 | /*IsAssigmentOperator=*/isEqualOp); |
| 5692 | |
| 5693 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5694 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5695 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5696 | S.Context.getVolatileType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5697 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5698 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5699 | CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5700 | /*IsAssigmentOperator=*/isEqualOp); |
| 5701 | } |
| 5702 | } |
| 5703 | } |
| 5704 | |
| 5705 | // Extension: Add the binary operators =, +=, -=, *=, /= for vector types. |
| 5706 | for (BuiltinCandidateTypeSet::iterator |
| 5707 | Vec1 = CandidateTypes[0].vector_begin(), |
| 5708 | Vec1End = CandidateTypes[0].vector_end(); |
| 5709 | Vec1 != Vec1End; ++Vec1) { |
| 5710 | for (BuiltinCandidateTypeSet::iterator |
| 5711 | Vec2 = CandidateTypes[1].vector_begin(), |
| 5712 | Vec2End = CandidateTypes[1].vector_end(); |
| 5713 | Vec2 != Vec2End; ++Vec2) { |
| 5714 | QualType ParamTypes[2]; |
| 5715 | ParamTypes[1] = *Vec2; |
| 5716 | // Add this built-in operator as a candidate (VQ is empty). |
| 5717 | ParamTypes[0] = S.Context.getLValueReferenceType(*Vec1); |
| 5718 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 5719 | /*IsAssigmentOperator=*/isEqualOp); |
| 5720 | |
| 5721 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 5722 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5723 | ParamTypes[0] = S.Context.getVolatileType(*Vec1); |
| 5724 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 5725 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5726 | CandidateSet, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5727 | /*IsAssigmentOperator=*/isEqualOp); |
| 5728 | } |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | // C++ [over.built]p22: |
| 5734 | // |
| 5735 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 5736 | // is either volatile or empty, and R is a promoted integral |
| 5737 | // type, there exist candidate operator functions of the form |
| 5738 | // |
| 5739 | // VQ L& operator%=(VQ L&, R); |
| 5740 | // VQ L& operator<<=(VQ L&, R); |
| 5741 | // VQ L& operator>>=(VQ L&, R); |
| 5742 | // VQ L& operator&=(VQ L&, R); |
| 5743 | // VQ L& operator^=(VQ L&, R); |
| 5744 | // VQ L& operator|=(VQ L&, R); |
| 5745 | void addAssignmentIntegralOverloads() { |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5746 | if (!HasArithmeticOrEnumeralCandidateType) |
| 5747 | return; |
| 5748 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5749 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
| 5750 | for (unsigned Right = FirstPromotedIntegralType; |
| 5751 | Right < LastPromotedIntegralType; ++Right) { |
| 5752 | QualType ParamTypes[2]; |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5753 | ParamTypes[1] = getArithmeticType(Right); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5754 | |
| 5755 | // Add this built-in operator as a candidate (VQ is empty). |
| 5756 | ParamTypes[0] = |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5757 | S.Context.getLValueReferenceType(getArithmeticType(Left)); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5758 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 5759 | if (VisibleTypeConversionsQuals.hasVolatile()) { |
| 5760 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
Chandler Carruth | c6586e5 | 2010-12-12 10:35:00 +0000 | [diff] [blame] | 5761 | ParamTypes[0] = getArithmeticType(Left); |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5762 | ParamTypes[0] = S.Context.getVolatileType(ParamTypes[0]); |
| 5763 | ParamTypes[0] = S.Context.getLValueReferenceType(ParamTypes[0]); |
| 5764 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, |
| 5765 | CandidateSet); |
| 5766 | } |
| 5767 | } |
| 5768 | } |
| 5769 | } |
| 5770 | |
| 5771 | // C++ [over.operator]p23: |
| 5772 | // |
| 5773 | // There also exist candidate operator functions of the form |
| 5774 | // |
| 5775 | // bool operator!(bool); |
| 5776 | // bool operator&&(bool, bool); |
| 5777 | // bool operator||(bool, bool); |
| 5778 | void addExclaimOverload() { |
| 5779 | QualType ParamTy = S.Context.BoolTy; |
| 5780 | S.AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 5781 | /*IsAssignmentOperator=*/false, |
| 5782 | /*NumContextualBoolArguments=*/1); |
| 5783 | } |
| 5784 | void addAmpAmpOrPipePipeOverload() { |
| 5785 | QualType ParamTypes[2] = { S.Context.BoolTy, S.Context.BoolTy }; |
| 5786 | S.AddBuiltinCandidate(S.Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 5787 | /*IsAssignmentOperator=*/false, |
| 5788 | /*NumContextualBoolArguments=*/2); |
| 5789 | } |
| 5790 | |
| 5791 | // C++ [over.built]p13: |
| 5792 | // |
| 5793 | // For every cv-qualified or cv-unqualified object type T there |
| 5794 | // exist candidate operator functions of the form |
| 5795 | // |
| 5796 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 5797 | // T& operator[](T*, ptrdiff_t); |
| 5798 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 5799 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 5800 | // T& operator[](ptrdiff_t, T*); |
| 5801 | void addSubscriptOverloads() { |
| 5802 | for (BuiltinCandidateTypeSet::iterator |
| 5803 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5804 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5805 | Ptr != PtrEnd; ++Ptr) { |
| 5806 | QualType ParamTypes[2] = { *Ptr, S.Context.getPointerDiffType() }; |
| 5807 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5808 | if (!PointeeType->isObjectType()) |
| 5809 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5810 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5811 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5812 | |
| 5813 | // T& operator[](T*, ptrdiff_t) |
| 5814 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5815 | } |
| 5816 | |
| 5817 | for (BuiltinCandidateTypeSet::iterator |
| 5818 | Ptr = CandidateTypes[1].pointer_begin(), |
| 5819 | PtrEnd = CandidateTypes[1].pointer_end(); |
| 5820 | Ptr != PtrEnd; ++Ptr) { |
| 5821 | QualType ParamTypes[2] = { S.Context.getPointerDiffType(), *Ptr }; |
| 5822 | QualType PointeeType = (*Ptr)->getPointeeType(); |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 5823 | if (!PointeeType->isObjectType()) |
| 5824 | continue; |
| 5825 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5826 | QualType ResultTy = S.Context.getLValueReferenceType(PointeeType); |
| 5827 | |
| 5828 | // T& operator[](ptrdiff_t, T*) |
| 5829 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5830 | } |
| 5831 | } |
| 5832 | |
| 5833 | // C++ [over.built]p11: |
| 5834 | // For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, |
| 5835 | // C1 is the same type as C2 or is a derived class of C2, T is an object |
| 5836 | // type or a function type, and CV1 and CV2 are cv-qualifier-seqs, |
| 5837 | // there exist candidate operator functions of the form |
| 5838 | // |
| 5839 | // CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 5840 | // |
| 5841 | // where CV12 is the union of CV1 and CV2. |
| 5842 | void addArrowStarOverloads() { |
| 5843 | for (BuiltinCandidateTypeSet::iterator |
| 5844 | Ptr = CandidateTypes[0].pointer_begin(), |
| 5845 | PtrEnd = CandidateTypes[0].pointer_end(); |
| 5846 | Ptr != PtrEnd; ++Ptr) { |
| 5847 | QualType C1Ty = (*Ptr); |
| 5848 | QualType C1; |
| 5849 | QualifierCollector Q1; |
| 5850 | C1 = QualType(Q1.strip(C1Ty->getPointeeType()), 0); |
| 5851 | if (!isa<RecordType>(C1)) |
| 5852 | continue; |
| 5853 | // heuristic to reduce number of builtin candidates in the set. |
| 5854 | // Add volatile/restrict version only if there are conversions to a |
| 5855 | // volatile/restrict type. |
| 5856 | if (!VisibleTypeConversionsQuals.hasVolatile() && Q1.hasVolatile()) |
| 5857 | continue; |
| 5858 | if (!VisibleTypeConversionsQuals.hasRestrict() && Q1.hasRestrict()) |
| 5859 | continue; |
| 5860 | for (BuiltinCandidateTypeSet::iterator |
| 5861 | MemPtr = CandidateTypes[1].member_pointer_begin(), |
| 5862 | MemPtrEnd = CandidateTypes[1].member_pointer_end(); |
| 5863 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5864 | const MemberPointerType *mptr = cast<MemberPointerType>(*MemPtr); |
| 5865 | QualType C2 = QualType(mptr->getClass(), 0); |
| 5866 | C2 = C2.getUnqualifiedType(); |
| 5867 | if (C1 != C2 && !S.IsDerivedFrom(C1, C2)) |
| 5868 | break; |
| 5869 | QualType ParamTypes[2] = { *Ptr, *MemPtr }; |
| 5870 | // build CV12 T& |
| 5871 | QualType T = mptr->getPointeeType(); |
| 5872 | if (!VisibleTypeConversionsQuals.hasVolatile() && |
| 5873 | T.isVolatileQualified()) |
| 5874 | continue; |
| 5875 | if (!VisibleTypeConversionsQuals.hasRestrict() && |
| 5876 | T.isRestrictQualified()) |
| 5877 | continue; |
| 5878 | T = Q1.apply(S.Context, T); |
| 5879 | QualType ResultTy = S.Context.getLValueReferenceType(T); |
| 5880 | S.AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 5881 | } |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | // Note that we don't consider the first argument, since it has been |
| 5886 | // contextually converted to bool long ago. The candidates below are |
| 5887 | // therefore added as binary. |
| 5888 | // |
| 5889 | // C++ [over.built]p25: |
| 5890 | // For every type T, where T is a pointer, pointer-to-member, or scoped |
| 5891 | // enumeration type, there exist candidate operator functions of the form |
| 5892 | // |
| 5893 | // T operator?(bool, T, T); |
| 5894 | // |
| 5895 | void addConditionalOperatorOverloads() { |
| 5896 | /// Set of (canonical) types that we've already handled. |
| 5897 | llvm::SmallPtrSet<QualType, 8> AddedTypes; |
| 5898 | |
| 5899 | for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { |
| 5900 | for (BuiltinCandidateTypeSet::iterator |
| 5901 | Ptr = CandidateTypes[ArgIdx].pointer_begin(), |
| 5902 | PtrEnd = CandidateTypes[ArgIdx].pointer_end(); |
| 5903 | Ptr != PtrEnd; ++Ptr) { |
| 5904 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Ptr))) |
| 5905 | continue; |
| 5906 | |
| 5907 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 5908 | S.AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 5909 | } |
| 5910 | |
| 5911 | for (BuiltinCandidateTypeSet::iterator |
| 5912 | MemPtr = CandidateTypes[ArgIdx].member_pointer_begin(), |
| 5913 | MemPtrEnd = CandidateTypes[ArgIdx].member_pointer_end(); |
| 5914 | MemPtr != MemPtrEnd; ++MemPtr) { |
| 5915 | if (!AddedTypes.insert(S.Context.getCanonicalType(*MemPtr))) |
| 5916 | continue; |
| 5917 | |
| 5918 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 5919 | S.AddBuiltinCandidate(*MemPtr, ParamTypes, Args, 2, CandidateSet); |
| 5920 | } |
| 5921 | |
| 5922 | if (S.getLangOptions().CPlusPlus0x) { |
| 5923 | for (BuiltinCandidateTypeSet::iterator |
| 5924 | Enum = CandidateTypes[ArgIdx].enumeration_begin(), |
| 5925 | EnumEnd = CandidateTypes[ArgIdx].enumeration_end(); |
| 5926 | Enum != EnumEnd; ++Enum) { |
| 5927 | if (!(*Enum)->getAs<EnumType>()->getDecl()->isScoped()) |
| 5928 | continue; |
| 5929 | |
| 5930 | if (!AddedTypes.insert(S.Context.getCanonicalType(*Enum))) |
| 5931 | continue; |
| 5932 | |
| 5933 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 5934 | S.AddBuiltinCandidate(*Enum, ParamTypes, Args, 2, CandidateSet); |
| 5935 | } |
| 5936 | } |
| 5937 | } |
| 5938 | } |
| 5939 | }; |
| 5940 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5941 | } // end anonymous namespace |
| 5942 | |
| 5943 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 5944 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 5945 | /// on the operator @p Op and the arguments given. For example, if the |
| 5946 | /// operator is a binary '+', this routine might add "int |
| 5947 | /// operator+(int, int)" to cover integer addition. |
| 5948 | void |
| 5949 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
| 5950 | SourceLocation OpLoc, |
| 5951 | Expr **Args, unsigned NumArgs, |
| 5952 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5953 | // Find all of the types that the arguments can convert to, but only |
| 5954 | // if the operator we're looking at has built-in operator candidates |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5955 | // that make use of these types. Also record whether we encounter non-record |
| 5956 | // candidate types or either arithmetic or enumeral candidate types. |
Fariborz Jahanian | 3b937fa | 2009-10-15 17:14:05 +0000 | [diff] [blame] | 5957 | Qualifiers VisibleTypeConversionsQuals; |
| 5958 | VisibleTypeConversionsQuals.addConst(); |
Fariborz Jahanian | b9e8c42 | 2009-10-19 21:30:45 +0000 | [diff] [blame] | 5959 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 5960 | VisibleTypeConversionsQuals += CollectVRQualifiers(Context, Args[ArgIdx]); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5961 | |
| 5962 | bool HasNonRecordCandidateType = false; |
| 5963 | bool HasArithmeticOrEnumeralCandidateType = false; |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 5964 | llvm::SmallVector<BuiltinCandidateTypeSet, 2> CandidateTypes; |
| 5965 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 5966 | CandidateTypes.push_back(BuiltinCandidateTypeSet(*this)); |
| 5967 | CandidateTypes[ArgIdx].AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 5968 | OpLoc, |
| 5969 | true, |
| 5970 | (Op == OO_Exclaim || |
| 5971 | Op == OO_AmpAmp || |
| 5972 | Op == OO_PipePipe), |
| 5973 | VisibleTypeConversionsQuals); |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5974 | HasNonRecordCandidateType = HasNonRecordCandidateType || |
| 5975 | CandidateTypes[ArgIdx].hasNonRecordTypes(); |
| 5976 | HasArithmeticOrEnumeralCandidateType = |
| 5977 | HasArithmeticOrEnumeralCandidateType || |
| 5978 | CandidateTypes[ArgIdx].hasArithmeticOrEnumeralTypes(); |
Douglas Gregor | b37c9af | 2010-11-03 17:00:07 +0000 | [diff] [blame] | 5979 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5980 | |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5981 | // Exit early when no non-record types have been added to the candidate set |
| 5982 | // for any of the arguments to the operator. |
| 5983 | if (!HasNonRecordCandidateType) |
| 5984 | return; |
| 5985 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5986 | // Setup an object to manage the common state for building overloads. |
| 5987 | BuiltinOperatorOverloadBuilder OpBuilder(*this, Args, NumArgs, |
| 5988 | VisibleTypeConversionsQuals, |
Chandler Carruth | 00a3833 | 2010-12-13 01:44:01 +0000 | [diff] [blame] | 5989 | HasArithmeticOrEnumeralCandidateType, |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 5990 | CandidateTypes, CandidateSet); |
| 5991 | |
| 5992 | // Dispatch over the operation to add in only those overloads which apply. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5993 | switch (Op) { |
| 5994 | case OO_None: |
| 5995 | case NUM_OVERLOADED_OPERATORS: |
| 5996 | assert(false && "Expected an overloaded operator"); |
| 5997 | break; |
| 5998 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 5999 | case OO_New: |
| 6000 | case OO_Delete: |
| 6001 | case OO_Array_New: |
| 6002 | case OO_Array_Delete: |
| 6003 | case OO_Call: |
| 6004 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
| 6005 | break; |
| 6006 | |
| 6007 | case OO_Comma: |
| 6008 | case OO_Arrow: |
| 6009 | // C++ [over.match.oper]p3: |
| 6010 | // -- For the operator ',', the unary operator '&', or the |
| 6011 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6012 | break; |
| 6013 | |
| 6014 | case OO_Plus: // '+' is either unary or binary |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6015 | if (NumArgs == 1) |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6016 | OpBuilder.addUnaryPlusPointerOverloads(); |
Chandler Carruth | 9694b9c | 2010-12-12 08:41:34 +0000 | [diff] [blame] | 6017 | // Fall through. |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6018 | |
| 6019 | case OO_Minus: // '-' is either unary or binary |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6020 | if (NumArgs == 1) { |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6021 | OpBuilder.addUnaryPlusOrMinusArithmeticOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6022 | } else { |
| 6023 | OpBuilder.addBinaryPlusOrMinusPointerOverloads(Op); |
| 6024 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6025 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6026 | break; |
| 6027 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6028 | case OO_Star: // '*' is either unary or binary |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6029 | if (NumArgs == 1) |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6030 | OpBuilder.addUnaryStarPointerOverloads(); |
| 6031 | else |
| 6032 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6033 | break; |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6034 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6035 | case OO_Slash: |
| 6036 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6037 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6038 | |
| 6039 | case OO_PlusPlus: |
| 6040 | case OO_MinusMinus: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6041 | OpBuilder.addPlusPlusMinusMinusArithmeticOverloads(Op); |
| 6042 | OpBuilder.addPlusPlusMinusMinusPointerOverloads(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6043 | break; |
| 6044 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 6045 | case OO_EqualEqual: |
| 6046 | case OO_ExclaimEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6047 | OpBuilder.addEqualEqualOrNotEqualMemberPointerOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6048 | // Fall through. |
Chandler Carruth | 9de23cd | 2010-12-12 08:45:02 +0000 | [diff] [blame] | 6049 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6050 | case OO_Less: |
| 6051 | case OO_Greater: |
| 6052 | case OO_LessEqual: |
| 6053 | case OO_GreaterEqual: |
Chandler Carruth | c02db8c | 2010-12-12 09:14:11 +0000 | [diff] [blame] | 6054 | OpBuilder.addRelationalPointerOrEnumeralOverloads(); |
Chandler Carruth | 0375e95 | 2010-12-12 08:32:28 +0000 | [diff] [blame] | 6055 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/true); |
| 6056 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6057 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6058 | case OO_Percent: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6059 | case OO_Caret: |
| 6060 | case OO_Pipe: |
| 6061 | case OO_LessLess: |
| 6062 | case OO_GreaterGreater: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6063 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6064 | break; |
| 6065 | |
Chandler Carruth | 5184de0 | 2010-12-12 08:51:33 +0000 | [diff] [blame] | 6066 | case OO_Amp: // '&' is either unary or binary |
| 6067 | if (NumArgs == 1) |
| 6068 | // C++ [over.match.oper]p3: |
| 6069 | // -- For the operator ',', the unary operator '&', or the |
| 6070 | // operator '->', the built-in candidates set is empty. |
| 6071 | break; |
| 6072 | |
| 6073 | OpBuilder.addBinaryBitwiseArithmeticOverloads(Op); |
| 6074 | break; |
| 6075 | |
| 6076 | case OO_Tilde: |
| 6077 | OpBuilder.addUnaryTildePromotedIntegralOverloads(); |
| 6078 | break; |
| 6079 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6080 | case OO_Equal: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6081 | OpBuilder.addAssignmentMemberPointerOrEnumeralOverloads(); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6082 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6083 | |
| 6084 | case OO_PlusEqual: |
| 6085 | case OO_MinusEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6086 | OpBuilder.addAssignmentPointerOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6087 | // Fall through. |
| 6088 | |
| 6089 | case OO_StarEqual: |
| 6090 | case OO_SlashEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6091 | OpBuilder.addAssignmentArithmeticOverloads(Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6092 | break; |
| 6093 | |
| 6094 | case OO_PercentEqual: |
| 6095 | case OO_LessLessEqual: |
| 6096 | case OO_GreaterGreaterEqual: |
| 6097 | case OO_AmpEqual: |
| 6098 | case OO_CaretEqual: |
| 6099 | case OO_PipeEqual: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6100 | OpBuilder.addAssignmentIntegralOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6101 | break; |
| 6102 | |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6103 | case OO_Exclaim: |
| 6104 | OpBuilder.addExclaimOverload(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6105 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6106 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6107 | case OO_AmpAmp: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6108 | case OO_PipePipe: |
| 6109 | OpBuilder.addAmpAmpOrPipePipeOverload(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6110 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6111 | |
| 6112 | case OO_Subscript: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6113 | OpBuilder.addSubscriptOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6114 | break; |
| 6115 | |
| 6116 | case OO_ArrowStar: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6117 | OpBuilder.addArrowStarOverloads(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6118 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 6119 | |
| 6120 | case OO_Conditional: |
Chandler Carruth | 85c2d09a | 2010-12-12 08:11:30 +0000 | [diff] [blame] | 6121 | OpBuilder.addConditionalOperatorOverloads(); |
Chandler Carruth | f980244 | 2010-12-12 08:39:38 +0000 | [diff] [blame] | 6122 | OpBuilder.addGenericBinaryArithmeticOverloads(/*isComparison=*/false); |
| 6123 | break; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6124 | } |
| 6125 | } |
| 6126 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6127 | /// \brief Add function candidates found via argument-dependent lookup |
| 6128 | /// to the set of overloading candidates. |
| 6129 | /// |
| 6130 | /// This routine performs argument-dependent name lookup based on the |
| 6131 | /// given function name (which may also be an operator name) and adds |
| 6132 | /// all of the overload candidates found by ADL to the overload |
| 6133 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6134 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6135 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6136 | bool Operator, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6137 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 6138 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6139 | OverloadCandidateSet& CandidateSet, |
| 6140 | bool PartialOverloading) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6141 | ADLResult Fns; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6142 | |
John McCall | 91f61fc | 2010-01-26 06:04:06 +0000 | [diff] [blame] | 6143 | // FIXME: This approach for uniquing ADL results (and removing |
| 6144 | // redundant candidates from the set) relies on pointer-equality, |
| 6145 | // which means we need to key off the canonical decl. However, |
| 6146 | // always going back to the canonical decl might not get us the |
| 6147 | // right set of default arguments. What default arguments are |
| 6148 | // we supposed to consider on ADL candidates, anyway? |
| 6149 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6150 | // FIXME: Pass in the explicit template arguments? |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6151 | ArgumentDependentLookup(Name, Operator, Args, NumArgs, Fns); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6152 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6153 | // Erase all of the candidates we already knew about. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6154 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 6155 | CandEnd = CandidateSet.end(); |
| 6156 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6157 | if (Cand->Function) { |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6158 | Fns.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6159 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6160 | Fns.erase(FunTmpl); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6161 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6162 | |
| 6163 | // For each of the ADL candidates we found, add it to the overload |
| 6164 | // set. |
John McCall | 8fe6808 | 2010-01-26 07:16:45 +0000 | [diff] [blame] | 6165 | for (ADLResult::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6166 | DeclAccessPair FoundDecl = DeclAccessPair::make(*I, AS_none); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6167 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6168 | if (ExplicitTemplateArgs) |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6169 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6170 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6171 | AddOverloadCandidate(FD, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 6172 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 6173 | } else |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6174 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*I), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6175 | FoundDecl, ExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 6176 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 6177 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6180 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 6181 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6182 | bool |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6183 | isBetterOverloadCandidate(Sema &S, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6184 | const OverloadCandidate &Cand1, |
| 6185 | const OverloadCandidate &Cand2, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6186 | SourceLocation Loc, |
| 6187 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6188 | // Define viable functions to be better candidates than non-viable |
| 6189 | // functions. |
| 6190 | if (!Cand2.Viable) |
| 6191 | return Cand1.Viable; |
| 6192 | else if (!Cand1.Viable) |
| 6193 | return false; |
| 6194 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6195 | // C++ [over.match.best]p1: |
| 6196 | // |
| 6197 | // -- if F is a static member function, ICS1(F) is defined such |
| 6198 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 6199 | // any function G, and, symmetrically, ICS1(G) is neither |
| 6200 | // better nor worse than ICS1(F). |
| 6201 | unsigned StartArg = 0; |
| 6202 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 6203 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6205 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6206 | // A viable function F1 is defined to be a better function than another |
| 6207 | // 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] | 6208 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6209 | unsigned NumArgs = Cand1.Conversions.size(); |
| 6210 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 6211 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 6212 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6213 | switch (CompareImplicitConversionSequences(S, |
| 6214 | Cand1.Conversions[ArgIdx], |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6215 | Cand2.Conversions[ArgIdx])) { |
| 6216 | case ImplicitConversionSequence::Better: |
| 6217 | // Cand1 has a better conversion sequence. |
| 6218 | HasBetterConversion = true; |
| 6219 | break; |
| 6220 | |
| 6221 | case ImplicitConversionSequence::Worse: |
| 6222 | // Cand1 can't be better than Cand2. |
| 6223 | return false; |
| 6224 | |
| 6225 | case ImplicitConversionSequence::Indistinguishable: |
| 6226 | // Do nothing. |
| 6227 | break; |
| 6228 | } |
| 6229 | } |
| 6230 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6231 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6232 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6233 | if (HasBetterConversion) |
| 6234 | return true; |
| 6235 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6236 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6237 | // specialization, or, if not that, |
Douglas Gregor | ce21919b | 2010-06-08 21:03:17 +0000 | [diff] [blame] | 6238 | if ((!Cand1.Function || !Cand1.Function->getPrimaryTemplate()) && |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 6239 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 6240 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6241 | |
| 6242 | // -- F1 and F2 are function template specializations, and the function |
| 6243 | // template for F1 is more specialized than the template for F2 |
| 6244 | // 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] | 6245 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 6246 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6247 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6248 | if (FunctionTemplateDecl *BetterTemplate |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6249 | = S.getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 6250 | Cand2.Function->getPrimaryTemplate(), |
| 6251 | Loc, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6252 | isa<CXXConversionDecl>(Cand1.Function)? TPOC_Conversion |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6253 | : TPOC_Call, |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6254 | Cand1.ExplicitCallArguments)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 6255 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 6edd977 | 2011-01-19 23:54:39 +0000 | [diff] [blame] | 6256 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6257 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6258 | // -- the context is an initialization by user-defined conversion |
| 6259 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 6260 | // from the return type of F1 to the destination type (i.e., |
| 6261 | // the type of the entity being initialized) is a better |
| 6262 | // conversion sequence than the standard conversion sequence |
| 6263 | // from the return type of F2 to the destination type. |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6264 | if (UserDefinedConversion && Cand1.Function && Cand2.Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6265 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6266 | isa<CXXConversionDecl>(Cand2.Function)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6267 | switch (CompareStandardConversionSequences(S, |
| 6268 | Cand1.FinalConversion, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 6269 | Cand2.FinalConversion)) { |
| 6270 | case ImplicitConversionSequence::Better: |
| 6271 | // Cand1 has a better conversion sequence. |
| 6272 | return true; |
| 6273 | |
| 6274 | case ImplicitConversionSequence::Worse: |
| 6275 | // Cand1 can't be better than Cand2. |
| 6276 | return false; |
| 6277 | |
| 6278 | case ImplicitConversionSequence::Indistinguishable: |
| 6279 | // Do nothing |
| 6280 | break; |
| 6281 | } |
| 6282 | } |
| 6283 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6284 | return false; |
| 6285 | } |
| 6286 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6287 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6288 | /// within an overload candidate set. |
| 6289 | /// |
| 6290 | /// \param CandidateSet the set of candidate functions. |
| 6291 | /// |
| 6292 | /// \param Loc the location of the function name (or operator symbol) for |
| 6293 | /// which overload resolution occurs. |
| 6294 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6295 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6296 | /// function, Best points to the candidate function found. |
| 6297 | /// |
| 6298 | /// \returns The result of overload resolution. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6299 | OverloadingResult |
| 6300 | OverloadCandidateSet::BestViableFunction(Sema &S, SourceLocation Loc, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 6301 | iterator &Best, |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 6302 | bool UserDefinedConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6303 | // Find the best viable function. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6304 | Best = end(); |
| 6305 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
| 6306 | if (Cand->Viable) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6307 | if (Best == end() || isBetterOverloadCandidate(S, *Cand, *Best, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6308 | UserDefinedConversion)) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6309 | Best = Cand; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | // If we didn't find any viable functions, abort. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6313 | if (Best == end()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6314 | return OR_No_Viable_Function; |
| 6315 | |
| 6316 | // Make sure that this function is better than every other viable |
| 6317 | // function. If not, we have an ambiguity. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6318 | for (iterator Cand = begin(); Cand != end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6319 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6320 | Cand != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6321 | !isBetterOverloadCandidate(S, *Best, *Cand, Loc, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6322 | UserDefinedConversion)) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6323 | Best = end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6324 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 6325 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6326 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6327 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6328 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6329 | if (Best->Function && |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6330 | (Best->Function->isDeleted() || Best->Function->isUnavailable())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 6331 | return OR_Deleted; |
| 6332 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6333 | return OR_Success; |
| 6334 | } |
| 6335 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6336 | namespace { |
| 6337 | |
| 6338 | enum OverloadCandidateKind { |
| 6339 | oc_function, |
| 6340 | oc_method, |
| 6341 | oc_constructor, |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6342 | oc_function_template, |
| 6343 | oc_method_template, |
| 6344 | oc_constructor_template, |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6345 | oc_implicit_default_constructor, |
| 6346 | oc_implicit_copy_constructor, |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6347 | oc_implicit_copy_assignment, |
| 6348 | oc_implicit_inherited_constructor |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6349 | }; |
| 6350 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6351 | OverloadCandidateKind ClassifyOverloadCandidate(Sema &S, |
| 6352 | FunctionDecl *Fn, |
| 6353 | std::string &Description) { |
| 6354 | bool isTemplate = false; |
| 6355 | |
| 6356 | if (FunctionTemplateDecl *FunTmpl = Fn->getPrimaryTemplate()) { |
| 6357 | isTemplate = true; |
| 6358 | Description = S.getTemplateArgumentBindingsText( |
| 6359 | FunTmpl->getTemplateParameters(), *Fn->getTemplateSpecializationArgs()); |
| 6360 | } |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6361 | |
| 6362 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn)) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6363 | if (!Ctor->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6364 | return isTemplate ? oc_constructor_template : oc_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6365 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6366 | if (Ctor->getInheritedConstructor()) |
| 6367 | return oc_implicit_inherited_constructor; |
| 6368 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6369 | return Ctor->isCopyConstructor() ? oc_implicit_copy_constructor |
| 6370 | : oc_implicit_default_constructor; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | if (CXXMethodDecl *Meth = dyn_cast<CXXMethodDecl>(Fn)) { |
| 6374 | // This actually gets spelled 'candidate function' for now, but |
| 6375 | // it doesn't hurt to split it out. |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6376 | if (!Meth->isImplicit()) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6377 | return isTemplate ? oc_method_template : oc_method; |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6378 | |
Douglas Gregor | ec3bec0 | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 6379 | assert(Meth->isCopyAssignmentOperator() |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6380 | && "implicit method is not copy assignment operator?"); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6381 | return oc_implicit_copy_assignment; |
| 6382 | } |
| 6383 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6384 | return isTemplate ? oc_function_template : oc_function; |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6387 | void MaybeEmitInheritedConstructorNote(Sema &S, FunctionDecl *Fn) { |
| 6388 | const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Fn); |
| 6389 | if (!Ctor) return; |
| 6390 | |
| 6391 | Ctor = Ctor->getInheritedConstructor(); |
| 6392 | if (!Ctor) return; |
| 6393 | |
| 6394 | S.Diag(Ctor->getLocation(), diag::note_ovl_candidate_inherited_constructor); |
| 6395 | } |
| 6396 | |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6397 | } // end anonymous namespace |
| 6398 | |
| 6399 | // Notes the location of an overload candidate. |
| 6400 | void Sema::NoteOverloadCandidate(FunctionDecl *Fn) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6401 | std::string FnDesc; |
| 6402 | OverloadCandidateKind K = ClassifyOverloadCandidate(*this, Fn, FnDesc); |
| 6403 | Diag(Fn->getLocation(), diag::note_ovl_candidate) |
| 6404 | << (unsigned) K << FnDesc; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6405 | MaybeEmitInheritedConstructorNote(*this, Fn); |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 6406 | } |
| 6407 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6408 | //Notes the location of all overload candidates designated through |
| 6409 | // OverloadedExpr |
| 6410 | void Sema::NoteAllOverloadCandidates(Expr* OverloadedExpr) { |
| 6411 | assert(OverloadedExpr->getType() == Context.OverloadTy); |
| 6412 | |
| 6413 | OverloadExpr::FindResult Ovl = OverloadExpr::find(OverloadedExpr); |
| 6414 | OverloadExpr *OvlExpr = Ovl.Expression; |
| 6415 | |
| 6416 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 6417 | IEnd = OvlExpr->decls_end(); |
| 6418 | I != IEnd; ++I) { |
| 6419 | if (FunctionTemplateDecl *FunTmpl = |
| 6420 | dyn_cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()) ) { |
| 6421 | NoteOverloadCandidate(FunTmpl->getTemplatedDecl()); |
| 6422 | } else if (FunctionDecl *Fun |
| 6423 | = dyn_cast<FunctionDecl>((*I)->getUnderlyingDecl()) ) { |
| 6424 | NoteOverloadCandidate(Fun); |
| 6425 | } |
| 6426 | } |
| 6427 | } |
| 6428 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6429 | /// Diagnoses an ambiguous conversion. The partial diagnostic is the |
| 6430 | /// "lead" diagnostic; it will be given two arguments, the source and |
| 6431 | /// target types of the conversion. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6432 | void ImplicitConversionSequence::DiagnoseAmbiguousConversion( |
| 6433 | Sema &S, |
| 6434 | SourceLocation CaretLoc, |
| 6435 | const PartialDiagnostic &PDiag) const { |
| 6436 | S.Diag(CaretLoc, PDiag) |
| 6437 | << Ambiguous.getFromType() << Ambiguous.getToType(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6438 | for (AmbiguousConversionSequence::const_iterator |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6439 | I = Ambiguous.begin(), E = Ambiguous.end(); I != E; ++I) { |
| 6440 | S.NoteOverloadCandidate(*I); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6441 | } |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6444 | namespace { |
| 6445 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6446 | void DiagnoseBadConversion(Sema &S, OverloadCandidate *Cand, unsigned I) { |
| 6447 | const ImplicitConversionSequence &Conv = Cand->Conversions[I]; |
| 6448 | assert(Conv.isBad()); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6449 | assert(Cand->Function && "for now, candidate must be a function"); |
| 6450 | FunctionDecl *Fn = Cand->Function; |
| 6451 | |
| 6452 | // There's a conversion slot for the object argument if this is a |
| 6453 | // non-constructor method. Note that 'I' corresponds the |
| 6454 | // conversion-slot index. |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6455 | bool isObjectArgument = false; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6456 | if (isa<CXXMethodDecl>(Fn) && !isa<CXXConstructorDecl>(Fn)) { |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6457 | if (I == 0) |
| 6458 | isObjectArgument = true; |
| 6459 | else |
| 6460 | I--; |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6463 | std::string FnDesc; |
| 6464 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
| 6465 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6466 | Expr *FromExpr = Conv.Bad.FromExpr; |
| 6467 | QualType FromTy = Conv.Bad.getFromType(); |
| 6468 | QualType ToTy = Conv.Bad.getToType(); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6469 | |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6470 | if (FromTy == S.Context.OverloadTy) { |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6471 | assert(FromExpr && "overload set argument came from implicit argument?"); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6472 | Expr *E = FromExpr->IgnoreParens(); |
| 6473 | if (isa<UnaryOperator>(E)) |
| 6474 | E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 6475 | DeclarationName Name = cast<OverloadExpr>(E)->getName(); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6476 | |
| 6477 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_overload) |
| 6478 | << (unsigned) FnKind << FnDesc |
| 6479 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6480 | << ToTy << Name << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6481 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | fb7ad0f | 2010-02-02 02:42:52 +0000 | [diff] [blame] | 6482 | return; |
| 6483 | } |
| 6484 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6485 | // Do some hand-waving analysis to see if the non-viability is due |
| 6486 | // to a qualifier mismatch. |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6487 | CanQualType CFromTy = S.Context.getCanonicalType(FromTy); |
| 6488 | CanQualType CToTy = S.Context.getCanonicalType(ToTy); |
| 6489 | if (CanQual<ReferenceType> RT = CToTy->getAs<ReferenceType>()) |
| 6490 | CToTy = RT->getPointeeType(); |
| 6491 | else { |
| 6492 | // TODO: detect and diagnose the full richness of const mismatches. |
| 6493 | if (CanQual<PointerType> FromPT = CFromTy->getAs<PointerType>()) |
| 6494 | if (CanQual<PointerType> ToPT = CToTy->getAs<PointerType>()) |
| 6495 | CFromTy = FromPT->getPointeeType(), CToTy = ToPT->getPointeeType(); |
| 6496 | } |
| 6497 | |
| 6498 | if (CToTy.getUnqualifiedType() == CFromTy.getUnqualifiedType() && |
| 6499 | !CToTy.isAtLeastAsQualifiedAs(CFromTy)) { |
| 6500 | // It is dumb that we have to do this here. |
| 6501 | while (isa<ArrayType>(CFromTy)) |
| 6502 | CFromTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6503 | while (isa<ArrayType>(CToTy)) |
| 6504 | CToTy = CFromTy->getAs<ArrayType>()->getElementType(); |
| 6505 | |
| 6506 | Qualifiers FromQs = CFromTy.getQualifiers(); |
| 6507 | Qualifiers ToQs = CToTy.getQualifiers(); |
| 6508 | |
| 6509 | if (FromQs.getAddressSpace() != ToQs.getAddressSpace()) { |
| 6510 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_addrspace) |
| 6511 | << (unsigned) FnKind << FnDesc |
| 6512 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6513 | << FromTy |
| 6514 | << FromQs.getAddressSpace() << ToQs.getAddressSpace() |
| 6515 | << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6516 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6517 | return; |
| 6518 | } |
| 6519 | |
| 6520 | unsigned CVR = FromQs.getCVRQualifiers() & ~ToQs.getCVRQualifiers(); |
| 6521 | assert(CVR && "unexpected qualifiers mismatch"); |
| 6522 | |
| 6523 | if (isObjectArgument) { |
| 6524 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr_this) |
| 6525 | << (unsigned) FnKind << FnDesc |
| 6526 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6527 | << FromTy << (CVR - 1); |
| 6528 | } else { |
| 6529 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_cvr) |
| 6530 | << (unsigned) FnKind << FnDesc |
| 6531 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6532 | << FromTy << (CVR - 1) << I+1; |
| 6533 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6534 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6535 | return; |
| 6536 | } |
| 6537 | |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6538 | // Diagnose references or pointers to incomplete types differently, |
| 6539 | // since it's far from impossible that the incompleteness triggered |
| 6540 | // the failure. |
| 6541 | QualType TempFromTy = FromTy.getNonReferenceType(); |
| 6542 | if (const PointerType *PTy = TempFromTy->getAs<PointerType>()) |
| 6543 | TempFromTy = PTy->getPointeeType(); |
| 6544 | if (TempFromTy->isIncompleteType()) { |
| 6545 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv_incomplete) |
| 6546 | << (unsigned) FnKind << FnDesc |
| 6547 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
| 6548 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6549 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6d17464 | 2010-01-23 08:10:49 +0000 | [diff] [blame] | 6550 | return; |
| 6551 | } |
| 6552 | |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6553 | // Diagnose base -> derived pointer conversions. |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6554 | unsigned BaseToDerivedConversion = 0; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6555 | if (const PointerType *FromPtrTy = FromTy->getAs<PointerType>()) { |
| 6556 | if (const PointerType *ToPtrTy = ToTy->getAs<PointerType>()) { |
| 6557 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6558 | FromPtrTy->getPointeeType()) && |
| 6559 | !FromPtrTy->getPointeeType()->isIncompleteType() && |
| 6560 | !ToPtrTy->getPointeeType()->isIncompleteType() && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6561 | S.IsDerivedFrom(ToPtrTy->getPointeeType(), |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6562 | FromPtrTy->getPointeeType())) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6563 | BaseToDerivedConversion = 1; |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6564 | } |
| 6565 | } else if (const ObjCObjectPointerType *FromPtrTy |
| 6566 | = FromTy->getAs<ObjCObjectPointerType>()) { |
| 6567 | if (const ObjCObjectPointerType *ToPtrTy |
| 6568 | = ToTy->getAs<ObjCObjectPointerType>()) |
| 6569 | if (const ObjCInterfaceDecl *FromIface = FromPtrTy->getInterfaceDecl()) |
| 6570 | if (const ObjCInterfaceDecl *ToIface = ToPtrTy->getInterfaceDecl()) |
| 6571 | if (ToPtrTy->getPointeeType().isAtLeastAsQualifiedAs( |
| 6572 | FromPtrTy->getPointeeType()) && |
| 6573 | FromIface->isSuperClassOf(ToIface)) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6574 | BaseToDerivedConversion = 2; |
| 6575 | } else if (const ReferenceType *ToRefTy = ToTy->getAs<ReferenceType>()) { |
| 6576 | if (ToRefTy->getPointeeType().isAtLeastAsQualifiedAs(FromTy) && |
| 6577 | !FromTy->isIncompleteType() && |
| 6578 | !ToRefTy->getPointeeType()->isIncompleteType() && |
| 6579 | S.IsDerivedFrom(ToRefTy->getPointeeType(), FromTy)) |
| 6580 | BaseToDerivedConversion = 3; |
| 6581 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6582 | |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6583 | if (BaseToDerivedConversion) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6584 | S.Diag(Fn->getLocation(), |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6585 | diag::note_ovl_candidate_bad_base_to_derived_conv) |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6586 | << (unsigned) FnKind << FnDesc |
| 6587 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
Douglas Gregor | fb0c0d3 | 2010-07-01 02:14:45 +0000 | [diff] [blame] | 6588 | << (BaseToDerivedConversion - 1) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | << FromTy << ToTy << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6590 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 56f2e34 | 2010-06-30 23:01:39 +0000 | [diff] [blame] | 6591 | return; |
| 6592 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6593 | |
John McCall | 4700099 | 2010-01-14 03:28:57 +0000 | [diff] [blame] | 6594 | // TODO: specialize more based on the kind of mismatch |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6595 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_conv) |
| 6596 | << (unsigned) FnKind << FnDesc |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6597 | << (FromExpr ? FromExpr->getSourceRange() : SourceRange()) |
John McCall | a1709fd | 2010-01-14 00:56:20 +0000 | [diff] [blame] | 6598 | << FromTy << ToTy << (unsigned) isObjectArgument << I+1; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6599 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | void DiagnoseArityMismatch(Sema &S, OverloadCandidate *Cand, |
| 6603 | unsigned NumFormalArgs) { |
| 6604 | // TODO: treat calls to a missing default constructor as a special case |
| 6605 | |
| 6606 | FunctionDecl *Fn = Cand->Function; |
| 6607 | const FunctionProtoType *FnTy = Fn->getType()->getAs<FunctionProtoType>(); |
| 6608 | |
| 6609 | unsigned MinParams = Fn->getMinRequiredArguments(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6610 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6611 | // at least / at most / exactly |
| 6612 | unsigned mode, modeCount; |
| 6613 | if (NumFormalArgs < MinParams) { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6614 | assert((Cand->FailureKind == ovl_fail_too_few_arguments) || |
| 6615 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6616 | Cand->DeductionFailure.Result == Sema::TDK_TooFewArguments)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6617 | if (MinParams != FnTy->getNumArgs() || |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 6618 | FnTy->isVariadic() || FnTy->isTemplateVariadic()) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6619 | mode = 0; // "at least" |
| 6620 | else |
| 6621 | mode = 2; // "exactly" |
| 6622 | modeCount = MinParams; |
| 6623 | } else { |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6624 | assert((Cand->FailureKind == ovl_fail_too_many_arguments) || |
| 6625 | (Cand->FailureKind == ovl_fail_bad_deduction && |
| 6626 | Cand->DeductionFailure.Result == Sema::TDK_TooManyArguments)); |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6627 | if (MinParams != FnTy->getNumArgs()) |
| 6628 | mode = 1; // "at most" |
| 6629 | else |
| 6630 | mode = 2; // "exactly" |
| 6631 | modeCount = FnTy->getNumArgs(); |
| 6632 | } |
| 6633 | |
| 6634 | std::string Description; |
| 6635 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, Description); |
| 6636 | |
| 6637 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6638 | << (unsigned) FnKind << (Fn->getDescribedFunctionTemplate() != 0) << mode |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6639 | << modeCount << NumFormalArgs; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6640 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6643 | /// Diagnose a failed template-argument deduction. |
| 6644 | void DiagnoseBadDeduction(Sema &S, OverloadCandidate *Cand, |
| 6645 | Expr **Args, unsigned NumArgs) { |
| 6646 | FunctionDecl *Fn = Cand->Function; // pattern |
| 6647 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6648 | TemplateParameter Param = Cand->DeductionFailure.getTemplateParameter(); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6649 | NamedDecl *ParamD; |
| 6650 | (ParamD = Param.dyn_cast<TemplateTypeParmDecl*>()) || |
| 6651 | (ParamD = Param.dyn_cast<NonTypeTemplateParmDecl*>()) || |
| 6652 | (ParamD = Param.dyn_cast<TemplateTemplateParmDecl*>()); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6653 | switch (Cand->DeductionFailure.Result) { |
| 6654 | case Sema::TDK_Success: |
| 6655 | llvm_unreachable("TDK_success while diagnosing bad deduction"); |
| 6656 | |
| 6657 | case Sema::TDK_Incomplete: { |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6658 | assert(ParamD && "no parameter found for incomplete deduction result"); |
| 6659 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_incomplete_deduction) |
| 6660 | << ParamD->getDeclName(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6661 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6662 | return; |
| 6663 | } |
| 6664 | |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6665 | case Sema::TDK_Underqualified: { |
| 6666 | assert(ParamD && "no parameter found for bad qualifiers deduction result"); |
| 6667 | TemplateTypeParmDecl *TParam = cast<TemplateTypeParmDecl>(ParamD); |
| 6668 | |
| 6669 | QualType Param = Cand->DeductionFailure.getFirstArg()->getAsType(); |
| 6670 | |
| 6671 | // Param will have been canonicalized, but it should just be a |
| 6672 | // qualified version of ParamD, so move the qualifiers to that. |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6673 | QualifierCollector Qs; |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6674 | Qs.strip(Param); |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6675 | QualType NonCanonParam = Qs.apply(S.Context, TParam->getTypeForDecl()); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6676 | assert(S.Context.hasSameType(Param, NonCanonParam)); |
| 6677 | |
| 6678 | // Arg has also been canonicalized, but there's nothing we can do |
| 6679 | // about that. It also doesn't matter as much, because it won't |
| 6680 | // have any template parameters in it (because deduction isn't |
| 6681 | // done on dependent types). |
| 6682 | QualType Arg = Cand->DeductionFailure.getSecondArg()->getAsType(); |
| 6683 | |
| 6684 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_underqualified) |
| 6685 | << ParamD->getDeclName() << Arg << NonCanonParam; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6686 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 6687 | return; |
| 6688 | } |
| 6689 | |
| 6690 | case Sema::TDK_Inconsistent: { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 6691 | assert(ParamD && "no parameter found for inconsistent deduction result"); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6692 | int which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6693 | if (isa<TemplateTypeParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6694 | which = 0; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6695 | else if (isa<NonTypeTemplateParmDecl>(ParamD)) |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6696 | which = 1; |
| 6697 | else { |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6698 | which = 2; |
| 6699 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6700 | |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6701 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_inconsistent_deduction) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | << which << ParamD->getDeclName() |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6703 | << *Cand->DeductionFailure.getFirstArg() |
| 6704 | << *Cand->DeductionFailure.getSecondArg(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6705 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 3626a5c | 2010-05-08 17:41:32 +0000 | [diff] [blame] | 6706 | return; |
| 6707 | } |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6708 | |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6709 | case Sema::TDK_InvalidExplicitArguments: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6710 | assert(ParamD && "no parameter found for invalid explicit arguments"); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6711 | if (ParamD->getDeclName()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6712 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6713 | diag::note_ovl_candidate_explicit_arg_mismatch_named) |
| 6714 | << ParamD->getDeclName(); |
| 6715 | else { |
| 6716 | int index = 0; |
| 6717 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ParamD)) |
| 6718 | index = TTP->getIndex(); |
| 6719 | else if (NonTypeTemplateParmDecl *NTTP |
| 6720 | = dyn_cast<NonTypeTemplateParmDecl>(ParamD)) |
| 6721 | index = NTTP->getIndex(); |
| 6722 | else |
| 6723 | index = cast<TemplateTemplateParmDecl>(ParamD)->getIndex(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6724 | S.Diag(Fn->getLocation(), |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6725 | diag::note_ovl_candidate_explicit_arg_mismatch_unnamed) |
| 6726 | << (index + 1); |
| 6727 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6728 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 6729 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6730 | |
Douglas Gregor | 02eb483 | 2010-05-08 18:13:28 +0000 | [diff] [blame] | 6731 | case Sema::TDK_TooManyArguments: |
| 6732 | case Sema::TDK_TooFewArguments: |
| 6733 | DiagnoseArityMismatch(S, Cand, NumArgs); |
| 6734 | return; |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6735 | |
| 6736 | case Sema::TDK_InstantiationDepth: |
| 6737 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_instantiation_depth); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6738 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6739 | return; |
| 6740 | |
| 6741 | case Sema::TDK_SubstitutionFailure: { |
| 6742 | std::string ArgString; |
| 6743 | if (TemplateArgumentList *Args |
| 6744 | = Cand->DeductionFailure.getTemplateArgumentList()) |
| 6745 | ArgString = S.getTemplateArgumentBindingsText( |
| 6746 | Fn->getDescribedFunctionTemplate()->getTemplateParameters(), |
| 6747 | *Args); |
| 6748 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_substitution_failure) |
| 6749 | << ArgString; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6750 | MaybeEmitInheritedConstructorNote(S, Fn); |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 6751 | return; |
| 6752 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6753 | |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6754 | // TODO: diagnose these individually, then kill off |
| 6755 | // note_ovl_candidate_bad_deduction, which is uselessly vague. |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6756 | case Sema::TDK_NonDeducedMismatch: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6757 | case Sema::TDK_FailedOverloadResolution: |
| 6758 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_bad_deduction); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6759 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6760 | return; |
| 6761 | } |
| 6762 | } |
| 6763 | |
| 6764 | /// Generates a 'note' diagnostic for an overload candidate. We've |
| 6765 | /// already generated a primary error at the call site. |
| 6766 | /// |
| 6767 | /// It really does need to be a single diagnostic with its caret |
| 6768 | /// pointed at the candidate declaration. Yes, this creates some |
| 6769 | /// major challenges of technical writing. Yes, this makes pointing |
| 6770 | /// out problems with specific arguments quite awkward. It's still |
| 6771 | /// better than generating twenty screens of text for every failed |
| 6772 | /// overload. |
| 6773 | /// |
| 6774 | /// It would be great to be able to express per-candidate problems |
| 6775 | /// more richly for those diagnostic clients that cared, but we'd |
| 6776 | /// still have to be just as careful with the default diagnostics. |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6777 | void NoteFunctionCandidate(Sema &S, OverloadCandidate *Cand, |
| 6778 | Expr **Args, unsigned NumArgs) { |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6779 | FunctionDecl *Fn = Cand->Function; |
| 6780 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6781 | // Note deleted candidates, but only if they're viable. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6782 | if (Cand->Viable && (Fn->isDeleted() || Fn->isUnavailable())) { |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6783 | std::string FnDesc; |
| 6784 | OverloadCandidateKind FnKind = ClassifyOverloadCandidate(S, Fn, FnDesc); |
John McCall | 53262c9 | 2010-01-12 02:15:36 +0000 | [diff] [blame] | 6785 | |
| 6786 | S.Diag(Fn->getLocation(), diag::note_ovl_candidate_deleted) |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6787 | << FnKind << FnDesc << Fn->isDeleted(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6788 | MaybeEmitInheritedConstructorNote(S, Fn); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6789 | return; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6790 | } |
| 6791 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6792 | // We don't really have anything else to say about viable candidates. |
| 6793 | if (Cand->Viable) { |
| 6794 | S.NoteOverloadCandidate(Fn); |
| 6795 | return; |
| 6796 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6797 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6798 | switch (Cand->FailureKind) { |
| 6799 | case ovl_fail_too_many_arguments: |
| 6800 | case ovl_fail_too_few_arguments: |
| 6801 | return DiagnoseArityMismatch(S, Cand, NumArgs); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6802 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6803 | case ovl_fail_bad_deduction: |
John McCall | 8b9ed55 | 2010-02-01 18:53:26 +0000 | [diff] [blame] | 6804 | return DiagnoseBadDeduction(S, Cand, Args, NumArgs); |
| 6805 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6806 | case ovl_fail_trivial_conversion: |
| 6807 | case ovl_fail_bad_final_conversion: |
Douglas Gregor | 2c326bc | 2010-04-12 23:42:09 +0000 | [diff] [blame] | 6808 | case ovl_fail_final_conversion_not_exact: |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6809 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6810 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6811 | case ovl_fail_bad_conversion: { |
| 6812 | unsigned I = (Cand->IgnoreObjectArgument ? 1 : 0); |
| 6813 | for (unsigned N = Cand->Conversions.size(); I != N; ++I) |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6814 | if (Cand->Conversions[I].isBad()) |
| 6815 | return DiagnoseBadConversion(S, Cand, I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6816 | |
John McCall | 6a61b52 | 2010-01-13 09:16:55 +0000 | [diff] [blame] | 6817 | // FIXME: this currently happens when we're called from SemaInit |
| 6818 | // when user-conversion overload fails. Figure out how to handle |
| 6819 | // those conditions and diagnose them well. |
| 6820 | return S.NoteOverloadCandidate(Fn); |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 6821 | } |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6822 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | void NoteSurrogateCandidate(Sema &S, OverloadCandidate *Cand) { |
| 6826 | // Desugar the type of the surrogate down to a function type, |
| 6827 | // retaining as many typedefs as possible while still showing |
| 6828 | // the function type (and, therefore, its parameter types). |
| 6829 | QualType FnType = Cand->Surrogate->getConversionType(); |
| 6830 | bool isLValueReference = false; |
| 6831 | bool isRValueReference = false; |
| 6832 | bool isPointer = false; |
| 6833 | if (const LValueReferenceType *FnTypeRef = |
| 6834 | FnType->getAs<LValueReferenceType>()) { |
| 6835 | FnType = FnTypeRef->getPointeeType(); |
| 6836 | isLValueReference = true; |
| 6837 | } else if (const RValueReferenceType *FnTypeRef = |
| 6838 | FnType->getAs<RValueReferenceType>()) { |
| 6839 | FnType = FnTypeRef->getPointeeType(); |
| 6840 | isRValueReference = true; |
| 6841 | } |
| 6842 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
| 6843 | FnType = FnTypePtr->getPointeeType(); |
| 6844 | isPointer = true; |
| 6845 | } |
| 6846 | // Desugar down to a function type. |
| 6847 | FnType = QualType(FnType->getAs<FunctionType>(), 0); |
| 6848 | // Reconstruct the pointer/reference as appropriate. |
| 6849 | if (isPointer) FnType = S.Context.getPointerType(FnType); |
| 6850 | if (isRValueReference) FnType = S.Context.getRValueReferenceType(FnType); |
| 6851 | if (isLValueReference) FnType = S.Context.getLValueReferenceType(FnType); |
| 6852 | |
| 6853 | S.Diag(Cand->Surrogate->getLocation(), diag::note_ovl_surrogate_cand) |
| 6854 | << FnType; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6855 | MaybeEmitInheritedConstructorNote(S, Cand->Surrogate); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
| 6858 | void NoteBuiltinOperatorCandidate(Sema &S, |
| 6859 | const char *Opc, |
| 6860 | SourceLocation OpLoc, |
| 6861 | OverloadCandidate *Cand) { |
| 6862 | assert(Cand->Conversions.size() <= 2 && "builtin operator is not binary"); |
| 6863 | std::string TypeStr("operator"); |
| 6864 | TypeStr += Opc; |
| 6865 | TypeStr += "("; |
| 6866 | TypeStr += Cand->BuiltinTypes.ParamTypes[0].getAsString(); |
| 6867 | if (Cand->Conversions.size() == 1) { |
| 6868 | TypeStr += ")"; |
| 6869 | S.Diag(OpLoc, diag::note_ovl_builtin_unary_candidate) << TypeStr; |
| 6870 | } else { |
| 6871 | TypeStr += ", "; |
| 6872 | TypeStr += Cand->BuiltinTypes.ParamTypes[1].getAsString(); |
| 6873 | TypeStr += ")"; |
| 6874 | S.Diag(OpLoc, diag::note_ovl_builtin_binary_candidate) << TypeStr; |
| 6875 | } |
| 6876 | } |
| 6877 | |
| 6878 | void NoteAmbiguousUserConversions(Sema &S, SourceLocation OpLoc, |
| 6879 | OverloadCandidate *Cand) { |
| 6880 | unsigned NoOperands = Cand->Conversions.size(); |
| 6881 | for (unsigned ArgIdx = 0; ArgIdx < NoOperands; ++ArgIdx) { |
| 6882 | const ImplicitConversionSequence &ICS = Cand->Conversions[ArgIdx]; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 6883 | if (ICS.isBad()) break; // all meaningless after first invalid |
| 6884 | if (!ICS.isAmbiguous()) continue; |
| 6885 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6886 | ICS.DiagnoseAmbiguousConversion(S, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6887 | S.PDiag(diag::note_ambiguous_type_conversion)); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 6888 | } |
| 6889 | } |
| 6890 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6891 | SourceLocation GetLocationForCandidate(const OverloadCandidate *Cand) { |
| 6892 | if (Cand->Function) |
| 6893 | return Cand->Function->getLocation(); |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 6894 | if (Cand->IsSurrogate) |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6895 | return Cand->Surrogate->getLocation(); |
| 6896 | return SourceLocation(); |
| 6897 | } |
| 6898 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6899 | struct CompareOverloadCandidatesForDisplay { |
| 6900 | Sema &S; |
| 6901 | CompareOverloadCandidatesForDisplay(Sema &S) : S(S) {} |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6902 | |
| 6903 | bool operator()(const OverloadCandidate *L, |
| 6904 | const OverloadCandidate *R) { |
John McCall | 982adb5 | 2010-01-16 03:50:16 +0000 | [diff] [blame] | 6905 | // Fast-path this check. |
| 6906 | if (L == R) return false; |
| 6907 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6908 | // Order first by viability. |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6909 | if (L->Viable) { |
| 6910 | if (!R->Viable) return true; |
| 6911 | |
| 6912 | // TODO: introduce a tri-valued comparison for overload |
| 6913 | // candidates. Would be more worthwhile if we had a sort |
| 6914 | // that could exploit it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6915 | if (isBetterOverloadCandidate(S, *L, *R, SourceLocation())) return true; |
| 6916 | if (isBetterOverloadCandidate(S, *R, *L, SourceLocation())) return false; |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 6917 | } else if (R->Viable) |
| 6918 | return false; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6919 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6920 | assert(L->Viable == R->Viable); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6921 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6922 | // Criteria by which we can sort non-viable candidates: |
| 6923 | if (!L->Viable) { |
| 6924 | // 1. Arity mismatches come after other candidates. |
| 6925 | if (L->FailureKind == ovl_fail_too_many_arguments || |
| 6926 | L->FailureKind == ovl_fail_too_few_arguments) |
| 6927 | return false; |
| 6928 | if (R->FailureKind == ovl_fail_too_many_arguments || |
| 6929 | R->FailureKind == ovl_fail_too_few_arguments) |
| 6930 | return true; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6931 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6932 | // 2. Bad conversions come first and are ordered by the number |
| 6933 | // of bad conversions and quality of good conversions. |
| 6934 | if (L->FailureKind == ovl_fail_bad_conversion) { |
| 6935 | if (R->FailureKind != ovl_fail_bad_conversion) |
| 6936 | return true; |
| 6937 | |
| 6938 | // If there's any ordering between the defined conversions... |
| 6939 | // FIXME: this might not be transitive. |
| 6940 | assert(L->Conversions.size() == R->Conversions.size()); |
| 6941 | |
| 6942 | int leftBetter = 0; |
John McCall | 21b57fa | 2010-02-25 10:46:05 +0000 | [diff] [blame] | 6943 | unsigned I = (L->IgnoreObjectArgument || R->IgnoreObjectArgument); |
| 6944 | for (unsigned E = L->Conversions.size(); I != E; ++I) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6945 | switch (CompareImplicitConversionSequences(S, |
| 6946 | L->Conversions[I], |
| 6947 | R->Conversions[I])) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6948 | case ImplicitConversionSequence::Better: |
| 6949 | leftBetter++; |
| 6950 | break; |
| 6951 | |
| 6952 | case ImplicitConversionSequence::Worse: |
| 6953 | leftBetter--; |
| 6954 | break; |
| 6955 | |
| 6956 | case ImplicitConversionSequence::Indistinguishable: |
| 6957 | break; |
| 6958 | } |
| 6959 | } |
| 6960 | if (leftBetter > 0) return true; |
| 6961 | if (leftBetter < 0) return false; |
| 6962 | |
| 6963 | } else if (R->FailureKind == ovl_fail_bad_conversion) |
| 6964 | return false; |
| 6965 | |
John McCall | 3712d9e | 2010-01-15 23:32:50 +0000 | [diff] [blame] | 6966 | // TODO: others? |
| 6967 | } |
| 6968 | |
| 6969 | // Sort everything else by location. |
| 6970 | SourceLocation LLoc = GetLocationForCandidate(L); |
| 6971 | SourceLocation RLoc = GetLocationForCandidate(R); |
| 6972 | |
| 6973 | // Put candidates without locations (e.g. builtins) at the end. |
| 6974 | if (LLoc.isInvalid()) return false; |
| 6975 | if (RLoc.isInvalid()) return true; |
| 6976 | |
| 6977 | return S.SourceMgr.isBeforeInTranslationUnit(LLoc, RLoc); |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 6978 | } |
| 6979 | }; |
| 6980 | |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6981 | /// CompleteNonViableCandidate - Normally, overload resolution only |
| 6982 | /// computes up to the first |
| 6983 | void CompleteNonViableCandidate(Sema &S, OverloadCandidate *Cand, |
| 6984 | Expr **Args, unsigned NumArgs) { |
| 6985 | assert(!Cand->Viable); |
| 6986 | |
| 6987 | // Don't do anything on failures other than bad conversion. |
| 6988 | if (Cand->FailureKind != ovl_fail_bad_conversion) return; |
| 6989 | |
| 6990 | // Skip forward to the first bad conversion. |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 6991 | unsigned ConvIdx = (Cand->IgnoreObjectArgument ? 1 : 0); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 6992 | unsigned ConvCount = Cand->Conversions.size(); |
| 6993 | while (true) { |
| 6994 | assert(ConvIdx != ConvCount && "no bad conversion in candidate"); |
| 6995 | ConvIdx++; |
| 6996 | if (Cand->Conversions[ConvIdx - 1].isBad()) |
| 6997 | break; |
| 6998 | } |
| 6999 | |
| 7000 | if (ConvIdx == ConvCount) |
| 7001 | return; |
| 7002 | |
John McCall | 65eb879 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 7003 | assert(!Cand->Conversions[ConvIdx].isInitialized() && |
| 7004 | "remaining conversion is initialized?"); |
| 7005 | |
Douglas Gregor | adc7a70 | 2010-04-16 17:45:54 +0000 | [diff] [blame] | 7006 | // FIXME: this should probably be preserved from the overload |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7007 | // operation somehow. |
| 7008 | bool SuppressUserConversions = false; |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7009 | |
| 7010 | const FunctionProtoType* Proto; |
| 7011 | unsigned ArgIdx = ConvIdx; |
| 7012 | |
| 7013 | if (Cand->IsSurrogate) { |
| 7014 | QualType ConvType |
| 7015 | = Cand->Surrogate->getConversionType().getNonReferenceType(); |
| 7016 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 7017 | ConvType = ConvPtrType->getPointeeType(); |
| 7018 | Proto = ConvType->getAs<FunctionProtoType>(); |
| 7019 | ArgIdx--; |
| 7020 | } else if (Cand->Function) { |
| 7021 | Proto = Cand->Function->getType()->getAs<FunctionProtoType>(); |
| 7022 | if (isa<CXXMethodDecl>(Cand->Function) && |
| 7023 | !isa<CXXConstructorDecl>(Cand->Function)) |
| 7024 | ArgIdx--; |
| 7025 | } else { |
| 7026 | // Builtin binary operator with a bad first conversion. |
| 7027 | assert(ConvCount <= 3); |
| 7028 | for (; ConvIdx != ConvCount; ++ConvIdx) |
| 7029 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7030 | = TryCopyInitialization(S, Args[ConvIdx], |
| 7031 | Cand->BuiltinTypes.ParamTypes[ConvIdx], |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7032 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7033 | /*InOverloadResolution*/ true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7034 | return; |
| 7035 | } |
| 7036 | |
| 7037 | // Fill in the rest of the conversions. |
| 7038 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 7039 | for (; ConvIdx != ConvCount; ++ConvIdx, ++ArgIdx) { |
| 7040 | if (ArgIdx < NumArgsInProto) |
| 7041 | Cand->Conversions[ConvIdx] |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7042 | = TryCopyInitialization(S, Args[ArgIdx], Proto->getArgType(ArgIdx), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7043 | SuppressUserConversions, |
Douglas Gregor | cb13cfc | 2010-04-16 17:51:22 +0000 | [diff] [blame] | 7044 | /*InOverloadResolution=*/true); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7045 | else |
| 7046 | Cand->Conversions[ConvIdx].setEllipsis(); |
| 7047 | } |
| 7048 | } |
| 7049 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7050 | } // end anonymous namespace |
| 7051 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7052 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 7053 | /// diagnostic messages containing the candidates in the candidate |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7054 | /// set. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7055 | void OverloadCandidateSet::NoteCandidates(Sema &S, |
| 7056 | OverloadCandidateDisplayKind OCD, |
| 7057 | Expr **Args, unsigned NumArgs, |
| 7058 | const char *Opc, |
| 7059 | SourceLocation OpLoc) { |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7060 | // Sort the candidates by viability and position. Sorting directly would |
| 7061 | // be prohibitive, so we make a set of pointers and sort those. |
| 7062 | llvm::SmallVector<OverloadCandidate*, 32> Cands; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7063 | if (OCD == OCD_AllCandidates) Cands.reserve(size()); |
| 7064 | for (iterator Cand = begin(), LastCand = end(); Cand != LastCand; ++Cand) { |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7065 | if (Cand->Viable) |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7066 | Cands.push_back(Cand); |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7067 | else if (OCD == OCD_AllCandidates) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7068 | CompleteNonViableCandidate(S, Cand, Args, NumArgs); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7069 | if (Cand->Function || Cand->IsSurrogate) |
| 7070 | Cands.push_back(Cand); |
| 7071 | // Otherwise, this a non-viable builtin candidate. We do not, in general, |
| 7072 | // want to list every possible builtin candidate. |
John McCall | fe796dd | 2010-01-23 05:17:32 +0000 | [diff] [blame] | 7073 | } |
| 7074 | } |
| 7075 | |
John McCall | ad2587a | 2010-01-12 00:48:53 +0000 | [diff] [blame] | 7076 | std::sort(Cands.begin(), Cands.end(), |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7077 | CompareOverloadCandidatesForDisplay(S)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7078 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7079 | bool ReportedAmbiguousConversions = false; |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7080 | |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7081 | llvm::SmallVectorImpl<OverloadCandidate*>::iterator I, E; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7082 | const Diagnostic::OverloadsShown ShowOverloads = S.Diags.getShowOverloads(); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7083 | unsigned CandsShown = 0; |
John McCall | 12f97bc | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 7084 | for (I = Cands.begin(), E = Cands.end(); I != E; ++I) { |
| 7085 | OverloadCandidate *Cand = *I; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 7086 | |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7087 | // Set an arbitrary limit on the number of candidate functions we'll spam |
| 7088 | // the user with. FIXME: This limit should depend on details of the |
| 7089 | // candidate list. |
| 7090 | if (CandsShown >= 4 && ShowOverloads == Diagnostic::Ovl_Best) { |
| 7091 | break; |
| 7092 | } |
| 7093 | ++CandsShown; |
| 7094 | |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7095 | if (Cand->Function) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7096 | NoteFunctionCandidate(S, Cand, Args, NumArgs); |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7097 | else if (Cand->IsSurrogate) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7098 | NoteSurrogateCandidate(S, Cand); |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7099 | else { |
| 7100 | assert(Cand->Viable && |
| 7101 | "Non-viable built-in candidates are not added to Cands."); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7102 | // Generally we only see ambiguities including viable builtin |
| 7103 | // operators if overload resolution got screwed up by an |
| 7104 | // ambiguous user-defined conversion. |
| 7105 | // |
| 7106 | // FIXME: It's quite possible for different conversions to see |
| 7107 | // different ambiguities, though. |
| 7108 | if (!ReportedAmbiguousConversions) { |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7109 | NoteAmbiguousUserConversions(S, OpLoc, Cand); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7110 | ReportedAmbiguousConversions = true; |
| 7111 | } |
John McCall | d322416 | 2010-01-08 00:58:21 +0000 | [diff] [blame] | 7112 | |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 7113 | // If this is a viable builtin, print it. |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7114 | NoteBuiltinOperatorCandidate(S, Opc, OpLoc, Cand); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 7115 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7116 | } |
Jeffrey Yasskin | 2b99c6f | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 7117 | |
| 7118 | if (I != E) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7119 | S.Diag(OpLoc, diag::note_ovl_too_many_candidates) << int(E - I); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7122 | // [PossiblyAFunctionType] --> [Return] |
| 7123 | // NonFunctionType --> NonFunctionType |
| 7124 | // R (A) --> R(A) |
| 7125 | // R (*)(A) --> R (A) |
| 7126 | // R (&)(A) --> R (A) |
| 7127 | // R (S::*)(A) --> R (A) |
| 7128 | QualType Sema::ExtractUnqualifiedFunctionType(QualType PossiblyAFunctionType) { |
| 7129 | QualType Ret = PossiblyAFunctionType; |
| 7130 | if (const PointerType *ToTypePtr = |
| 7131 | PossiblyAFunctionType->getAs<PointerType>()) |
| 7132 | Ret = ToTypePtr->getPointeeType(); |
| 7133 | else if (const ReferenceType *ToTypeRef = |
| 7134 | PossiblyAFunctionType->getAs<ReferenceType>()) |
| 7135 | Ret = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7136 | else if (const MemberPointerType *MemTypePtr = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7137 | PossiblyAFunctionType->getAs<MemberPointerType>()) |
| 7138 | Ret = MemTypePtr->getPointeeType(); |
| 7139 | Ret = |
| 7140 | Context.getCanonicalType(Ret).getUnqualifiedType(); |
| 7141 | return Ret; |
| 7142 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7143 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7144 | // A helper class to help with address of function resolution |
| 7145 | // - allows us to avoid passing around all those ugly parameters |
| 7146 | class AddressOfFunctionResolver |
| 7147 | { |
| 7148 | Sema& S; |
| 7149 | Expr* SourceExpr; |
| 7150 | const QualType& TargetType; |
| 7151 | QualType TargetFunctionType; // Extracted function type from target type |
| 7152 | |
| 7153 | bool Complain; |
| 7154 | //DeclAccessPair& ResultFunctionAccessPair; |
| 7155 | ASTContext& Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7156 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7157 | bool TargetTypeIsNonStaticMemberFunction; |
| 7158 | bool FoundNonTemplateFunction; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7159 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7160 | OverloadExpr::FindResult OvlExprInfo; |
| 7161 | OverloadExpr *OvlExpr; |
| 7162 | TemplateArgumentListInfo OvlExplicitTemplateArgs; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7163 | llvm::SmallVector<std::pair<DeclAccessPair, FunctionDecl*>, 4> Matches; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7164 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7165 | public: |
| 7166 | AddressOfFunctionResolver(Sema &S, Expr* SourceExpr, |
| 7167 | const QualType& TargetType, bool Complain) |
| 7168 | : S(S), SourceExpr(SourceExpr), TargetType(TargetType), |
| 7169 | Complain(Complain), Context(S.getASTContext()), |
| 7170 | TargetTypeIsNonStaticMemberFunction( |
| 7171 | !!TargetType->getAs<MemberPointerType>()), |
| 7172 | FoundNonTemplateFunction(false), |
| 7173 | OvlExprInfo(OverloadExpr::find(SourceExpr)), |
| 7174 | OvlExpr(OvlExprInfo.Expression) |
| 7175 | { |
| 7176 | ExtractUnqualifiedFunctionTypeFromTargetType(); |
| 7177 | |
| 7178 | if (!TargetFunctionType->isFunctionType()) { |
| 7179 | if (OvlExpr->hasExplicitTemplateArgs()) { |
| 7180 | DeclAccessPair dap; |
| 7181 | if( FunctionDecl* Fn = S.ResolveSingleFunctionTemplateSpecialization( |
| 7182 | OvlExpr, false, &dap) ) { |
Chandler Carruth | ffce245 | 2011-03-29 08:08:18 +0000 | [diff] [blame] | 7183 | |
| 7184 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 7185 | if (!Method->isStatic()) { |
| 7186 | // If the target type is a non-function type and the function |
| 7187 | // found is a non-static member function, pretend as if that was |
| 7188 | // the target, it's the only possible type to end up with. |
| 7189 | TargetTypeIsNonStaticMemberFunction = true; |
| 7190 | |
| 7191 | // And skip adding the function if its not in the proper form. |
| 7192 | // We'll diagnose this due to an empty set of functions. |
| 7193 | if (!OvlExprInfo.HasFormOfMemberPointer) |
| 7194 | return; |
| 7195 | } |
| 7196 | } |
| 7197 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7198 | Matches.push_back(std::make_pair(dap,Fn)); |
| 7199 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7200 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7201 | return; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 7202 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7203 | |
| 7204 | if (OvlExpr->hasExplicitTemplateArgs()) |
| 7205 | OvlExpr->getExplicitTemplateArgs().copyInto(OvlExplicitTemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7206 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7207 | if (FindAllFunctionsThatMatchTargetTypeExactly()) { |
| 7208 | // C++ [over.over]p4: |
| 7209 | // If more than one function is selected, [...] |
| 7210 | if (Matches.size() > 1) { |
| 7211 | if (FoundNonTemplateFunction) |
| 7212 | EliminateAllTemplateMatches(); |
| 7213 | else |
| 7214 | EliminateAllExceptMostSpecializedTemplate(); |
| 7215 | } |
| 7216 | } |
| 7217 | } |
| 7218 | |
| 7219 | private: |
| 7220 | bool isTargetTypeAFunction() const { |
| 7221 | return TargetFunctionType->isFunctionType(); |
| 7222 | } |
| 7223 | |
| 7224 | // [ToType] [Return] |
| 7225 | |
| 7226 | // R (*)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7227 | // R (&)(A) --> R (A), IsNonStaticMemberFunction = false |
| 7228 | // R (S::*)(A) --> R (A), IsNonStaticMemberFunction = true |
| 7229 | void inline ExtractUnqualifiedFunctionTypeFromTargetType() { |
| 7230 | TargetFunctionType = S.ExtractUnqualifiedFunctionType(TargetType); |
| 7231 | } |
| 7232 | |
| 7233 | // return true if any matching specializations were found |
| 7234 | bool AddMatchingTemplateFunction(FunctionTemplateDecl* FunctionTemplate, |
| 7235 | const DeclAccessPair& CurAccessFunPair) { |
| 7236 | if (CXXMethodDecl *Method |
| 7237 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
| 7238 | // Skip non-static function templates when converting to pointer, and |
| 7239 | // static when converting to member pointer. |
| 7240 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7241 | return false; |
| 7242 | } |
| 7243 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7244 | return false; |
| 7245 | |
| 7246 | // C++ [over.over]p2: |
| 7247 | // If the name is a function template, template argument deduction is |
| 7248 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7249 | // resulting template argument list is used to generate a single |
| 7250 | // function template specialization, which is added to the set of |
| 7251 | // overloaded functions considered. |
| 7252 | FunctionDecl *Specialization = 0; |
| 7253 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
| 7254 | if (Sema::TemplateDeductionResult Result |
| 7255 | = S.DeduceTemplateArguments(FunctionTemplate, |
| 7256 | &OvlExplicitTemplateArgs, |
| 7257 | TargetFunctionType, Specialization, |
| 7258 | Info)) { |
| 7259 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7260 | (void)Result; |
| 7261 | return false; |
| 7262 | } |
| 7263 | |
| 7264 | // Template argument deduction ensures that we have an exact match. |
| 7265 | // This function template specicalization works. |
| 7266 | Specialization = cast<FunctionDecl>(Specialization->getCanonicalDecl()); |
| 7267 | assert(TargetFunctionType |
| 7268 | == Context.getCanonicalType(Specialization->getType())); |
| 7269 | Matches.push_back(std::make_pair(CurAccessFunPair, Specialization)); |
| 7270 | return true; |
| 7271 | } |
| 7272 | |
| 7273 | bool AddMatchingNonTemplateFunction(NamedDecl* Fn, |
| 7274 | const DeclAccessPair& CurAccessFunPair) { |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7275 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7276 | // Skip non-static functions when converting to pointer, and static |
| 7277 | // when converting to member pointer. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7278 | if (Method->isStatic() == TargetTypeIsNonStaticMemberFunction) |
| 7279 | return false; |
| 7280 | } |
| 7281 | else if (TargetTypeIsNonStaticMemberFunction) |
| 7282 | return false; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7283 | |
Chandler Carruth | c25c6ee | 2009-12-29 06:17:27 +0000 | [diff] [blame] | 7284 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(Fn)) { |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7285 | QualType ResultTy; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7286 | if (Context.hasSameUnqualifiedType(TargetFunctionType, |
| 7287 | FunDecl->getType()) || |
| 7288 | IsNoReturnConversion(Context, FunDecl->getType(), TargetFunctionType, |
Douglas Gregor | 40cb9ad | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 7289 | ResultTy)) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7290 | Matches.push_back(std::make_pair(CurAccessFunPair, |
| 7291 | cast<FunctionDecl>(FunDecl->getCanonicalDecl()))); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7292 | FoundNonTemplateFunction = true; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7293 | return true; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 7294 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7295 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7296 | |
| 7297 | return false; |
| 7298 | } |
| 7299 | |
| 7300 | bool FindAllFunctionsThatMatchTargetTypeExactly() { |
| 7301 | bool Ret = false; |
| 7302 | |
| 7303 | // If the overload expression doesn't have the form of a pointer to |
| 7304 | // member, don't try to convert it to a pointer-to-member type. |
| 7305 | if (IsInvalidFormOfPointerToMemberFunction()) |
| 7306 | return false; |
| 7307 | |
| 7308 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7309 | E = OvlExpr->decls_end(); |
| 7310 | I != E; ++I) { |
| 7311 | // Look through any using declarations to find the underlying function. |
| 7312 | NamedDecl *Fn = (*I)->getUnderlyingDecl(); |
| 7313 | |
| 7314 | // C++ [over.over]p3: |
| 7315 | // Non-member functions and static member functions match |
| 7316 | // targets of type "pointer-to-function" or "reference-to-function." |
| 7317 | // Nonstatic member functions match targets of |
| 7318 | // type "pointer-to-member-function." |
| 7319 | // Note that according to DR 247, the containing class does not matter. |
| 7320 | if (FunctionTemplateDecl *FunctionTemplate |
| 7321 | = dyn_cast<FunctionTemplateDecl>(Fn)) { |
| 7322 | if (AddMatchingTemplateFunction(FunctionTemplate, I.getPair())) |
| 7323 | Ret = true; |
| 7324 | } |
| 7325 | // If we have explicit template arguments supplied, skip non-templates. |
| 7326 | else if (!OvlExpr->hasExplicitTemplateArgs() && |
| 7327 | AddMatchingNonTemplateFunction(Fn, I.getPair())) |
| 7328 | Ret = true; |
| 7329 | } |
| 7330 | assert(Ret || Matches.empty()); |
| 7331 | return Ret; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7334 | void EliminateAllExceptMostSpecializedTemplate() { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 7335 | // [...] and any given function template specialization F1 is |
| 7336 | // eliminated if the set contains a second function template |
| 7337 | // specialization whose function template is more specialized |
| 7338 | // than the function template of F1 according to the partial |
| 7339 | // ordering rules of 14.5.5.2. |
| 7340 | |
| 7341 | // The algorithm specified above is quadratic. We instead use a |
| 7342 | // two-pass algorithm (similar to the one used to identify the |
| 7343 | // best viable function in an overload set) that identifies the |
| 7344 | // best function template (if it exists). |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7345 | |
| 7346 | UnresolvedSet<4> MatchesCopy; // TODO: avoid! |
| 7347 | for (unsigned I = 0, E = Matches.size(); I != E; ++I) |
| 7348 | MatchesCopy.addDecl(Matches[I].second, Matches[I].first.getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7349 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7350 | UnresolvedSetIterator Result = |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7351 | S.getMostSpecialized(MatchesCopy.begin(), MatchesCopy.end(), |
| 7352 | TPOC_Other, 0, SourceExpr->getLocStart(), |
| 7353 | S.PDiag(), |
| 7354 | S.PDiag(diag::err_addr_ovl_ambiguous) |
| 7355 | << Matches[0].second->getDeclName(), |
| 7356 | S.PDiag(diag::note_ovl_candidate) |
| 7357 | << (unsigned) oc_function_template, |
| 7358 | Complain); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7359 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7360 | if (Result != MatchesCopy.end()) { |
| 7361 | // Make it the first and only element |
| 7362 | Matches[0].first = Matches[Result - MatchesCopy.begin()].first; |
| 7363 | Matches[0].second = cast<FunctionDecl>(*Result); |
| 7364 | Matches.resize(1); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7365 | } |
| 7366 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7367 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7368 | void EliminateAllTemplateMatches() { |
| 7369 | // [...] any function template specializations in the set are |
| 7370 | // eliminated if the set also contains a non-template function, [...] |
| 7371 | for (unsigned I = 0, N = Matches.size(); I != N; ) { |
| 7372 | if (Matches[I].second->getPrimaryTemplate() == 0) |
| 7373 | ++I; |
| 7374 | else { |
| 7375 | Matches[I] = Matches[--N]; |
| 7376 | Matches.set_size(N); |
| 7377 | } |
| 7378 | } |
| 7379 | } |
| 7380 | |
| 7381 | public: |
| 7382 | void ComplainNoMatchesFound() const { |
| 7383 | assert(Matches.empty()); |
| 7384 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_no_viable) |
| 7385 | << OvlExpr->getName() << TargetFunctionType |
| 7386 | << OvlExpr->getSourceRange(); |
| 7387 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7388 | } |
| 7389 | |
| 7390 | bool IsInvalidFormOfPointerToMemberFunction() const { |
| 7391 | return TargetTypeIsNonStaticMemberFunction && |
| 7392 | !OvlExprInfo.HasFormOfMemberPointer; |
| 7393 | } |
| 7394 | |
| 7395 | void ComplainIsInvalidFormOfPointerToMemberFunction() const { |
| 7396 | // TODO: Should we condition this on whether any functions might |
| 7397 | // have matched, or is it more appropriate to do that in callers? |
| 7398 | // TODO: a fixit wouldn't hurt. |
| 7399 | S.Diag(OvlExpr->getNameLoc(), diag::err_addr_ovl_no_qualifier) |
| 7400 | << TargetType << OvlExpr->getSourceRange(); |
| 7401 | } |
| 7402 | |
| 7403 | void ComplainOfInvalidConversion() const { |
| 7404 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_not_func_ptrref) |
| 7405 | << OvlExpr->getName() << TargetType; |
| 7406 | } |
| 7407 | |
| 7408 | void ComplainMultipleMatchesFound() const { |
| 7409 | assert(Matches.size() > 1); |
| 7410 | S.Diag(OvlExpr->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7411 | << OvlExpr->getName() |
| 7412 | << OvlExpr->getSourceRange(); |
| 7413 | S.NoteAllOverloadCandidates(OvlExpr); |
| 7414 | } |
| 7415 | |
| 7416 | int getNumMatches() const { return Matches.size(); } |
| 7417 | |
| 7418 | FunctionDecl* getMatchingFunctionDecl() const { |
| 7419 | if (Matches.size() != 1) return 0; |
| 7420 | return Matches[0].second; |
| 7421 | } |
| 7422 | |
| 7423 | const DeclAccessPair* getMatchingFunctionAccessPair() const { |
| 7424 | if (Matches.size() != 1) return 0; |
| 7425 | return &Matches[0].first; |
| 7426 | } |
| 7427 | }; |
| 7428 | |
| 7429 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 7430 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 7431 | /// expression with overloaded function type and @p ToType is the type |
| 7432 | /// we're trying to resolve to. For example: |
| 7433 | /// |
| 7434 | /// @code |
| 7435 | /// int f(double); |
| 7436 | /// int f(int); |
| 7437 | /// |
| 7438 | /// int (*pfd)(double) = f; // selects f(double) |
| 7439 | /// @endcode |
| 7440 | /// |
| 7441 | /// This routine returns the resulting FunctionDecl if it could be |
| 7442 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 7443 | /// routine will emit diagnostics if there is an error. |
| 7444 | FunctionDecl * |
| 7445 | Sema::ResolveAddressOfOverloadedFunction(Expr *AddressOfExpr, QualType TargetType, |
| 7446 | bool Complain, |
| 7447 | DeclAccessPair &FoundResult) { |
| 7448 | |
| 7449 | assert(AddressOfExpr->getType() == Context.OverloadTy); |
| 7450 | |
| 7451 | AddressOfFunctionResolver Resolver(*this, AddressOfExpr, TargetType, Complain); |
| 7452 | int NumMatches = Resolver.getNumMatches(); |
| 7453 | FunctionDecl* Fn = 0; |
| 7454 | if ( NumMatches == 0 && Complain) { |
| 7455 | if (Resolver.IsInvalidFormOfPointerToMemberFunction()) |
| 7456 | Resolver.ComplainIsInvalidFormOfPointerToMemberFunction(); |
| 7457 | else |
| 7458 | Resolver.ComplainNoMatchesFound(); |
| 7459 | } |
| 7460 | else if (NumMatches > 1 && Complain) |
| 7461 | Resolver.ComplainMultipleMatchesFound(); |
| 7462 | else if (NumMatches == 1) { |
| 7463 | Fn = Resolver.getMatchingFunctionDecl(); |
| 7464 | assert(Fn); |
| 7465 | FoundResult = *Resolver.getMatchingFunctionAccessPair(); |
| 7466 | MarkDeclarationReferenced(AddressOfExpr->getLocStart(), Fn); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 7467 | if (Complain) |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7468 | CheckAddressOfMemberAccess(AddressOfExpr, FoundResult); |
Sebastian Redl | df4b80e | 2009-10-17 21:12:09 +0000 | [diff] [blame] | 7469 | } |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7470 | |
| 7471 | return Fn; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7472 | } |
| 7473 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7474 | /// \brief Given an expression that refers to an overloaded function, try to |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7475 | /// resolve that overloaded function expression down to a single function. |
| 7476 | /// |
| 7477 | /// This routine can only resolve template-ids that refer to a single function |
| 7478 | /// template, where that template-id refers to a single template whose template |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7479 | /// arguments are either provided by the template-id or have defaults, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7480 | /// as described in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7481 | FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization(Expr *From, |
| 7482 | bool Complain, |
| 7483 | DeclAccessPair* FoundResult) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7484 | // C++ [over.over]p1: |
| 7485 | // [...] [Note: any redundant set of parentheses surrounding the |
| 7486 | // overloaded function name is ignored (5.1). ] |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7487 | // C++ [over.over]p1: |
| 7488 | // [...] The overloaded function name can be preceded by the & |
| 7489 | // operator. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7490 | if (From->getType() != Context.OverloadTy) |
| 7491 | return 0; |
| 7492 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7493 | OverloadExpr *OvlExpr = OverloadExpr::find(From).Expression; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7494 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7495 | // If we didn't actually find any template-ids, we're done. |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7496 | if (!OvlExpr->hasExplicitTemplateArgs()) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7497 | return 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7498 | |
| 7499 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 7500 | OvlExpr->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7501 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7502 | // Look through all of the overloaded functions, searching for one |
| 7503 | // whose type matches exactly. |
| 7504 | FunctionDecl *Matched = 0; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 7505 | for (UnresolvedSetIterator I = OvlExpr->decls_begin(), |
| 7506 | E = OvlExpr->decls_end(); I != E; ++I) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7507 | // C++0x [temp.arg.explicit]p3: |
| 7508 | // [...] In contexts where deduction is done and fails, or in contexts |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7509 | // where deduction is not done, if a template argument list is |
| 7510 | // specified and it, along with any default template arguments, |
| 7511 | // identifies a single function template specialization, then the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7512 | // template-id is an lvalue for the function template specialization. |
Douglas Gregor | eebe721 | 2010-07-14 23:20:53 +0000 | [diff] [blame] | 7513 | FunctionTemplateDecl *FunctionTemplate |
| 7514 | = cast<FunctionTemplateDecl>((*I)->getUnderlyingDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7515 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7516 | // C++ [over.over]p2: |
| 7517 | // If the name is a function template, template argument deduction is |
| 7518 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 7519 | // resulting template argument list is used to generate a single |
| 7520 | // function template specialization, which is added to the set of |
| 7521 | // overloaded functions considered. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7522 | FunctionDecl *Specialization = 0; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7523 | TemplateDeductionInfo Info(Context, OvlExpr->getNameLoc()); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7524 | if (TemplateDeductionResult Result |
| 7525 | = DeduceTemplateArguments(FunctionTemplate, &ExplicitTemplateArgs, |
| 7526 | Specialization, Info)) { |
| 7527 | // FIXME: make a note of the failed deduction for diagnostics. |
| 7528 | (void)Result; |
| 7529 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7530 | } |
| 7531 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7532 | // Multiple matches; we can't resolve to a single declaration. |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7533 | if (Matched) { |
| 7534 | if (FoundResult) |
| 7535 | *FoundResult = DeclAccessPair(); |
| 7536 | |
| 7537 | if (Complain) { |
| 7538 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 7539 | << OvlExpr->getName(); |
| 7540 | NoteAllOverloadCandidates(OvlExpr); |
| 7541 | } |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7542 | return 0; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
| 7545 | if ((Matched = Specialization) && FoundResult) |
| 7546 | *FoundResult = I.getPair(); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7547 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7548 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 7549 | return Matched; |
| 7550 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7551 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7552 | |
| 7553 | |
| 7554 | |
| 7555 | // Resolve and fix an overloaded expression that |
| 7556 | // can be resolved because it identifies a single function |
| 7557 | // template specialization |
| 7558 | // Last three arguments should only be supplied if Complain = true |
| 7559 | ExprResult Sema::ResolveAndFixSingleFunctionTemplateSpecialization( |
| 7560 | Expr *SrcExpr, bool DoFunctionPointerConverion, bool Complain, |
| 7561 | const SourceRange& OpRangeForComplaining, |
| 7562 | QualType DestTypeForComplaining, |
| 7563 | unsigned DiagIDForComplaining ) { |
| 7564 | |
| 7565 | assert(SrcExpr->getType() == Context.OverloadTy); |
| 7566 | |
| 7567 | DeclAccessPair Found; |
| 7568 | Expr* SingleFunctionExpression = 0; |
| 7569 | if (FunctionDecl* Fn = ResolveSingleFunctionTemplateSpecialization( |
| 7570 | SrcExpr, false, // false -> Complain |
| 7571 | &Found)) { |
| 7572 | if (!DiagnoseUseOfDecl(Fn, SrcExpr->getSourceRange().getBegin())) { |
| 7573 | // mark the expression as resolved to Fn |
| 7574 | SingleFunctionExpression = FixOverloadedFunctionReference(SrcExpr, |
| 7575 | Found, Fn); |
| 7576 | if (DoFunctionPointerConverion) |
| 7577 | DefaultFunctionArrayLvalueConversion(SingleFunctionExpression); |
| 7578 | } |
| 7579 | } |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7580 | if (!SingleFunctionExpression) { |
| 7581 | if (Complain) { |
| 7582 | OverloadExpr* oe = OverloadExpr::find(SrcExpr).Expression; |
| 7583 | Diag(OpRangeForComplaining.getBegin(), DiagIDForComplaining) |
| 7584 | << oe->getName() << DestTypeForComplaining << OpRangeForComplaining |
| 7585 | << oe->getQualifierLoc().getSourceRange(); |
| 7586 | NoteAllOverloadCandidates(SrcExpr); |
| 7587 | } |
| 7588 | return ExprError(); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7589 | } |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 7590 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7591 | return SingleFunctionExpression; |
| 7592 | } |
| 7593 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7594 | /// \brief Add a single candidate to the overload set. |
| 7595 | static void AddOverloadedCallCandidate(Sema &S, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7596 | DeclAccessPair FoundDecl, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7597 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7598 | Expr **Args, unsigned NumArgs, |
| 7599 | OverloadCandidateSet &CandidateSet, |
| 7600 | bool PartialOverloading) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7601 | NamedDecl *Callee = FoundDecl.getDecl(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7602 | if (isa<UsingShadowDecl>(Callee)) |
| 7603 | Callee = cast<UsingShadowDecl>(Callee)->getTargetDecl(); |
| 7604 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7605 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Callee)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 7606 | assert(!ExplicitTemplateArgs && "Explicit template arguments?"); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7607 | S.AddOverloadCandidate(Func, FoundDecl, Args, NumArgs, CandidateSet, |
Douglas Gregor | b05275a | 2010-04-16 17:41:49 +0000 | [diff] [blame] | 7608 | false, PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7609 | return; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7610 | } |
| 7611 | |
| 7612 | if (FunctionTemplateDecl *FuncTemplate |
| 7613 | = dyn_cast<FunctionTemplateDecl>(Callee)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7614 | S.AddTemplateOverloadCandidate(FuncTemplate, FoundDecl, |
| 7615 | ExplicitTemplateArgs, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7616 | Args, NumArgs, CandidateSet); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7617 | return; |
| 7618 | } |
| 7619 | |
| 7620 | assert(false && "unhandled case in overloaded call candidate"); |
| 7621 | |
| 7622 | // do nothing? |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7623 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7624 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7625 | /// \brief Add the overload candidates named by callee and/or found by argument |
| 7626 | /// dependent lookup to the given overload set. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7627 | void Sema::AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7628 | Expr **Args, unsigned NumArgs, |
| 7629 | OverloadCandidateSet &CandidateSet, |
| 7630 | bool PartialOverloading) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7631 | |
| 7632 | #ifndef NDEBUG |
| 7633 | // Verify that ArgumentDependentLookup is consistent with the rules |
| 7634 | // in C++0x [basic.lookup.argdep]p3: |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7635 | // |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7636 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 7637 | // and let Y be the lookup set produced by argument dependent |
| 7638 | // lookup (defined as follows). If X contains |
| 7639 | // |
| 7640 | // -- a declaration of a class member, or |
| 7641 | // |
| 7642 | // -- a block-scope function declaration that is not a |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7643 | // using-declaration, or |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7644 | // |
| 7645 | // -- a declaration that is neither a function or a function |
| 7646 | // template |
| 7647 | // |
| 7648 | // then Y is empty. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7649 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7650 | if (ULE->requiresADL()) { |
| 7651 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7652 | E = ULE->decls_end(); I != E; ++I) { |
| 7653 | assert(!(*I)->getDeclContext()->isRecord()); |
| 7654 | assert(isa<UsingShadowDecl>(*I) || |
| 7655 | !(*I)->getDeclContext()->isFunctionOrMethod()); |
| 7656 | assert((*I)->getUnderlyingDecl()->isFunctionOrFunctionTemplate()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7657 | } |
| 7658 | } |
| 7659 | #endif |
| 7660 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7661 | // It would be nice to avoid this copy. |
| 7662 | TemplateArgumentListInfo TABuffer; |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 7663 | TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7664 | if (ULE->hasExplicitTemplateArgs()) { |
| 7665 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7666 | ExplicitTemplateArgs = &TABuffer; |
| 7667 | } |
| 7668 | |
| 7669 | for (UnresolvedLookupExpr::decls_iterator I = ULE->decls_begin(), |
| 7670 | E = ULE->decls_end(); I != E; ++I) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7671 | AddOverloadedCallCandidate(*this, I.getPair(), ExplicitTemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7672 | Args, NumArgs, CandidateSet, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7673 | PartialOverloading); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7674 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7675 | if (ULE->requiresADL()) |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7676 | AddArgumentDependentLookupCandidates(ULE->getName(), /*Operator*/ false, |
| 7677 | Args, NumArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7678 | ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7679 | CandidateSet, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7680 | PartialOverloading); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 7681 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7682 | |
| 7683 | /// Attempts to recover from a call where no functions were found. |
| 7684 | /// |
| 7685 | /// Returns true if new candidates were found. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7686 | static ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7687 | BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7688 | UnresolvedLookupExpr *ULE, |
| 7689 | SourceLocation LParenLoc, |
| 7690 | Expr **Args, unsigned NumArgs, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7691 | SourceLocation RParenLoc) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7692 | |
| 7693 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7694 | SS.Adopt(ULE->getQualifierLoc()); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7695 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7696 | TemplateArgumentListInfo TABuffer; |
| 7697 | const TemplateArgumentListInfo *ExplicitTemplateArgs = 0; |
| 7698 | if (ULE->hasExplicitTemplateArgs()) { |
| 7699 | ULE->copyTemplateArgumentsInto(TABuffer); |
| 7700 | ExplicitTemplateArgs = &TABuffer; |
| 7701 | } |
| 7702 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7703 | LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), |
| 7704 | Sema::LookupOrdinaryName); |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 7705 | if (SemaRef.DiagnoseEmptyLookup(S, SS, R, Sema::CTC_Expression)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7706 | return ExprError(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7707 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7708 | assert(!R.empty() && "lookup results empty despite recovery"); |
| 7709 | |
| 7710 | // Build an implicit member call if appropriate. Just drop the |
| 7711 | // casts and such from the call, we don't really care. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7712 | ExprResult NewFn = ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7713 | if ((*R.begin())->isCXXClassMember()) |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7714 | NewFn = SemaRef.BuildPossibleImplicitMemberExpr(SS, R, |
| 7715 | ExplicitTemplateArgs); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7716 | else if (ExplicitTemplateArgs) |
| 7717 | NewFn = SemaRef.BuildTemplateIdExpr(SS, R, false, *ExplicitTemplateArgs); |
| 7718 | else |
| 7719 | NewFn = SemaRef.BuildDeclarationNameExpr(SS, R, false); |
| 7720 | |
| 7721 | if (NewFn.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7722 | return ExprError(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7723 | |
| 7724 | // This shouldn't cause an infinite loop because we're giving it |
| 7725 | // an expression with non-empty lookup results, which should never |
| 7726 | // end up here. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7727 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, NewFn.take(), LParenLoc, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7728 | MultiExprArg(Args, NumArgs), RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7729 | } |
Douglas Gregor | 4038cf4 | 2010-06-08 17:35:15 +0000 | [diff] [blame] | 7730 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7731 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 7732 | /// (which eventually refers to the declaration Func) and the call |
| 7733 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 7734 | /// to a specific function. If overload resolution succeeds, returns |
| 7735 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 7736 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7737 | /// arguments and Fn, and returns NULL. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7738 | ExprResult |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7739 | Sema::BuildOverloadedCallExpr(Scope *S, Expr *Fn, UnresolvedLookupExpr *ULE, |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7740 | SourceLocation LParenLoc, |
| 7741 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7742 | SourceLocation RParenLoc, |
| 7743 | Expr *ExecConfig) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7744 | #ifndef NDEBUG |
| 7745 | if (ULE->requiresADL()) { |
| 7746 | // To do ADL, we must have found an unqualified name. |
| 7747 | assert(!ULE->getQualifier() && "qualified name with ADL"); |
| 7748 | |
| 7749 | // We don't perform ADL for implicit declarations of builtins. |
| 7750 | // Verify that this was correctly set up. |
| 7751 | FunctionDecl *F; |
| 7752 | if (ULE->decls_begin() + 1 == ULE->decls_end() && |
| 7753 | (F = dyn_cast<FunctionDecl>(*ULE->decls_begin())) && |
| 7754 | F->getBuiltinID() && F->isImplicit()) |
| 7755 | assert(0 && "performing ADL for builtin"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7756 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7757 | // We don't perform ADL in C. |
| 7758 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 7759 | } |
| 7760 | #endif |
| 7761 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7762 | OverloadCandidateSet CandidateSet(Fn->getExprLoc()); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 7763 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7764 | // Add the functions denoted by the callee to the set of candidate |
| 7765 | // functions, including those from argument-dependent lookup. |
| 7766 | AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7767 | |
| 7768 | // If we found nothing, try to recover. |
| 7769 | // AddRecoveryCallCandidates diagnoses the error itself, so we just |
| 7770 | // bailout out if it fails. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7771 | if (CandidateSet.empty()) |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 7772 | return BuildRecoveryCallExpr(*this, S, Fn, ULE, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7773 | RParenLoc); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 7774 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7775 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7776 | switch (CandidateSet.BestViableFunction(*this, Fn->getLocStart(), Best)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7777 | case OR_Success: { |
| 7778 | FunctionDecl *FDecl = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7779 | MarkDeclarationReferenced(Fn->getExprLoc(), FDecl); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7780 | CheckUnresolvedLookupAccess(ULE, Best->FoundDecl); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 7781 | DiagnoseUseOfDecl(FDecl? FDecl : Best->FoundDecl.getDecl(), |
| 7782 | ULE->getNameLoc()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7783 | Fn = FixOverloadedFunctionReference(Fn, Best->FoundDecl, FDecl); |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7784 | return BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 7785 | ExecConfig); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7786 | } |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7787 | |
| 7788 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 7789 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7790 | diag::err_ovl_no_viable_function_in_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7791 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7792 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7793 | break; |
| 7794 | |
| 7795 | case OR_Ambiguous: |
| 7796 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7797 | << ULE->getName() << Fn->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7798 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7799 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7800 | |
| 7801 | case OR_Deleted: |
Fariborz Jahanian | bff158d | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 7802 | { |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 7803 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 7804 | << Best->Function->isDeleted() |
| 7805 | << ULE->getName() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 7806 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 7807 | << Fn->getSourceRange(); |
Fariborz Jahanian | bff158d | 2011-02-25 18:38:59 +0000 | [diff] [blame] | 7808 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
| 7809 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 7810 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7811 | } |
| 7812 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7813 | // Overload resolution failed. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 7814 | return ExprError(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 7815 | } |
| 7816 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7817 | static bool IsOverloaded(const UnresolvedSetImpl &Functions) { |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 7818 | return Functions.size() > 1 || |
| 7819 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 7820 | } |
| 7821 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7822 | /// \brief Create a unary operation that may resolve to an overloaded |
| 7823 | /// operator. |
| 7824 | /// |
| 7825 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 7826 | /// |
| 7827 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 7828 | /// operator. |
| 7829 | /// |
| 7830 | /// \param Functions The set of non-member functions that will be |
| 7831 | /// considered by overload resolution. The caller needs to build this |
| 7832 | /// set based on the context using, e.g., |
| 7833 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 7834 | /// set should not contain any member functions; those will be added |
| 7835 | /// by CreateOverloadedUnaryOp(). |
| 7836 | /// |
| 7837 | /// \param input The input argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7838 | ExprResult |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7839 | Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, unsigned OpcIn, |
| 7840 | const UnresolvedSetImpl &Fns, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7841 | Expr *Input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7842 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7843 | |
| 7844 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 7845 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 7846 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7847 | // TODO: provide better source location info. |
| 7848 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7849 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 7850 | if (Input->getObjectKind() == OK_ObjCProperty) |
| 7851 | ConvertPropertyForRValue(Input); |
| 7852 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7853 | Expr *Args[2] = { Input, 0 }; |
| 7854 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7855 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7856 | // For post-increment and post-decrement, add the implicit '0' as |
| 7857 | // the second argument, so that we know this is a post-increment or |
| 7858 | // post-decrement. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7859 | if (Opc == UO_PostInc || Opc == UO_PostDec) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7860 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7861 | Args[1] = IntegerLiteral::Create(Context, Zero, Context.IntTy, |
| 7862 | SourceLocation()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7863 | NumArgs = 2; |
| 7864 | } |
| 7865 | |
| 7866 | if (Input->isTypeDependent()) { |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7867 | if (Fns.empty()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7868 | return Owned(new (Context) UnaryOperator(Input, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7869 | Opc, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7870 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7871 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 630dec5 | 2010-06-17 15:46:20 +0000 | [diff] [blame] | 7872 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7873 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 7874 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7875 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 7876 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7877 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 7878 | /*ADL*/ true, IsOverloaded(Fns), |
| 7879 | Fns.begin(), Fns.end()); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7880 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 7881 | &Args[0], NumArgs, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7882 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7883 | VK_RValue, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7884 | OpLoc)); |
| 7885 | } |
| 7886 | |
| 7887 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 7888 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7889 | |
| 7890 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7891 | AddFunctionCandidates(Fns, &Args[0], NumArgs, CandidateSet, false); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7892 | |
| 7893 | // Add operator candidates that are member functions. |
| 7894 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 7895 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7896 | // Add candidates from ADL. |
| 7897 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
Douglas Gregor | 6ec89d4 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 7898 | Args, NumArgs, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7899 | /*ExplicitTemplateArgs*/ 0, |
| 7900 | CandidateSet); |
| 7901 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7902 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 7903 | AddBuiltinOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7904 | |
| 7905 | // Perform overload resolution. |
| 7906 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7907 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7908 | case OR_Success: { |
| 7909 | // We found a built-in operator or an overloaded operator. |
| 7910 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7911 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7912 | if (FnDecl) { |
| 7913 | // We matched an overloaded operator. Build a call to that |
| 7914 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7915 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 7916 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 7917 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7918 | // Convert the arguments. |
| 7919 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 7920 | CheckMemberOperatorAccess(OpLoc, Args[0], 0, Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 7921 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 7922 | if (PerformObjectArgumentInitialization(Input, /*Qualifier=*/0, |
| 7923 | Best->FoundDecl, Method)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7924 | return ExprError(); |
| 7925 | } else { |
| 7926 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7927 | ExprResult InputInit |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 7928 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7929 | Context, |
Douglas Gregor | 8d48e9a | 2009-12-23 00:02:00 +0000 | [diff] [blame] | 7930 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7931 | SourceLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7932 | Input); |
Douglas Gregor | e660037 | 2009-12-23 17:40:29 +0000 | [diff] [blame] | 7933 | if (InputInit.isInvalid()) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7934 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7935 | Input = InputInit.take(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7938 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 7939 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7940 | // Determine the result type. |
| 7941 | QualType ResultTy = FnDecl->getResultType(); |
| 7942 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 7943 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7944 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7945 | // Build the actual expression node. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7946 | Expr *FnExpr = CreateFunctionRefExpr(*this, FnDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7947 | |
Eli Friedman | 030eee4 | 2009-11-18 03:58:17 +0000 | [diff] [blame] | 7948 | Args[0] = Input; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7949 | CallExpr *TheCall = |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 7950 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7951 | Args, NumArgs, ResultTy, VK, OpLoc); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 7952 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7953 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | f64a3da | 2009-10-13 21:19:37 +0000 | [diff] [blame] | 7954 | FnDecl)) |
| 7955 | return ExprError(); |
| 7956 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7957 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7958 | } else { |
| 7959 | // We matched a built-in operator. Convert the arguments, then |
| 7960 | // break out so that we will build the appropriate built-in |
| 7961 | // operator node. |
| 7962 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7963 | Best->Conversions[0], AA_Passing)) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7964 | return ExprError(); |
| 7965 | |
| 7966 | break; |
| 7967 | } |
| 7968 | } |
| 7969 | |
| 7970 | case OR_No_Viable_Function: |
| 7971 | // No viable function; fall through to handling this as a |
| 7972 | // built-in operator, which will produce an error message for us. |
| 7973 | break; |
| 7974 | |
| 7975 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 7976 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7977 | << UnaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 7978 | << Input->getType() |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7979 | << Input->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7980 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, |
| 7981 | Args, NumArgs, |
| 7982 | UnaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7983 | return ExprError(); |
| 7984 | |
| 7985 | case OR_Deleted: |
| 7986 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 7987 | << Best->Function->isDeleted() |
| 7988 | << UnaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 7989 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7990 | << Input->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 7991 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7992 | return ExprError(); |
| 7993 | } |
| 7994 | |
| 7995 | // Either we found no viable overloaded operator or we matched a |
| 7996 | // built-in operator. In either case, fall through to trying to |
| 7997 | // build a built-in operation. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7998 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8001 | /// \brief Create a binary operation that may resolve to an overloaded |
| 8002 | /// operator. |
| 8003 | /// |
| 8004 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 8005 | /// |
| 8006 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 8007 | /// operator. |
| 8008 | /// |
| 8009 | /// \param Functions The set of non-member functions that will be |
| 8010 | /// considered by overload resolution. The caller needs to build this |
| 8011 | /// set based on the context using, e.g., |
| 8012 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 8013 | /// set should not contain any member functions; those will be added |
| 8014 | /// by CreateOverloadedBinOp(). |
| 8015 | /// |
| 8016 | /// \param LHS Left-hand argument. |
| 8017 | /// \param RHS Right-hand argument. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8018 | ExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8019 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8020 | unsigned OpcIn, |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8021 | const UnresolvedSetImpl &Fns, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8022 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8023 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8024 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8025 | |
| 8026 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 8027 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 8028 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 8029 | |
| 8030 | // If either side is type-dependent, create an appropriate dependent |
| 8031 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8032 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8033 | if (Fns.empty()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8034 | // If there are no functions to store, just build a dependent |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8035 | // BinaryOperator or CompoundAssignment. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8036 | if (Opc <= BO_Assign || Opc > BO_OrAssign) |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8037 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8038 | Context.DependentTy, |
| 8039 | VK_RValue, OK_Ordinary, |
| 8040 | OpLoc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8041 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8042 | return Owned(new (Context) CompoundAssignOperator(Args[0], Args[1], Opc, |
| 8043 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8044 | VK_LValue, |
| 8045 | OK_Ordinary, |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8046 | Context.DependentTy, |
| 8047 | Context.DependentTy, |
| 8048 | OpLoc)); |
| 8049 | } |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8050 | |
| 8051 | // FIXME: save results of ADL from here? |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8052 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8053 | // TODO: provide better source location info in DNLoc component. |
| 8054 | DeclarationNameInfo OpNameInfo(OpName, OpLoc); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8055 | UnresolvedLookupExpr *Fn |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8056 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
| 8057 | NestedNameSpecifierLoc(), OpNameInfo, |
| 8058 | /*ADL*/ true, IsOverloaded(Fns), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8059 | Fns.begin(), Fns.end()); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8060 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8061 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8062 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8063 | VK_RValue, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8064 | OpLoc)); |
| 8065 | } |
| 8066 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8067 | // Always do property rvalue conversions on the RHS. |
| 8068 | if (Args[1]->getObjectKind() == OK_ObjCProperty) |
| 8069 | ConvertPropertyForRValue(Args[1]); |
| 8070 | |
| 8071 | // The LHS is more complicated. |
| 8072 | if (Args[0]->getObjectKind() == OK_ObjCProperty) { |
| 8073 | |
| 8074 | // There's a tension for assignment operators between primitive |
| 8075 | // property assignment and the overloaded operators. |
| 8076 | if (BinaryOperator::isAssignmentOp(Opc)) { |
| 8077 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 8078 | |
| 8079 | // Is the property "logically" settable? |
| 8080 | bool Settable = (PRE->isExplicitProperty() || |
| 8081 | PRE->getImplicitPropertySetter()); |
| 8082 | |
| 8083 | // To avoid gratuitously inventing semantics, use the primitive |
| 8084 | // unless it isn't. Thoughts in case we ever really care: |
| 8085 | // - If the property isn't logically settable, we have to |
| 8086 | // load and hope. |
| 8087 | // - If the property is settable and this is simple assignment, |
| 8088 | // we really should use the primitive. |
| 8089 | // - If the property is settable, then we could try overloading |
| 8090 | // on a generic lvalue of the appropriate type; if it works |
| 8091 | // out to a builtin candidate, we would do that same operation |
| 8092 | // on the property, and otherwise just error. |
| 8093 | if (Settable) |
| 8094 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8095 | } |
| 8096 | |
| 8097 | ConvertPropertyForRValue(Args[0]); |
| 8098 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8099 | |
Sebastian Redl | 6a96bf7 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 8100 | // If this is the assignment operator, we only perform overload resolution |
| 8101 | // if the left-hand side is a class or enumeration type. This is actually |
| 8102 | // a hack. The standard requires that we do overload resolution between the |
| 8103 | // various built-in candidates, but as DR507 points out, this can lead to |
| 8104 | // problems. So we do it this way, which pretty much follows what GCC does. |
| 8105 | // Note that we go the traditional code path for compound assignment forms. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8106 | if (Opc == BO_Assign && !Args[0]->getType()->isOverloadableType()) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8107 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8108 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8109 | // If this is the .* operator, which is not overloadable, just |
| 8110 | // create a built-in binary operator. |
| 8111 | if (Opc == BO_PtrMemD) |
| 8112 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
| 8113 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8114 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8115 | OverloadCandidateSet CandidateSet(OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8116 | |
| 8117 | // Add the candidates from the given function set. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8118 | AddFunctionCandidates(Fns, Args, 2, CandidateSet, false); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8119 | |
| 8120 | // Add operator candidates that are member functions. |
| 8121 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 8122 | |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8123 | // Add candidates from ADL. |
| 8124 | AddArgumentDependentLookupCandidates(OpName, /*Operator*/ true, |
| 8125 | Args, 2, |
| 8126 | /*ExplicitTemplateArgs*/ 0, |
| 8127 | CandidateSet); |
| 8128 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8129 | // Add builtin operator candidates. |
Douglas Gregor | c02cfe2 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 8130 | AddBuiltinOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8131 | |
| 8132 | // Perform overload resolution. |
| 8133 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8134 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 8135 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8136 | // We found a built-in operator or an overloaded operator. |
| 8137 | FunctionDecl *FnDecl = Best->Function; |
| 8138 | |
| 8139 | if (FnDecl) { |
| 8140 | // We matched an overloaded operator. Build a call to that |
| 8141 | // operator. |
| 8142 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8143 | MarkDeclarationReferenced(OpLoc, FnDecl); |
| 8144 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8145 | // Convert the arguments. |
| 8146 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8147 | // Best->Access is only meaningful for class members. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8148 | CheckMemberOperatorAccess(OpLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | b3a4400 | 2010-01-28 01:42:12 +0000 | [diff] [blame] | 8149 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8150 | ExprResult Arg1 = |
| 8151 | PerformCopyInitialization( |
| 8152 | InitializedEntity::InitializeParameter(Context, |
| 8153 | FnDecl->getParamDecl(0)), |
| 8154 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8155 | if (Arg1.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8156 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8157 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8158 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8159 | Best->FoundDecl, Method)) |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8160 | return ExprError(); |
| 8161 | |
| 8162 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8163 | } else { |
| 8164 | // Convert the arguments. |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8165 | ExprResult Arg0 = PerformCopyInitialization( |
| 8166 | InitializedEntity::InitializeParameter(Context, |
| 8167 | FnDecl->getParamDecl(0)), |
| 8168 | SourceLocation(), Owned(Args[0])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8169 | if (Arg0.isInvalid()) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8170 | return ExprError(); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8171 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8172 | ExprResult Arg1 = |
| 8173 | PerformCopyInitialization( |
| 8174 | InitializedEntity::InitializeParameter(Context, |
| 8175 | FnDecl->getParamDecl(1)), |
| 8176 | SourceLocation(), Owned(Args[1])); |
Douglas Gregor | 0a70c4d | 2009-12-22 21:44:34 +0000 | [diff] [blame] | 8177 | if (Arg1.isInvalid()) |
| 8178 | return ExprError(); |
| 8179 | Args[0] = LHS = Arg0.takeAs<Expr>(); |
| 8180 | Args[1] = RHS = Arg1.takeAs<Expr>(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8181 | } |
| 8182 | |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8183 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
| 8184 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8185 | // Determine the result type. |
| 8186 | QualType ResultTy = FnDecl->getResultType(); |
| 8187 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8188 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8189 | |
| 8190 | // Build the actual expression node. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8191 | Expr *FnExpr = CreateFunctionRefExpr(*this, FnDecl, OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8192 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8193 | CXXOperatorCallExpr *TheCall = |
| 8194 | new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8195 | Args, 2, ResultTy, VK, OpLoc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8196 | |
| 8197 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8198 | FnDecl)) |
| 8199 | return ExprError(); |
| 8200 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8201 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8202 | } else { |
| 8203 | // We matched a built-in operator. Convert the arguments, then |
| 8204 | // break out so that we will build the appropriate built-in |
| 8205 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8206 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 8207 | Best->Conversions[0], AA_Passing) || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8208 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 8209 | Best->Conversions[1], AA_Passing)) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8210 | return ExprError(); |
| 8211 | |
| 8212 | break; |
| 8213 | } |
| 8214 | } |
| 8215 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8216 | case OR_No_Viable_Function: { |
| 8217 | // C++ [over.match.oper]p9: |
| 8218 | // If the operator is the operator , [...] and there are no |
| 8219 | // viable functions, then the operator is assumed to be the |
| 8220 | // built-in operator and interpreted according to clause 5. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8221 | if (Opc == BO_Comma) |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8222 | break; |
| 8223 | |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8224 | // For class as left operand for assignment or compound assigment |
| 8225 | // operator do not fall through to handling in built-in, but report that |
| 8226 | // no overloaded assignment operator found |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8227 | ExprResult Result = ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8228 | if (Args[0]->getType()->isRecordType() && |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8229 | Opc >= BO_Assign && Opc <= BO_OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8230 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 8231 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8232 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8233 | } else { |
| 8234 | // No viable function; try to create a built-in operation, which will |
| 8235 | // produce an error. Then, show the non-viable candidates. |
| 8236 | Result = CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 8237 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8238 | assert(Result.isInvalid() && |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8239 | "C++ binary operator overloading is missing candidates!"); |
| 8240 | if (Result.isInvalid()) |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8241 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8242 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8243 | return move(Result); |
| 8244 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8245 | |
| 8246 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8247 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_binary) |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8248 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8249 | << Args[0]->getType() << Args[1]->getType() |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8250 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8251 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8252 | BinaryOperator::getOpcodeStr(Opc), OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8253 | return ExprError(); |
| 8254 | |
| 8255 | case OR_Deleted: |
| 8256 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8257 | << Best->Function->isDeleted() |
| 8258 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8259 | << getDeletedOrUnavailableSuffix(Best->Function) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8260 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8261 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8262 | return ExprError(); |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 8263 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8264 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 8265 | // We matched a built-in operator; build it. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 8266 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 8267 | } |
| 8268 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8269 | ExprResult |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8270 | Sema::CreateOverloadedArraySubscriptExpr(SourceLocation LLoc, |
| 8271 | SourceLocation RLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8272 | Expr *Base, Expr *Idx) { |
| 8273 | Expr *Args[2] = { Base, Idx }; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8274 | DeclarationName OpName = |
| 8275 | Context.DeclarationNames.getCXXOperatorName(OO_Subscript); |
| 8276 | |
| 8277 | // If either side is type-dependent, create an appropriate dependent |
| 8278 | // expression. |
| 8279 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
| 8280 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8281 | CXXRecordDecl *NamingClass = 0; // because lookup ignores member operators |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8282 | // CHECKME: no 'operator' keyword? |
| 8283 | DeclarationNameInfo OpNameInfo(OpName, LLoc); |
| 8284 | OpNameInfo.setCXXOperatorNameRange(SourceRange(LLoc, RLoc)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8285 | UnresolvedLookupExpr *Fn |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 8286 | = UnresolvedLookupExpr::Create(Context, NamingClass, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 8287 | NestedNameSpecifierLoc(), OpNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 8288 | /*ADL*/ true, /*Overloaded*/ false, |
| 8289 | UnresolvedSetIterator(), |
| 8290 | UnresolvedSetIterator()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 8291 | // Can't add any actual overloads yet |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8292 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8293 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, Fn, |
| 8294 | Args, 2, |
| 8295 | Context.DependentTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8296 | VK_RValue, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8297 | RLoc)); |
| 8298 | } |
| 8299 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8300 | if (Args[0]->getObjectKind() == OK_ObjCProperty) |
| 8301 | ConvertPropertyForRValue(Args[0]); |
| 8302 | if (Args[1]->getObjectKind() == OK_ObjCProperty) |
| 8303 | ConvertPropertyForRValue(Args[1]); |
| 8304 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8305 | // Build an empty overload set. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8306 | OverloadCandidateSet CandidateSet(LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8307 | |
| 8308 | // Subscript can only be overloaded as a member function. |
| 8309 | |
| 8310 | // Add operator candidates that are member functions. |
| 8311 | AddMemberOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8312 | |
| 8313 | // Add builtin operator candidates. |
| 8314 | AddBuiltinOperatorCandidates(OO_Subscript, LLoc, Args, 2, CandidateSet); |
| 8315 | |
| 8316 | // Perform overload resolution. |
| 8317 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8318 | switch (CandidateSet.BestViableFunction(*this, LLoc, Best)) { |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8319 | case OR_Success: { |
| 8320 | // We found a built-in operator or an overloaded operator. |
| 8321 | FunctionDecl *FnDecl = Best->Function; |
| 8322 | |
| 8323 | if (FnDecl) { |
| 8324 | // We matched an overloaded operator. Build a call to that |
| 8325 | // operator. |
| 8326 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8327 | MarkDeclarationReferenced(LLoc, FnDecl); |
| 8328 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8329 | CheckMemberOperatorAccess(LLoc, Args[0], Args[1], Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8330 | DiagnoseUseOfDecl(Best->FoundDecl, LLoc); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 8331 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8332 | // Convert the arguments. |
| 8333 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8334 | if (PerformObjectArgumentInitialization(Args[0], /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8335 | Best->FoundDecl, Method)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8336 | return ExprError(); |
| 8337 | |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8338 | // Convert the arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8339 | ExprResult InputInit |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8340 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8341 | Context, |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8342 | FnDecl->getParamDecl(0)), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8343 | SourceLocation(), |
Anders Carlsson | a68e51e | 2010-01-29 18:37:50 +0000 | [diff] [blame] | 8344 | Owned(Args[1])); |
| 8345 | if (InputInit.isInvalid()) |
| 8346 | return ExprError(); |
| 8347 | |
| 8348 | Args[1] = InputInit.takeAs<Expr>(); |
| 8349 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8350 | // Determine the result type |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8351 | QualType ResultTy = FnDecl->getResultType(); |
| 8352 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8353 | ResultTy = ResultTy.getNonLValueExprType(Context); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8354 | |
| 8355 | // Build the actual expression node. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8356 | Expr *FnExpr = CreateFunctionRefExpr(*this, FnDecl, LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8357 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8358 | CXXOperatorCallExpr *TheCall = |
| 8359 | new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 8360 | FnExpr, Args, 2, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8361 | ResultTy, VK, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8362 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8363 | if (CheckCallReturnType(FnDecl->getResultType(), LLoc, TheCall, |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8364 | FnDecl)) |
| 8365 | return ExprError(); |
| 8366 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8367 | return MaybeBindToTemporary(TheCall); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8368 | } else { |
| 8369 | // We matched a built-in operator. Convert the arguments, then |
| 8370 | // break out so that we will build the appropriate built-in |
| 8371 | // operator node. |
| 8372 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 8373 | Best->Conversions[0], AA_Passing) || |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8374 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 8375 | Best->Conversions[1], AA_Passing)) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8376 | return ExprError(); |
| 8377 | |
| 8378 | break; |
| 8379 | } |
| 8380 | } |
| 8381 | |
| 8382 | case OR_No_Viable_Function: { |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8383 | if (CandidateSet.empty()) |
| 8384 | Diag(LLoc, diag::err_ovl_no_oper) |
| 8385 | << Args[0]->getType() << /*subscript*/ 0 |
| 8386 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
| 8387 | else |
| 8388 | Diag(LLoc, diag::err_ovl_no_viable_subscript) |
| 8389 | << Args[0]->getType() |
| 8390 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8391 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8392 | "[]", LLoc); |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8393 | return ExprError(); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8394 | } |
| 8395 | |
| 8396 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8397 | Diag(LLoc, diag::err_ovl_ambiguous_oper_binary) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8398 | << "[]" |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8399 | << Args[0]->getType() << Args[1]->getType() |
| 8400 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8401 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, 2, |
| 8402 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8403 | return ExprError(); |
| 8404 | |
| 8405 | case OR_Deleted: |
| 8406 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 8407 | << Best->Function->isDeleted() << "[]" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8408 | << getDeletedOrUnavailableSuffix(Best->Function) |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8409 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8410 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, 2, |
| 8411 | "[]", LLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8412 | return ExprError(); |
| 8413 | } |
| 8414 | |
| 8415 | // We matched a built-in operator; build it. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8416 | return CreateBuiltinArraySubscriptExpr(Args[0], LLoc, Args[1], RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8419 | /// BuildCallToMemberFunction - Build a call to a member |
| 8420 | /// function. MemExpr is the expression that refers to the member |
| 8421 | /// function (and includes the object parameter), Args/NumArgs are the |
| 8422 | /// arguments to the function call (not including the object |
| 8423 | /// parameter). The caller needs to validate that the member |
| 8424 | /// expression refers to a member function or an overloaded member |
| 8425 | /// function. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8426 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8427 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 8428 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8429 | unsigned NumArgs, SourceLocation RParenLoc) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8430 | // Dig out the member expression. This holds both the object |
| 8431 | // argument and the member function we're referring to. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8432 | Expr *NakedMemExpr = MemExprE->IgnoreParens(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8433 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8434 | MemberExpr *MemExpr; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8435 | CXXMethodDecl *Method = 0; |
John McCall | 3a65ef4 | 2010-04-08 00:13:37 +0000 | [diff] [blame] | 8436 | DeclAccessPair FoundDecl = DeclAccessPair::make(0, AS_public); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8437 | NestedNameSpecifier *Qualifier = 0; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8438 | if (isa<MemberExpr>(NakedMemExpr)) { |
| 8439 | MemExpr = cast<MemberExpr>(NakedMemExpr); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8440 | Method = cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8441 | FoundDecl = MemExpr->getFoundDecl(); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8442 | Qualifier = MemExpr->getQualifier(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8443 | } else { |
| 8444 | UnresolvedMemberExpr *UnresExpr = cast<UnresolvedMemberExpr>(NakedMemExpr); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 8445 | Qualifier = UnresExpr->getQualifier(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8446 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8447 | QualType ObjectType = UnresExpr->getBaseType(); |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8448 | Expr::Classification ObjectClassification |
| 8449 | = UnresExpr->isArrow()? Expr::Classification::makeSimpleLValue() |
| 8450 | : UnresExpr->getBase()->Classify(Context); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8451 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8452 | // Add overload candidates |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8453 | OverloadCandidateSet CandidateSet(UnresExpr->getMemberLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8454 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8455 | // FIXME: avoid copy. |
| 8456 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 8457 | if (UnresExpr->hasExplicitTemplateArgs()) { |
| 8458 | UnresExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 8459 | TemplateArgs = &TemplateArgsBuffer; |
| 8460 | } |
| 8461 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8462 | for (UnresolvedMemberExpr::decls_iterator I = UnresExpr->decls_begin(), |
| 8463 | E = UnresExpr->decls_end(); I != E; ++I) { |
| 8464 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8465 | NamedDecl *Func = *I; |
| 8466 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(Func->getDeclContext()); |
| 8467 | if (isa<UsingShadowDecl>(Func)) |
| 8468 | Func = cast<UsingShadowDecl>(Func)->getTargetDecl(); |
| 8469 | |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8470 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 8471 | // Microsoft supports direct constructor calls. |
| 8472 | if (getLangOptions().Microsoft && isa<CXXConstructorDecl>(Func)) { |
| 8473 | AddOverloadCandidate(cast<CXXConstructorDecl>(Func), I.getPair(), Args, NumArgs, |
| 8474 | CandidateSet); |
| 8475 | } else if ((Method = dyn_cast<CXXMethodDecl>(Func))) { |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8476 | // If explicit template arguments were provided, we can't call a |
| 8477 | // non-template member function. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8478 | if (TemplateArgs) |
Douglas Gregor | d331984 | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 8479 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8480 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8481 | AddMethodCandidate(Method, I.getPair(), ActingDC, ObjectType, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8482 | ObjectClassification, |
| 8483 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8484 | /*SuppressUserConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8485 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8486 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(Func), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8487 | I.getPair(), ActingDC, TemplateArgs, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8488 | ObjectType, ObjectClassification, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8489 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8490 | /*SuppressUsedConversions=*/false); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 8491 | } |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 8492 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8493 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8494 | DeclarationName DeclName = UnresExpr->getMemberName(); |
| 8495 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8496 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8497 | switch (CandidateSet.BestViableFunction(*this, UnresExpr->getLocStart(), |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 8498 | Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8499 | case OR_Success: |
| 8500 | Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8501 | MarkDeclarationReferenced(UnresExpr->getMemberLoc(), Method); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8502 | FoundDecl = Best->FoundDecl; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8503 | CheckUnresolvedMemberAccess(UnresExpr, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8504 | DiagnoseUseOfDecl(Best->FoundDecl, UnresExpr->getNameLoc()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8505 | break; |
| 8506 | |
| 8507 | case OR_No_Viable_Function: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8508 | Diag(UnresExpr->getMemberLoc(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8509 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8510 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8511 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8512 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8513 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8514 | |
| 8515 | case OR_Ambiguous: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8516 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 8517 | << DeclName << MemExprE->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8518 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8519 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8520 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8521 | |
| 8522 | case OR_Deleted: |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8523 | Diag(UnresExpr->getMemberLoc(), diag::err_ovl_deleted_member_call) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8524 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8525 | << DeclName |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8526 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8527 | << MemExprE->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8528 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8529 | // FIXME: Leaking incoming expressions! |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8530 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8531 | } |
| 8532 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8533 | MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8534 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8535 | // If overload resolution picked a static member, build a |
| 8536 | // non-member call based on that function. |
| 8537 | if (Method->isStatic()) { |
| 8538 | return BuildResolvedCallExpr(MemExprE, Method, LParenLoc, |
| 8539 | Args, NumArgs, RParenLoc); |
| 8540 | } |
| 8541 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 8542 | MemExpr = cast<MemberExpr>(MemExprE->IgnoreParens()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8545 | QualType ResultType = Method->getResultType(); |
| 8546 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 8547 | ResultType = ResultType.getNonLValueExprType(Context); |
| 8548 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8549 | assert(Method && "Member call to something that isn't a method?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8550 | CXXMemberCallExpr *TheCall = |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8551 | new (Context) CXXMemberCallExpr(Context, MemExprE, Args, NumArgs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8552 | ResultType, VK, RParenLoc); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8553 | |
Anders Carlsson | c4859ba | 2009-10-10 00:06:20 +0000 | [diff] [blame] | 8554 | // Check for a valid return type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8555 | if (CheckCallReturnType(Method->getResultType(), MemExpr->getMemberLoc(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8556 | TheCall, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8557 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8558 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8559 | // Convert the object argument (for a non-static member function call). |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8560 | // We only need to do this if there was actually an overload; otherwise |
| 8561 | // it was done at lookup. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8562 | Expr *ObjectArg = MemExpr->getBase(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8563 | if (!Method->isStatic() && |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8564 | PerformObjectArgumentInitialization(ObjectArg, Qualifier, |
| 8565 | FoundDecl, Method)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8566 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8567 | MemExpr->setBase(ObjectArg); |
| 8568 | |
| 8569 | // Convert the rest of the arguments |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8570 | const FunctionProtoType *Proto = |
| 8571 | Method->getType()->getAs<FunctionProtoType>(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8572 | if (ConvertArgumentsForCall(TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8573 | RParenLoc)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8574 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8575 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8576 | if (CheckFunctionCall(Method, TheCall)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 8577 | return ExprError(); |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 8578 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8579 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 8580 | } |
| 8581 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8582 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 8583 | /// type (C++ [over.call.object]), which can end up invoking an |
| 8584 | /// overloaded function call operator (@c operator()) or performing a |
| 8585 | /// user-defined conversion on the object argument. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8586 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8587 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 8588 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8589 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8590 | SourceLocation RParenLoc) { |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8591 | if (Object->getObjectKind() == OK_ObjCProperty) |
| 8592 | ConvertPropertyForRValue(Object); |
| 8593 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8594 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8595 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8596 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8597 | // C++ [over.call.object]p1: |
| 8598 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 8599 | // 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] | 8600 | // candidate functions includes at least the function call |
| 8601 | // operators of T. The function call operators of T are obtained by |
| 8602 | // ordinary lookup of the name operator() in the context of |
| 8603 | // (E).operator(). |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8604 | OverloadCandidateSet CandidateSet(LParenLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 8605 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8606 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8607 | if (RequireCompleteType(LParenLoc, Object->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8608 | PDiag(diag::err_incomplete_object_call) |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8609 | << Object->getSourceRange())) |
| 8610 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8611 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8612 | LookupResult R(*this, OpName, LParenLoc, LookupOrdinaryName); |
| 8613 | LookupQualifiedName(R, Record->getDecl()); |
| 8614 | R.suppressDiagnostics(); |
| 8615 | |
Douglas Gregor | c473cbb | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 8616 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 8617 | Oper != OperEnd; ++Oper) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8618 | AddMethodCandidate(Oper.getPair(), Object->getType(), |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8619 | Object->Classify(Context), Args, NumArgs, CandidateSet, |
John McCall | f0f1cf0 | 2009-11-17 07:50:12 +0000 | [diff] [blame] | 8620 | /*SuppressUserConversions=*/ false); |
Douglas Gregor | 358e774 | 2009-11-07 17:23:56 +0000 | [diff] [blame] | 8621 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8622 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8623 | // C++ [over.call.object]p2: |
| 8624 | // In addition, for each conversion function declared in T of the |
| 8625 | // form |
| 8626 | // |
| 8627 | // operator conversion-type-id () cv-qualifier; |
| 8628 | // |
| 8629 | // where cv-qualifier is the same cv-qualification as, or a |
| 8630 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 8631 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 8632 | // R", or the type "reference to pointer to function of |
| 8633 | // (P1,...,Pn) returning R", or the type "reference to function |
| 8634 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8635 | // is also considered as a candidate function. Similarly, |
| 8636 | // surrogate call functions are added to the set of candidate |
| 8637 | // functions for each conversion function declared in an |
| 8638 | // accessible base class provided the function is not hidden |
| 8639 | // within T by another intervening declaration. |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 8640 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 2159182 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 8641 | = cast<CXXRecordDecl>(Record->getDecl())->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 8642 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8643 | E = Conversions->end(); I != E; ++I) { |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8644 | NamedDecl *D = *I; |
| 8645 | CXXRecordDecl *ActingContext = cast<CXXRecordDecl>(D->getDeclContext()); |
| 8646 | if (isa<UsingShadowDecl>(D)) |
| 8647 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8648 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8649 | // Skip over templated conversion functions; they aren't |
| 8650 | // surrogates. |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8651 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8652 | continue; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 8653 | |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8654 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8655 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8656 | // Strip the reference type (if any) and then the pointer type (if |
| 8657 | // any) to get down to what might be a function type. |
| 8658 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 8659 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 8660 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8661 | |
Douglas Gregor | 74ba25c | 2009-10-21 06:18:39 +0000 | [diff] [blame] | 8662 | if (const FunctionProtoType *Proto = ConvType->getAs<FunctionProtoType>()) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8663 | AddSurrogateCandidate(Conv, I.getPair(), ActingContext, Proto, |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8664 | Object, Args, NumArgs, CandidateSet); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8665 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8666 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8667 | // Perform overload resolution. |
| 8668 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8669 | switch (CandidateSet.BestViableFunction(*this, Object->getLocStart(), |
| 8670 | Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8671 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8672 | // Overload resolution succeeded; we'll build the appropriate call |
| 8673 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8674 | break; |
| 8675 | |
| 8676 | case OR_No_Viable_Function: |
John McCall | 0237485 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 8677 | if (CandidateSet.empty()) |
| 8678 | Diag(Object->getSourceRange().getBegin(), diag::err_ovl_no_oper) |
| 8679 | << Object->getType() << /*call*/ 1 |
| 8680 | << Object->getSourceRange(); |
| 8681 | else |
| 8682 | Diag(Object->getSourceRange().getBegin(), |
| 8683 | diag::err_ovl_no_viable_object_call) |
| 8684 | << Object->getType() << Object->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8685 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8686 | break; |
| 8687 | |
| 8688 | case OR_Ambiguous: |
| 8689 | Diag(Object->getSourceRange().getBegin(), |
| 8690 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8691 | << Object->getType() << Object->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8692 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8693 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8694 | |
| 8695 | case OR_Deleted: |
| 8696 | Diag(Object->getSourceRange().getBegin(), |
| 8697 | diag::err_ovl_deleted_object_call) |
| 8698 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8699 | << Object->getType() |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8700 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8701 | << Object->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8702 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8703 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8704 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8705 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 8706 | if (Best == CandidateSet.end()) |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8707 | return true; |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8708 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8709 | if (Best->Function == 0) { |
| 8710 | // Since there is no function declaration, this is one of the |
| 8711 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8712 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8713 | = cast<CXXConversionDecl>( |
| 8714 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 8715 | |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8716 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8717 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 8718 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8719 | // We selected one of the surrogate functions that converts the |
| 8720 | // object parameter to a function pointer. Perform the conversion |
| 8721 | // on the object argument, then let ActOnCallExpr finish the job. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8722 | |
Fariborz Jahanian | 774cf79 | 2009-09-28 18:35:46 +0000 | [diff] [blame] | 8723 | // Create an implicit member expr to refer to the conversion operator. |
Fariborz Jahanian | 78cfcb5 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 8724 | // and then call it. |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 8725 | ExprResult Call = BuildCXXMemberCallExpr(Object, Best->FoundDecl, Conv); |
| 8726 | if (Call.isInvalid()) |
| 8727 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8728 | |
Douglas Gregor | 668443e | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 8729 | return ActOnCallExpr(S, Call.get(), LParenLoc, MultiExprArg(Args, NumArgs), |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8730 | RParenLoc); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8733 | MarkDeclarationReferenced(LParenLoc, Best->Function); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8734 | CheckMemberOperatorAccess(LParenLoc, Object, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8735 | DiagnoseUseOfDecl(Best->FoundDecl, LParenLoc); |
John McCall | 49ec2e6 | 2010-01-28 01:54:34 +0000 | [diff] [blame] | 8736 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 8737 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 8738 | // that calls this method, using Object for the implicit object |
| 8739 | // parameter and passing along the remaining arguments. |
| 8740 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8741 | const FunctionProtoType *Proto = |
| 8742 | Method->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8743 | |
| 8744 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 8745 | unsigned NumArgsToCheck = NumArgs; |
| 8746 | |
| 8747 | // Build the full argument list for the method call (the |
| 8748 | // implicit object parameter is placed at the beginning of the |
| 8749 | // list). |
| 8750 | Expr **MethodArgs; |
| 8751 | if (NumArgs < NumArgsInProto) { |
| 8752 | NumArgsToCheck = NumArgsInProto; |
| 8753 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 8754 | } else { |
| 8755 | MethodArgs = new Expr*[NumArgs + 1]; |
| 8756 | } |
| 8757 | MethodArgs[0] = Object; |
| 8758 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 8759 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8760 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8761 | Expr *NewFn = CreateFunctionRefExpr(*this, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8762 | |
| 8763 | // Once we've built TheCall, all of the expressions are properly |
| 8764 | // owned. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8765 | QualType ResultTy = Method->getResultType(); |
| 8766 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8767 | ResultTy = ResultTy.getNonLValueExprType(Context); |
| 8768 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8769 | CXXOperatorCallExpr *TheCall = |
| 8770 | new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
| 8771 | MethodArgs, NumArgs + 1, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8772 | ResultTy, VK, RParenLoc); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8773 | delete [] MethodArgs; |
| 8774 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8775 | if (CheckCallReturnType(Method->getResultType(), LParenLoc, TheCall, |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 8776 | Method)) |
| 8777 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8778 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8779 | // We may have default arguments. If so, we need to allocate more |
| 8780 | // slots in the call for them. |
| 8781 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 8782 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8783 | else if (NumArgs > NumArgsInProto) |
| 8784 | NumArgsToCheck = NumArgsInProto; |
| 8785 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8786 | bool IsError = false; |
| 8787 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8788 | // Initialize the implicit object parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8789 | IsError |= PerformObjectArgumentInitialization(Object, /*Qualifier=*/0, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8790 | Best->FoundDecl, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8791 | TheCall->setArg(0, Object); |
| 8792 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8793 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8794 | // Check the argument types. |
| 8795 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8796 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8797 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8798 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8799 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8800 | // Pass the argument. |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8801 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8802 | ExprResult InputInit |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8803 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8804 | Context, |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8805 | Method->getParamDecl(i)), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8806 | SourceLocation(), Arg); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8807 | |
Anders Carlsson | 7c5fe48 | 2010-01-29 18:43:53 +0000 | [diff] [blame] | 8808 | IsError |= InputInit.isInvalid(); |
| 8809 | Arg = InputInit.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8810 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8811 | ExprResult DefArg |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 8812 | = BuildCXXDefaultArgExpr(LParenLoc, Method, Method->getParamDecl(i)); |
| 8813 | if (DefArg.isInvalid()) { |
| 8814 | IsError = true; |
| 8815 | break; |
| 8816 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8817 | |
Douglas Gregor | 1bc688d | 2009-11-09 19:27:57 +0000 | [diff] [blame] | 8818 | Arg = DefArg.takeAs<Expr>(); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 8819 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8820 | |
| 8821 | TheCall->setArg(i + 1, Arg); |
| 8822 | } |
| 8823 | |
| 8824 | // If this is a variadic call, handle args passed through "...". |
| 8825 | if (Proto->isVariadic()) { |
| 8826 | // Promote the arguments (C99 6.5.2.2p7). |
| 8827 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 8828 | Expr *Arg = Args[i]; |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 8829 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod, 0); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8830 | TheCall->setArg(i + 1, Arg); |
| 8831 | } |
| 8832 | } |
| 8833 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 8834 | if (IsError) return true; |
| 8835 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8836 | if (CheckFunctionCall(Method, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 8837 | return true; |
| 8838 | |
John McCall | e172be5 | 2010-08-24 06:09:16 +0000 | [diff] [blame] | 8839 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 8840 | } |
| 8841 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8842 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8843 | /// (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] | 8844 | /// @c Member is the name of the member we're trying to find. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8845 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8846 | Sema::BuildOverloadedArrowExpr(Scope *S, Expr *Base, SourceLocation OpLoc) { |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8847 | assert(Base->getType()->isRecordType() && |
| 8848 | "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8849 | |
John McCall | e26a872 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 8850 | if (Base->getObjectKind() == OK_ObjCProperty) |
| 8851 | ConvertPropertyForRValue(Base); |
| 8852 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8853 | SourceLocation Loc = Base->getExprLoc(); |
| 8854 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8855 | // C++ [over.ref]p1: |
| 8856 | // |
| 8857 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 8858 | // for a class object x of type T if T::operator->() exists and if |
| 8859 | // the operator is selected as the best match function by the |
| 8860 | // overload resolution mechanism (13.3). |
Chandler Carruth | 8e543b3 | 2010-12-12 08:17:55 +0000 | [diff] [blame] | 8861 | DeclarationName OpName = |
| 8862 | Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8863 | OverloadCandidateSet CandidateSet(Loc); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8864 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8865 | |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 8866 | if (RequireCompleteType(Loc, Base->getType(), |
Eli Friedman | 132e70b | 2009-11-18 01:28:03 +0000 | [diff] [blame] | 8867 | PDiag(diag::err_typecheck_incomplete_tag) |
| 8868 | << Base->getSourceRange())) |
| 8869 | return ExprError(); |
| 8870 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8871 | LookupResult R(*this, OpName, OpLoc, LookupOrdinaryName); |
| 8872 | LookupQualifiedName(R, BaseRecord->getDecl()); |
| 8873 | R.suppressDiagnostics(); |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 8874 | |
| 8875 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8876 | Oper != OperEnd; ++Oper) { |
Douglas Gregor | 0282432 | 2011-01-26 19:30:28 +0000 | [diff] [blame] | 8877 | AddMethodCandidate(Oper.getPair(), Base->getType(), Base->Classify(Context), |
| 8878 | 0, 0, CandidateSet, /*SuppressUserConversions=*/false); |
John McCall | 6e9f8f6 | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 8879 | } |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8880 | |
| 8881 | // Perform overload resolution. |
| 8882 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8883 | switch (CandidateSet.BestViableFunction(*this, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8884 | case OR_Success: |
| 8885 | // Overload resolution succeeded; we'll build the call below. |
| 8886 | break; |
| 8887 | |
| 8888 | case OR_No_Viable_Function: |
| 8889 | if (CandidateSet.empty()) |
| 8890 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8891 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8892 | else |
| 8893 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8894 | << "operator->" << Base->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8895 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8896 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8897 | |
| 8898 | case OR_Ambiguous: |
Douglas Gregor | 052caec | 2010-11-13 20:06:38 +0000 | [diff] [blame] | 8899 | Diag(OpLoc, diag::err_ovl_ambiguous_oper_unary) |
| 8900 | << "->" << Base->getType() << Base->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8901 | CandidateSet.NoteCandidates(*this, OCD_ViableCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8902 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 8903 | |
| 8904 | case OR_Deleted: |
| 8905 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 8906 | << Best->Function->isDeleted() |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8907 | << "->" |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 8908 | << getDeletedOrUnavailableSuffix(Best->Function) |
Fariborz Jahanian | e6b127d | 2011-02-25 20:51:14 +0000 | [diff] [blame] | 8909 | << Base->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 8910 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, &Base, 1); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8911 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8912 | } |
| 8913 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 8914 | MarkDeclarationReferenced(OpLoc, Best->Function); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8915 | CheckMemberOperatorAccess(OpLoc, Base, 0, Best->FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 8916 | DiagnoseUseOfDecl(Best->FoundDecl, OpLoc); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 8917 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8918 | // Convert the object parameter. |
| 8919 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8920 | if (PerformObjectArgumentInitialization(Base, /*Qualifier=*/0, |
| 8921 | Best->FoundDecl, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 8922 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 8923 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8924 | // Build the operator call. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8925 | Expr *FnExpr = CreateFunctionRefExpr(*this, Method); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8926 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8927 | QualType ResultTy = Method->getResultType(); |
| 8928 | ExprValueKind VK = Expr::getValueKindForType(ResultTy); |
| 8929 | ResultTy = ResultTy.getNonLValueExprType(Context); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8930 | CXXOperatorCallExpr *TheCall = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8931 | new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8932 | &Base, 1, ResultTy, VK, OpLoc); |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8933 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8934 | if (CheckCallReturnType(Method->getResultType(), OpLoc, TheCall, |
Anders Carlsson | e4f4b5e | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 8935 | Method)) |
| 8936 | return ExprError(); |
Eli Friedman | 2d9c47e | 2011-04-04 01:18:25 +0000 | [diff] [blame] | 8937 | |
| 8938 | return MaybeBindToTemporary(TheCall); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 8939 | } |
| 8940 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8941 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 8942 | /// a C++ overloaded function (possibly with some parentheses and |
| 8943 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 8944 | /// to the function declaration Fn, so patch up the expression E to |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 8945 | /// refer (possibly indirectly) to Fn. Returns the new expr. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 8946 | Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8947 | FunctionDecl *Fn) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8948 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8949 | Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), |
| 8950 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8951 | if (SubExpr == PE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 8952 | return PE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8953 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8954 | return new (Context) ParenExpr(PE->getLParen(), PE->getRParen(), SubExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8955 | } |
| 8956 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8957 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8958 | Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), |
| 8959 | Found, Fn); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8960 | assert(Context.hasSameType(ICE->getSubExpr()->getType(), |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8961 | SubExpr->getType()) && |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 8962 | "Implicit cast type cannot be determined from overload"); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 8963 | assert(ICE->path_empty() && "fixing up hierarchy conversion?"); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8964 | if (SubExpr == ICE->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 8965 | return ICE; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8966 | |
| 8967 | return ImplicitCastExpr::Create(Context, ICE->getType(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 8968 | ICE->getCastKind(), |
| 8969 | SubExpr, 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 8970 | ICE->getValueKind()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 8971 | } |
| 8972 | |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8973 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8974 | assert(UnOp->getOpcode() == UO_AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8975 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 8976 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 8977 | if (Method->isStatic()) { |
| 8978 | // Do nothing: static member functions aren't any different |
| 8979 | // from non-member functions. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8980 | } else { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 8981 | // Fix the sub expression, which really has to be an |
| 8982 | // UnresolvedLookupExpr holding an overloaded member function |
| 8983 | // or template. |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 8984 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 8985 | Found, Fn); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8986 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 8987 | return UnOp; |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 8988 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8989 | assert(isa<DeclRefExpr>(SubExpr) |
| 8990 | && "fixed to something other than a decl ref"); |
| 8991 | assert(cast<DeclRefExpr>(SubExpr)->getQualifier() |
| 8992 | && "fixed to a member ref with no nested name qualifier"); |
| 8993 | |
| 8994 | // We have taken the address of a pointer to member |
| 8995 | // function. Perform the computation here so that we get the |
| 8996 | // appropriate pointer to member type. |
| 8997 | QualType ClassType |
| 8998 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 8999 | QualType MemPtrType |
| 9000 | = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); |
| 9001 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9002 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, MemPtrType, |
| 9003 | VK_RValue, OK_Ordinary, |
| 9004 | UnOp->getOperatorLoc()); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 9005 | } |
| 9006 | } |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9007 | Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), |
| 9008 | Found, Fn); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9009 | if (SubExpr == UnOp->getSubExpr()) |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9010 | return UnOp; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9011 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9012 | return new (Context) UnaryOperator(SubExpr, UO_AddrOf, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9013 | Context.getPointerType(SubExpr->getType()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9014 | VK_RValue, OK_Ordinary, |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9015 | UnOp->getOperatorLoc()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9016 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9017 | |
| 9018 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9019 | // FIXME: avoid copy. |
| 9020 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9021 | if (ULE->hasExplicitTemplateArgs()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9022 | ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9023 | TemplateArgs = &TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 9024 | } |
| 9025 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9026 | return DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9027 | ULE->getQualifierLoc(), |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9028 | Fn, |
| 9029 | ULE->getNameLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9030 | Fn->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9031 | VK_LValue, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9032 | TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 9033 | } |
| 9034 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 9035 | if (UnresolvedMemberExpr *MemExpr = dyn_cast<UnresolvedMemberExpr>(E)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9036 | // FIXME: avoid copy. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9037 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = 0; |
| 9038 | if (MemExpr->hasExplicitTemplateArgs()) { |
| 9039 | MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); |
| 9040 | TemplateArgs = &TemplateArgsBuffer; |
| 9041 | } |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 9042 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9043 | Expr *Base; |
| 9044 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9045 | // If we're filling in a static method where we used to have an |
| 9046 | // implicit member access, rewrite to a simple decl ref. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9047 | if (MemExpr->isImplicitAccess()) { |
| 9048 | if (cast<CXXMethodDecl>(Fn)->isStatic()) { |
| 9049 | return DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9050 | MemExpr->getQualifierLoc(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9051 | Fn, |
| 9052 | MemExpr->getMemberLoc(), |
| 9053 | Fn->getType(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9054 | VK_LValue, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9055 | TemplateArgs); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9056 | } else { |
| 9057 | SourceLocation Loc = MemExpr->getMemberLoc(); |
| 9058 | if (MemExpr->getQualifier()) |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 9059 | Loc = MemExpr->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 9060 | Base = new (Context) CXXThisExpr(Loc, |
| 9061 | MemExpr->getBaseType(), |
| 9062 | /*isImplicit=*/true); |
| 9063 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9064 | } else |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9065 | Base = MemExpr->getBase(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9066 | |
| 9067 | return MemberExpr::Create(Context, Base, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9068 | MemExpr->isArrow(), |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 9069 | MemExpr->getQualifierLoc(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9070 | Fn, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9071 | Found, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 9072 | MemExpr->getMemberNameInfo(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 9073 | TemplateArgs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9074 | Fn->getType(), |
| 9075 | cast<CXXMethodDecl>(Fn)->isStatic() |
| 9076 | ? VK_LValue : VK_RValue, |
| 9077 | OK_Ordinary); |
Douglas Gregor | 51c538b | 2009-11-20 19:42:02 +0000 | [diff] [blame] | 9078 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9079 | |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9080 | llvm_unreachable("Invalid reference to overloaded function"); |
| 9081 | return E; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 9082 | } |
| 9083 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 9084 | ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9085 | DeclAccessPair Found, |
| 9086 | FunctionDecl *Fn) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 9087 | return Owned(FixOverloadedFunctionReference((Expr *)E.get(), Found, Fn)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 9088 | } |
| 9089 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 9090 | } // end namespace clang |