Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1 | //===--- SemaOverload.cpp - C++ Overloading ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ overloading. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 58e008d | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <algorithm> |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | /// GetConversionCategory - Retrieve the implicit conversion |
| 32 | /// category corresponding to the given implicit conversion kind. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 34 | GetConversionCategory(ImplicitConversionKind Kind) { |
| 35 | static const ImplicitConversionCategory |
| 36 | Category[(int)ICK_Num_Conversion_Kinds] = { |
| 37 | ICC_Identity, |
| 38 | ICC_Lvalue_Transformation, |
| 39 | ICC_Lvalue_Transformation, |
| 40 | ICC_Lvalue_Transformation, |
| 41 | ICC_Qualification_Adjustment, |
| 42 | ICC_Promotion, |
| 43 | ICC_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 44 | ICC_Promotion, |
| 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 47 | ICC_Conversion, |
| 48 | ICC_Conversion, |
| 49 | ICC_Conversion, |
| 50 | ICC_Conversion, |
| 51 | ICC_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 52 | ICC_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 54 | ICC_Conversion |
| 55 | }; |
| 56 | return Category[(int)Kind]; |
| 57 | } |
| 58 | |
| 59 | /// GetConversionRank - Retrieve the implicit conversion rank |
| 60 | /// corresponding to the given implicit conversion kind. |
| 61 | ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind) { |
| 62 | static const ImplicitConversionRank |
| 63 | Rank[(int)ICK_Num_Conversion_Kinds] = { |
| 64 | ICR_Exact_Match, |
| 65 | ICR_Exact_Match, |
| 66 | ICR_Exact_Match, |
| 67 | ICR_Exact_Match, |
| 68 | ICR_Exact_Match, |
| 69 | ICR_Promotion, |
| 70 | ICR_Promotion, |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 74 | ICR_Conversion, |
| 75 | ICR_Conversion, |
| 76 | ICR_Conversion, |
| 77 | ICR_Conversion, |
| 78 | ICR_Conversion, |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 79 | ICR_Conversion, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 80 | ICR_Conversion, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 81 | ICR_Conversion |
| 82 | }; |
| 83 | return Rank[(int)Kind]; |
| 84 | } |
| 85 | |
| 86 | /// GetImplicitConversionName - Return the name of this kind of |
| 87 | /// implicit conversion. |
| 88 | const char* GetImplicitConversionName(ImplicitConversionKind Kind) { |
| 89 | static const char* Name[(int)ICK_Num_Conversion_Kinds] = { |
| 90 | "No conversion", |
| 91 | "Lvalue-to-rvalue", |
| 92 | "Array-to-pointer", |
| 93 | "Function-to-pointer", |
| 94 | "Qualification", |
| 95 | "Integral promotion", |
| 96 | "Floating point promotion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | "Complex promotion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Integral conversion", |
| 99 | "Floating conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Floating-integral conversion", |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 102 | "Complex-real conversion", |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 103 | "Pointer conversion", |
| 104 | "Pointer-to-member conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Boolean conversion", |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | "Compatible-types conversion", |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 107 | "Derived-to-base conversion" |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | }; |
| 109 | return Name[Kind]; |
| 110 | } |
| 111 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 112 | /// StandardConversionSequence - Set the standard conversion |
| 113 | /// sequence to the identity conversion. |
| 114 | void StandardConversionSequence::setAsIdentityConversion() { |
| 115 | First = ICK_Identity; |
| 116 | Second = ICK_Identity; |
| 117 | Third = ICK_Identity; |
| 118 | Deprecated = false; |
| 119 | ReferenceBinding = false; |
| 120 | DirectBinding = false; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 121 | RRefBinding = false; |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 122 | CopyConstructor = 0; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 125 | /// getRank - Retrieve the rank of this standard conversion sequence |
| 126 | /// (C++ 13.3.3.1.1p3). The rank is the largest rank of each of the |
| 127 | /// implicit conversions. |
| 128 | ImplicitConversionRank StandardConversionSequence::getRank() const { |
| 129 | ImplicitConversionRank Rank = ICR_Exact_Match; |
| 130 | if (GetConversionRank(First) > Rank) |
| 131 | Rank = GetConversionRank(First); |
| 132 | if (GetConversionRank(Second) > Rank) |
| 133 | Rank = GetConversionRank(Second); |
| 134 | if (GetConversionRank(Third) > Rank) |
| 135 | Rank = GetConversionRank(Third); |
| 136 | return Rank; |
| 137 | } |
| 138 | |
| 139 | /// isPointerConversionToBool - Determines whether this conversion is |
| 140 | /// 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] | 141 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | /// (C++ 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 144 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 145 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 146 | |
| 147 | // Note that FromType has not necessarily been transformed by the |
| 148 | // array-to-pointer or function-to-pointer implicit conversions, so |
| 149 | // check for their presence as well as checking whether FromType is |
| 150 | // a pointer. |
| 151 | if (ToType->isBooleanType() && |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 152 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 153 | First == ICK_Array_To_Pointer || First == ICK_Function_To_Pointer)) |
| 154 | return true; |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 159 | /// isPointerConversionToVoidPointer - Determines whether this |
| 160 | /// conversion is a conversion of a pointer to a void pointer. This is |
| 161 | /// used as part of the ranking of standard conversion sequences (C++ |
| 162 | /// 13.3.3.2p4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 166 | QualType FromType = QualType::getFromOpaquePtr(FromTypePtr); |
| 167 | QualType ToType = QualType::getFromOpaquePtr(ToTypePtr); |
| 168 | |
| 169 | // Note that FromType has not necessarily been transformed by the |
| 170 | // array-to-pointer implicit conversion, so check for its presence |
| 171 | // and redo the conversion to get a pointer. |
| 172 | if (First == ICK_Array_To_Pointer) |
| 173 | FromType = Context.getArrayDecayedType(FromType); |
| 174 | |
| 175 | if (Second == ICK_Pointer_Conversion) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 182 | /// DebugPrint - Print this standard conversion sequence to standard |
| 183 | /// error. Useful for debugging overloading issues. |
| 184 | void StandardConversionSequence::DebugPrint() const { |
| 185 | bool PrintedSomething = false; |
| 186 | if (First != ICK_Identity) { |
| 187 | fprintf(stderr, "%s", GetImplicitConversionName(First)); |
| 188 | PrintedSomething = true; |
| 189 | } |
| 190 | |
| 191 | if (Second != ICK_Identity) { |
| 192 | if (PrintedSomething) { |
| 193 | fprintf(stderr, " -> "); |
| 194 | } |
| 195 | fprintf(stderr, "%s", GetImplicitConversionName(Second)); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 196 | |
| 197 | if (CopyConstructor) { |
| 198 | fprintf(stderr, " (by copy constructor)"); |
| 199 | } else if (DirectBinding) { |
| 200 | fprintf(stderr, " (direct reference binding)"); |
| 201 | } else if (ReferenceBinding) { |
| 202 | fprintf(stderr, " (reference binding)"); |
| 203 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 204 | PrintedSomething = true; |
| 205 | } |
| 206 | |
| 207 | if (Third != ICK_Identity) { |
| 208 | if (PrintedSomething) { |
| 209 | fprintf(stderr, " -> "); |
| 210 | } |
| 211 | fprintf(stderr, "%s", GetImplicitConversionName(Third)); |
| 212 | PrintedSomething = true; |
| 213 | } |
| 214 | |
| 215 | if (!PrintedSomething) { |
| 216 | fprintf(stderr, "No conversions required"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// DebugPrint - Print this user-defined conversion sequence to standard |
| 221 | /// error. Useful for debugging overloading issues. |
| 222 | void UserDefinedConversionSequence::DebugPrint() const { |
| 223 | if (Before.First || Before.Second || Before.Third) { |
| 224 | Before.DebugPrint(); |
| 225 | fprintf(stderr, " -> "); |
| 226 | } |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 228 | if (After.First || After.Second || After.Third) { |
| 229 | fprintf(stderr, " -> "); |
| 230 | After.DebugPrint(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /// DebugPrint - Print this implicit conversion sequence to standard |
| 235 | /// error. Useful for debugging overloading issues. |
| 236 | void ImplicitConversionSequence::DebugPrint() const { |
| 237 | switch (ConversionKind) { |
| 238 | case StandardConversion: |
| 239 | fprintf(stderr, "Standard conversion: "); |
| 240 | Standard.DebugPrint(); |
| 241 | break; |
| 242 | case UserDefinedConversion: |
| 243 | fprintf(stderr, "User-defined conversion: "); |
| 244 | UserDefined.DebugPrint(); |
| 245 | break; |
| 246 | case EllipsisConversion: |
| 247 | fprintf(stderr, "Ellipsis conversion"); |
| 248 | break; |
| 249 | case BadConversion: |
| 250 | fprintf(stderr, "Bad conversion"); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | fprintf(stderr, "\n"); |
| 255 | } |
| 256 | |
| 257 | // IsOverload - Determine whether the given New declaration is an |
| 258 | // overload of the Old declaration. This routine returns false if New |
| 259 | // and Old cannot be overloaded, e.g., if they are functions with the |
| 260 | // same signature (C++ 1.3.10) or if the Old declaration isn't a |
| 261 | // function (or overload set). When it does return false and Old is an |
| 262 | // OverloadedFunctionDecl, MatchedDecl will be set to point to the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | // FunctionDecl that New cannot be overloaded with. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 264 | // |
| 265 | // Example: Given the following input: |
| 266 | // |
| 267 | // void f(int, float); // #1 |
| 268 | // void f(int, int); // #2 |
| 269 | // int f(int, int); // #3 |
| 270 | // |
| 271 | // When we process #1, there is no previous declaration of "f", |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | // so IsOverload will not be used. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 273 | // |
| 274 | // When we process #2, Old is a FunctionDecl for #1. By comparing the |
| 275 | // parameter types, we see that #1 and #2 are overloaded (since they |
| 276 | // have different signatures), so this routine returns false; |
| 277 | // MatchedDecl is unchanged. |
| 278 | // |
| 279 | // When we process #3, Old is an OverloadedFunctionDecl containing #1 |
| 280 | // and #2. We compare the signatures of #3 to #1 (they're overloaded, |
| 281 | // so we do nothing) and then #3 to #2. Since the signatures of #3 and |
| 282 | // #2 are identical (return types of functions are not part of the |
| 283 | // signature), IsOverload returns false and MatchedDecl will be set to |
| 284 | // point to the FunctionDecl for #2. |
| 285 | bool |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 288 | if (OverloadedFunctionDecl* Ovl = dyn_cast<OverloadedFunctionDecl>(OldD)) { |
| 289 | // Is this new function an overload of every function in the |
| 290 | // overload set? |
| 291 | OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 292 | FuncEnd = Ovl->function_end(); |
| 293 | for (; Func != FuncEnd; ++Func) { |
| 294 | if (!IsOverload(New, *Func, MatchedDecl)) { |
| 295 | MatchedDecl = Func; |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // This function overloads every function in the overload set. |
| 301 | return true; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 302 | } else if (FunctionTemplateDecl *Old = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 303 | return IsOverload(New, Old->getTemplatedDecl(), MatchedDecl); |
| 304 | else if (FunctionDecl* Old = dyn_cast<FunctionDecl>(OldD)) { |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 305 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 307 | |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 308 | // C++ [temp.fct]p2: |
| 309 | // A function template can be overloaded with other function templates |
| 310 | // and with normal (non-template) functions. |
| 311 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 312 | return true; |
| 313 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 314 | // Is the function New an overload of the function Old? |
| 315 | QualType OldQType = Context.getCanonicalType(Old->getType()); |
| 316 | QualType NewQType = Context.getCanonicalType(New->getType()); |
| 317 | |
| 318 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 319 | // determine whether they are overloads. If we find any mismatch |
| 320 | // in the signature, they are overloads. |
| 321 | |
| 322 | // If either of these functions is a K&R-style function (no |
| 323 | // prototype), then we consider them to have matching signatures. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 324 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 325 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 326 | return false; |
| 327 | |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 328 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 329 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 330 | |
| 331 | // The signature of a function includes the types of its |
| 332 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 333 | // of the ellipsis; see C++ DR 357). |
| 334 | if (OldQType != NewQType && |
| 335 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 336 | OldType->isVariadic() != NewType->isVariadic() || |
| 337 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 338 | NewType->arg_type_begin()))) |
| 339 | return true; |
| 340 | |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 341 | // C++ [temp.over.link]p4: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | // The signature of a function template consists of its function |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 343 | // signature, its return type and its template parameter list. The names |
| 344 | // of the template parameters are significant only for establishing the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | // relationship between the template parameters and the rest of the |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 346 | // signature. |
| 347 | // |
| 348 | // We check the return type and template parameter lists for function |
| 349 | // templates first; the remaining checks follow. |
| 350 | if (NewTemplate && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 352 | OldTemplate->getTemplateParameters(), |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 353 | false, false, SourceLocation()) || |
| 354 | OldType->getResultType() != NewType->getResultType())) |
| 355 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 357 | // If the function is a class member, its signature includes the |
| 358 | // cv-qualifiers (if any) on the function itself. |
| 359 | // |
| 360 | // As part of this, also check whether one of the member functions |
| 361 | // is static, in which case they are not overloads (C++ |
| 362 | // 13.1p2). While not part of the definition of the signature, |
| 363 | // this check is important to determine whether these functions |
| 364 | // can be overloaded. |
| 365 | CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 366 | CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | if (OldMethod && NewMethod && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 368 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | b81897c | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 369 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 370 | return true; |
| 371 | |
| 372 | // The signatures match; this is not an overload. |
| 373 | return false; |
| 374 | } else { |
| 375 | // (C++ 13p1): |
| 376 | // Only function declarations can be overloaded; object and type |
| 377 | // declarations cannot be overloaded. |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 382 | /// TryImplicitConversion - Attempt to perform an implicit conversion |
| 383 | /// from the given expression (Expr) to the given type (ToType). This |
| 384 | /// function returns an implicit conversion sequence that can be used |
| 385 | /// to perform the initialization. Given |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 386 | /// |
| 387 | /// void f(float f); |
| 388 | /// void g(int i) { f(i); } |
| 389 | /// |
| 390 | /// this routine would produce an implicit conversion sequence to |
| 391 | /// describe the initialization of f from i, which will be a standard |
| 392 | /// conversion sequence containing an lvalue-to-rvalue conversion (C++ |
| 393 | /// 4.1) followed by a floating-integral conversion (C++ 4.9). |
| 394 | // |
| 395 | /// Note that this routine only determines how the conversion can be |
| 396 | /// performed; it does not actually perform the conversion. As such, |
| 397 | /// it will not produce any diagnostics if no conversion is available, |
| 398 | /// but will instead return an implicit conversion sequence of kind |
| 399 | /// "BadConversion". |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 400 | /// |
| 401 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 402 | /// not permitted. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 403 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 404 | /// permitted. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 405 | /// If @p ForceRValue, then overloading is performed as if From was an rvalue, |
| 406 | /// no matter its actual lvalueness. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 407 | ImplicitConversionSequence |
Anders Carlsson | 5ec4abf | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 408 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 409 | bool SuppressUserConversions, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 410 | bool AllowExplicit, bool ForceRValue, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | bool InOverloadResolution) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | ImplicitConversionSequence ICS; |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 413 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 414 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 415 | else if (getLangOptions().CPlusPlus && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 417 | !SuppressUserConversions, AllowExplicit, |
| 418 | ForceRValue)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 419 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 420 | // C++ [over.ics.user]p4: |
| 421 | // A conversion of an expression of class type to the same class |
| 422 | // type is given Exact Match rank, and a conversion of an |
| 423 | // expression of class type to a base class of that type is |
| 424 | // given Conversion rank, in spite of the fact that a copy |
| 425 | // constructor (i.e., a user-defined conversion function) is |
| 426 | // called for those cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 428 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | QualType FromCanon |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 430 | = Context.getCanonicalType(From->getType().getUnqualifiedType()); |
| 431 | QualType ToCanon = Context.getCanonicalType(ToType).getUnqualifiedType(); |
| 432 | if (FromCanon == ToCanon || IsDerivedFrom(FromCanon, ToCanon)) { |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 433 | // Turn this into a "standard" conversion sequence, so that it |
| 434 | // gets ranked with standard conversion sequences. |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 435 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 436 | ICS.Standard.setAsIdentityConversion(); |
| 437 | ICS.Standard.FromTypePtr = From->getType().getAsOpaquePtr(); |
| 438 | ICS.Standard.ToTypePtr = ToType.getAsOpaquePtr(); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 439 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | bb2e6883 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 440 | if (ToCanon != FromCanon) |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 441 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 442 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 443 | } |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 444 | |
| 445 | // C++ [over.best.ics]p4: |
| 446 | // However, when considering the argument of a user-defined |
| 447 | // conversion function that is a candidate by 13.3.1.3 when |
| 448 | // invoked for the copying of the temporary in the second step |
| 449 | // of a class copy-initialization, or by 13.3.1.4, 13.3.1.5, or |
| 450 | // 13.3.1.6 in all cases, only standard conversion sequences and |
| 451 | // ellipsis conversion sequences are allowed. |
| 452 | if (SuppressUserConversions && |
| 453 | ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion) |
| 454 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 455 | } else |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 456 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 457 | |
| 458 | return ICS; |
| 459 | } |
| 460 | |
| 461 | /// IsStandardConversion - Determines whether there is a standard |
| 462 | /// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the |
| 463 | /// expression From to the type ToType. Standard conversion sequences |
| 464 | /// only consider non-class types; for conversions that involve class |
| 465 | /// types, use TryImplicitConversion. If a conversion exists, SCS will |
| 466 | /// contain the standard conversion sequence required to perform this |
| 467 | /// conversion and this routine will return true. Otherwise, this |
| 468 | /// routine will return false and the value of SCS is unspecified. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | bool |
| 470 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 471 | bool InOverloadResolution, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | StandardConversionSequence &SCS) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 473 | QualType FromType = From->getType(); |
| 474 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 475 | // Standard conversions (C++ [conv]) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 476 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 477 | SCS.Deprecated = false; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 478 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 479 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 480 | SCS.CopyConstructor = 0; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 482 | // There are no standard conversions for class types in C++, so |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 484 | if (FromType->isRecordType() || ToType->isRecordType()) { |
| 485 | if (getLangOptions().CPlusPlus) |
| 486 | return false; |
| 487 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 491 | // The first conversion can be an lvalue-to-rvalue conversion, |
| 492 | // array-to-pointer conversion, or function-to-pointer conversion |
| 493 | // (C++ 4p1). |
| 494 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 496 | // An lvalue (3.10) of a non-function, non-array type T can be |
| 497 | // converted to an rvalue. |
| 498 | Expr::isLvalueResult argIsLvalue = From->isLvalue(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 500 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 501 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 502 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 503 | |
| 504 | // If T is a non-class type, the type of the rvalue is the |
| 505 | // cv-unqualified version of T. Otherwise, the type of the rvalue |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 506 | // is T (C++ 4.1p1). C++ can't get here with class types; in C, we |
| 507 | // just strip the qualifiers because they don't matter. |
| 508 | |
| 509 | // FIXME: Doesn't see through to qualifiers behind a typedef! |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 510 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 511 | } else if (FromType->isArrayType()) { |
| 512 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 513 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 514 | |
| 515 | // An lvalue or rvalue of type "array of N T" or "array of unknown |
| 516 | // bound of T" can be converted to an rvalue of type "pointer to |
| 517 | // T" (C++ 4.2p1). |
| 518 | FromType = Context.getArrayDecayedType(FromType); |
| 519 | |
| 520 | if (IsStringLiteralToNonConstPointerConversion(From, ToType)) { |
| 521 | // This conversion is deprecated. (C++ D.4). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 522 | SCS.Deprecated = true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 523 | |
| 524 | // For the purpose of ranking in overload resolution |
| 525 | // (13.3.3.1.1), this conversion is considered an |
| 526 | // array-to-pointer conversion followed by a qualification |
| 527 | // conversion (4.4). (C++ 4.2p2) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 528 | SCS.Second = ICK_Identity; |
| 529 | SCS.Third = ICK_Qualification; |
| 530 | SCS.ToTypePtr = ToType.getAsOpaquePtr(); |
| 531 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 533 | } else if (FromType->isFunctionType() && argIsLvalue == Expr::LV_Valid) { |
| 534 | // Function-to-pointer conversion (C++ 4.3). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 535 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 536 | |
| 537 | // An lvalue of function type T can be converted to an rvalue of |
| 538 | // type "pointer to T." The result is a pointer to the |
| 539 | // function. (C++ 4.3p1). |
| 540 | FromType = Context.getPointerType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | } else if (FunctionDecl *Fn |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 542 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 543 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 544 | SCS.First = ICK_Function_To_Pointer; |
| 545 | |
| 546 | // We were able to resolve the address of the overloaded function, |
| 547 | // so we can convert to the type of that function. |
| 548 | FromType = Fn->getType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 549 | if (ToType->isLValueReferenceType()) |
| 550 | FromType = Context.getLValueReferenceType(FromType); |
| 551 | else if (ToType->isRValueReferenceType()) |
| 552 | FromType = Context.getRValueReferenceType(FromType); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 553 | else if (ToType->isMemberPointerType()) { |
| 554 | // Resolve address only succeeds if both sides are member pointers, |
| 555 | // but it doesn't have to be the same class. See DR 247. |
| 556 | // Note that this means that the type of &Derived::fn can be |
| 557 | // Ret (Base::*)(Args) if the fn overload actually found is from the |
| 558 | // base class, even if it was brought into the derived class via a |
| 559 | // using declaration. The standard isn't clear on this issue at all. |
| 560 | CXXMethodDecl *M = cast<CXXMethodDecl>(Fn); |
| 561 | FromType = Context.getMemberPointerType(FromType, |
| 562 | Context.getTypeDeclType(M->getParent()).getTypePtr()); |
| 563 | } else |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 564 | FromType = Context.getPointerType(FromType); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 565 | } else { |
| 566 | // We don't require any conversions for the first step. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 567 | SCS.First = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // The second conversion can be an integral promotion, floating |
| 571 | // point promotion, integral conversion, floating point conversion, |
| 572 | // floating-integral conversion, pointer conversion, |
| 573 | // pointer-to-member conversion, or boolean conversion (C++ 4p1). |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 574 | // For overloading in C, this can also be a "compatible-type" |
| 575 | // conversion. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 576 | bool IncompatibleObjC = false; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 577 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 578 | // The unqualified versions of the types are the same: there's no |
| 579 | // conversion to do. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 580 | SCS.Second = ICK_Identity; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 581 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 583 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 584 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 585 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 586 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 587 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 588 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 589 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 590 | // Complex promotion (Clang extension) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 591 | SCS.Second = ICK_Complex_Promotion; |
| 592 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 593 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 594 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 595 | // Integral conversions (C++ 4.7). |
| 596 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 597 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 599 | } else if (FromType->isFloatingType() && ToType->isFloatingType()) { |
| 600 | // Floating point conversions (C++ 4.8). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 601 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 602 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 603 | } else if (FromType->isComplexType() && ToType->isComplexType()) { |
| 604 | // Complex conversions (C99 6.3.1.6) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 605 | SCS.Second = ICK_Complex_Conversion; |
| 606 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 607 | } else if ((FromType->isFloatingType() && |
| 608 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 609 | !ToType->isEnumeralType())) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 611 | ToType->isFloatingType())) { |
| 612 | // Floating-integral conversions (C++ 4.9). |
| 613 | // FIXME: isIntegralType shouldn't be true for enums in C++. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 614 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 615 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 616 | } else if ((FromType->isComplexType() && ToType->isArithmeticType()) || |
| 617 | (ToType->isComplexType() && FromType->isArithmeticType())) { |
| 618 | // Complex-real conversions (C99 6.3.1.7) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 619 | SCS.Second = ICK_Complex_Real; |
| 620 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 621 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 622 | FromType, IncompatibleObjC)) { |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 623 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 624 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 625 | SCS.IncompatibleObjC = IncompatibleObjC; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 626 | } else if (IsMemberPointerConversion(From, FromType, ToType, FromType)) { |
| 627 | // Pointer to member conversions (4.11). |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 628 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 629 | } else if (ToType->isBooleanType() && |
| 630 | (FromType->isArithmeticType() || |
| 631 | FromType->isEnumeralType() || |
| 632 | FromType->isPointerType() || |
| 633 | FromType->isBlockPointerType() || |
| 634 | FromType->isMemberPointerType() || |
| 635 | FromType->isNullPtrType())) { |
| 636 | // Boolean conversions (C++ 4.12). |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 637 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 638 | FromType = Context.BoolTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 640 | Context.typesAreCompatible(ToType, FromType)) { |
| 641 | // Compatible conversions (Clang extension for C function overloading) |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 642 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 643 | } else { |
| 644 | // No second conversion required. |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 645 | SCS.Second = ICK_Identity; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 648 | QualType CanonFrom; |
| 649 | QualType CanonTo; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 650 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 651 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 652 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 653 | FromType = ToType; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 654 | CanonFrom = Context.getCanonicalType(FromType); |
| 655 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 656 | } else { |
| 657 | // No conversion required |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 658 | SCS.Third = ICK_Identity; |
| 659 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | // C++ [over.best.ics]p6: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 661 | // [...] Any difference in top-level cv-qualification is |
| 662 | // subsumed by the initialization itself and does not constitute |
| 663 | // a conversion. [...] |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 664 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 666 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 667 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 668 | FromType = ToType; |
| 669 | CanonFrom = CanonTo; |
| 670 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | // If we have not converted the argument type to the parameter type, |
| 674 | // this is a bad conversion sequence. |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 675 | if (CanonFrom != CanonTo) |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 676 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 678 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 679 | return true; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /// IsIntegralPromotion - Determines whether the conversion from the |
| 683 | /// expression From (whose potentially-adjusted type is FromType) to |
| 684 | /// ToType is an integral promotion (C++ 4.5). If so, returns true and |
| 685 | /// sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 687 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | ee54797 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 688 | // All integers are built-in. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 689 | if (!To) { |
| 690 | return false; |
| 691 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 692 | |
| 693 | // An rvalue of type char, signed char, unsigned char, short int, or |
| 694 | // unsigned short int can be converted to an rvalue of type int if |
| 695 | // int can represent all the values of the source type; otherwise, |
| 696 | // the source rvalue can be converted to an rvalue of type unsigned |
| 697 | // int (C++ 4.5p1). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 698 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 699 | if (// We can promote any signed, promotable integer type to an int |
| 700 | (FromType->isSignedIntegerType() || |
| 701 | // We can promote any unsigned integer type whose size is |
| 702 | // less than int to an int. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 704 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 705 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 708 | return To->getKind() == BuiltinType::UInt; |
| 709 | } |
| 710 | |
| 711 | // An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) |
| 712 | // can be converted to an rvalue of the first of the following types |
| 713 | // that can represent all the values of its underlying type: int, |
| 714 | // unsigned int, long, or unsigned long (C++ 4.5p2). |
| 715 | if ((FromType->isEnumeralType() || FromType->isWideCharType()) |
| 716 | && ToType->isIntegerType()) { |
| 717 | // Determine whether the type we're converting from is signed or |
| 718 | // unsigned. |
| 719 | bool FromIsSigned; |
| 720 | uint64_t FromSize = Context.getTypeSize(FromType); |
| 721 | if (const EnumType *FromEnumType = FromType->getAsEnumType()) { |
| 722 | QualType UnderlyingType = FromEnumType->getDecl()->getIntegerType(); |
| 723 | FromIsSigned = UnderlyingType->isSignedIntegerType(); |
| 724 | } else { |
| 725 | // FIXME: Is wchar_t signed or unsigned? We assume it's signed for now. |
| 726 | FromIsSigned = true; |
| 727 | } |
| 728 | |
| 729 | // The types we'll try to promote to, in the appropriate |
| 730 | // order. Try each of these types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | QualType PromoteTypes[6] = { |
| 732 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 733 | Context.LongTy, Context.UnsignedLongTy , |
| 734 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 735 | }; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 736 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 737 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 738 | if (FromSize < ToSize || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | (FromSize == ToSize && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 740 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) { |
| 741 | // We found the type that we can promote to. If this is the |
| 742 | // type we wanted, we have a promotion. Otherwise, no |
| 743 | // promotion. |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 744 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 745 | == Context.getCanonicalType(PromoteTypes[Idx]).getUnqualifiedType(); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // An rvalue for an integral bit-field (9.6) can be converted to an |
| 751 | // rvalue of type int if int can represent all the values of the |
| 752 | // bit-field; otherwise, it can be converted to unsigned int if |
| 753 | // unsigned int can represent all the values of the bit-field. If |
| 754 | // the bit-field is larger yet, no integral promotion applies to |
| 755 | // it. If the bit-field has an enumerated type, it is treated as any |
| 756 | // other value of that type for promotion purposes (C++ 4.5p3). |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 757 | // FIXME: We should delay checking of bit-fields until we actually perform the |
| 758 | // conversion. |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 759 | using llvm::APSInt; |
| 760 | if (From) |
| 761 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 762 | APSInt BitWidth; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 763 | if (FromType->isIntegralType() && !FromType->isEnumeralType() && |
| 764 | MemberDecl->getBitWidth()->isIntegerConstantExpr(BitWidth, Context)) { |
| 765 | APSInt ToSize(BitWidth.getBitWidth(), BitWidth.isUnsigned()); |
| 766 | ToSize = Context.getTypeSize(ToType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 768 | // Are we promoting to an int from a bitfield that fits in an int? |
| 769 | if (BitWidth < ToSize || |
| 770 | (FromType->isSignedIntegerType() && BitWidth <= ToSize)) { |
| 771 | return To->getKind() == BuiltinType::Int; |
| 772 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 774 | // Are we promoting to an unsigned int from an unsigned bitfield |
| 775 | // that fits into an unsigned int? |
| 776 | if (FromType->isUnsignedIntegerType() && BitWidth <= ToSize) { |
| 777 | return To->getKind() == BuiltinType::UInt; |
| 778 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 780 | return false; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 781 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 782 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 784 | // An rvalue of type bool can be converted to an rvalue of type int, |
| 785 | // with false becoming zero and true becoming one (C++ 4.5p4). |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 786 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 787 | return true; |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 788 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 789 | |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | /// IsFloatingPointPromotion - Determines whether the conversion from |
| 794 | /// FromType to ToType is a floating point promotion (C++ 4.6). If so, |
| 795 | /// returns true and sets PromotedType to the promoted type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 797 | /// An rvalue of type float can be converted to an rvalue of type |
| 798 | /// double. (C++ 4.6p1). |
| 799 | if (const BuiltinType *FromBuiltin = FromType->getAsBuiltinType()) |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 800 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 801 | if (FromBuiltin->getKind() == BuiltinType::Float && |
| 802 | ToBuiltin->getKind() == BuiltinType::Double) |
| 803 | return true; |
| 804 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 805 | // C99 6.3.1.5p1: |
| 806 | // When a float is promoted to double or long double, or a |
| 807 | // double is promoted to long double [...]. |
| 808 | if (!getLangOptions().CPlusPlus && |
| 809 | (FromBuiltin->getKind() == BuiltinType::Float || |
| 810 | FromBuiltin->getKind() == BuiltinType::Double) && |
| 811 | (ToBuiltin->getKind() == BuiltinType::LongDouble)) |
| 812 | return true; |
| 813 | } |
| 814 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 815 | return false; |
| 816 | } |
| 817 | |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 818 | /// \brief Determine if a conversion is a complex promotion. |
| 819 | /// |
| 820 | /// A complex promotion is defined as a complex -> complex conversion |
| 821 | /// where the conversion between the underlying real types is a |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 822 | /// floating-point or integral promotion. |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 823 | bool Sema::IsComplexPromotion(QualType FromType, QualType ToType) { |
| 824 | const ComplexType *FromComplex = FromType->getAsComplexType(); |
| 825 | if (!FromComplex) |
| 826 | return false; |
| 827 | |
| 828 | const ComplexType *ToComplex = ToType->getAsComplexType(); |
| 829 | if (!ToComplex) |
| 830 | return false; |
| 831 | |
| 832 | return IsFloatingPointPromotion(FromComplex->getElementType(), |
Douglas Gregor | 6752502 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 833 | ToComplex->getElementType()) || |
| 834 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 835 | ToComplex->getElementType()); |
Douglas Gregor | 78ca74d | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 838 | /// BuildSimilarlyQualifiedPointerType - In a pointer conversion from |
| 839 | /// the pointer type FromPtr to a pointer to type ToPointee, with the |
| 840 | /// same type qualifiers as FromPtr has on its pointee type. ToType, |
| 841 | /// if non-empty, will be a pointer to ToType that may or may not have |
| 842 | /// the right set of qualifiers on its pointee. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | static QualType |
| 844 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 845 | QualType ToPointee, QualType ToType, |
| 846 | ASTContext &Context) { |
| 847 | QualType CanonFromPointee = Context.getCanonicalType(FromPtr->getPointeeType()); |
| 848 | QualType CanonToPointee = Context.getCanonicalType(ToPointee); |
| 849 | unsigned Quals = CanonFromPointee.getCVRQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
| 851 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 852 | if (CanonToPointee.getCVRQualifiers() == Quals) { |
| 853 | // ToType is exactly what we need. Return it. |
| 854 | if (ToType.getTypePtr()) |
| 855 | return ToType; |
| 856 | |
| 857 | // Build a pointer to ToPointee. It has the right qualifiers |
| 858 | // already. |
| 859 | return Context.getPointerType(ToPointee); |
| 860 | } |
| 861 | |
| 862 | // Just build a canonical type that has the right qualifiers. |
| 863 | return Context.getPointerType(CanonToPointee.getQualifiedType(Quals)); |
| 864 | } |
| 865 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 867 | bool InOverloadResolution, |
| 868 | ASTContext &Context) { |
| 869 | // Handle value-dependent integral null pointer constants correctly. |
| 870 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#903 |
| 871 | if (Expr->isValueDependent() && !Expr->isTypeDependent() && |
| 872 | Expr->getType()->isIntegralType()) |
| 873 | return !InOverloadResolution; |
| 874 | |
| 875 | return Expr->isNullPointerConstant(Context); |
| 876 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 878 | /// IsPointerConversion - Determines whether the conversion of the |
| 879 | /// expression From, which has the (possibly adjusted) type FromType, |
| 880 | /// can be converted to the type ToType via a pointer conversion (C++ |
| 881 | /// 4.10). If so, returns true and places the converted type (that |
| 882 | /// might differ from ToType in its cv-qualifiers at some level) into |
| 883 | /// ConvertedType. |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 884 | /// |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 885 | /// This routine also supports conversions to and from block pointers |
| 886 | /// and conversions with Objective-C's 'id', 'id<protocols...>', and |
| 887 | /// pointers to interfaces. FIXME: Once we've determined the |
| 888 | /// appropriate overloading rules for Objective-C, we may want to |
| 889 | /// split the Objective-C checks into a different routine; however, |
| 890 | /// GCC seems to consider all of these conversions to be pointer |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 891 | /// conversions, so for now they live here. IncompatibleObjC will be |
| 892 | /// set if the conversion is an allowed Objective-C conversion that |
| 893 | /// should result in a warning. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 894 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 895 | bool InOverloadResolution, |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 896 | QualType& ConvertedType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | bool &IncompatibleObjC) { |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 898 | IncompatibleObjC = false; |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 899 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 900 | return true; |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 901 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | // Conversion from a null pointer constant to any Objective-C pointer type. |
| 903 | if (ToType->isObjCObjectPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 904 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 79a6b01 | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 905 | ConvertedType = ToType; |
| 906 | return true; |
| 907 | } |
| 908 | |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 909 | // Blocks: Block pointers can be converted to void*. |
| 910 | if (FromType->isBlockPointerType() && ToType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 911 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 912 | ConvertedType = ToType; |
| 913 | return true; |
| 914 | } |
| 915 | // Blocks: A null pointer constant can be converted to a block |
| 916 | // pointer type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | if (ToType->isBlockPointerType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 918 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 231d1c6 | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 919 | ConvertedType = ToType; |
| 920 | return true; |
| 921 | } |
| 922 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 923 | // If the left-hand-side is nullptr_t, the right side can be a null |
| 924 | // pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | if (ToType->isNullPtrType() && |
Anders Carlsson | 759b789 | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 926 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 927 | ConvertedType = ToType; |
| 928 | return true; |
| 929 | } |
| 930 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 931 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 932 | if (!ToTypePtr) |
| 933 | return false; |
| 934 | |
| 935 | // 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] | 936 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 937 | ConvertedType = ToType; |
| 938 | return true; |
| 939 | } |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 940 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 941 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 942 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 943 | if (!FromTypePtr) |
| 944 | return false; |
| 945 | |
| 946 | QualType FromPointeeType = FromTypePtr->getPointeeType(); |
| 947 | QualType ToPointeeType = ToTypePtr->getPointeeType(); |
| 948 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 949 | // An rvalue of type "pointer to cv T," where T is an object type, |
| 950 | // can be converted to an rvalue of type "pointer to cv void" (C++ |
| 951 | // 4.10p2). |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 952 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 954 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 955 | ToType, Context); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 956 | return true; |
| 957 | } |
| 958 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 959 | // When we're overloading in C, we allow a special kind of pointer |
| 960 | // conversion for compatible-but-not-identical pointee types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 962 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 964 | ToPointeeType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | ToType, Context); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 969 | // C++ [conv.ptr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | // |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 971 | // An rvalue of type "pointer to cv D," where D is a class type, |
| 972 | // can be converted to an rvalue of type "pointer to cv B," where |
| 973 | // B is a base class (clause 10) of D. If B is an inaccessible |
| 974 | // (clause 11) or ambiguous (10.2) base class of D, a program that |
| 975 | // necessitates this conversion is ill-formed. The result of the |
| 976 | // conversion is a pointer to the base class sub-object of the |
| 977 | // derived class object. The null pointer value is converted to |
| 978 | // the null pointer value of the destination type. |
| 979 | // |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 980 | // Note that we do not check for ambiguity or inaccessibility |
| 981 | // here. That is handled by CheckPointerConversion. |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 982 | if (getLangOptions().CPlusPlus && |
| 983 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 984 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bb9bf88 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 986 | ToPointeeType, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 987 | ToType, Context); |
| 988 | return true; |
| 989 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 991 | return false; |
| 992 | } |
| 993 | |
| 994 | /// isObjCPointerConversion - Determines whether this is an |
| 995 | /// Objective-C pointer conversion. Subroutine of IsPointerConversion, |
| 996 | /// with the same arguments and return values. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 998 | QualType& ConvertedType, |
| 999 | bool &IncompatibleObjC) { |
| 1000 | if (!getLangOptions().ObjC1) |
| 1001 | return false; |
| 1002 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1003 | // First, we handle all conversions on ObjC object pointer types. |
| 1004 | const ObjCObjectPointerType* ToObjCPtr = ToType->getAsObjCObjectPointerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | const ObjCObjectPointerType *FromObjCPtr = |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1006 | FromType->getAsObjCObjectPointerType(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1007 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1008 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1009 | // 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] | 1010 | // pointer to any interface (in both directions). |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1011 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1012 | ConvertedType = ToType; |
| 1013 | return true; |
| 1014 | } |
| 1015 | // Conversions with Objective-C's id<...>. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1017 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1019 | /*compare=*/false)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1020 | ConvertedType = ToType; |
| 1021 | return true; |
| 1022 | } |
| 1023 | // Objective C++: We're able to convert from a pointer to an |
| 1024 | // interface to a pointer to a different interface. |
| 1025 | if (Context.canAssignObjCInterfaces(ToObjCPtr, FromObjCPtr)) { |
| 1026 | ConvertedType = ToType; |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | if (Context.canAssignObjCInterfaces(FromObjCPtr, ToObjCPtr)) { |
| 1031 | // Okay: this is some kind of implicit downcast of Objective-C |
| 1032 | // interfaces, which is permitted. However, we're going to |
| 1033 | // complain about it. |
| 1034 | IncompatibleObjC = true; |
| 1035 | ConvertedType = FromType; |
| 1036 | return true; |
| 1037 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1039 | // 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] | 1040 | QualType ToPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1041 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1042 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1043 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1044 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1045 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1048 | QualType FromPointeeType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1049 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1050 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1051 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1052 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1053 | else |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1056 | // If we have pointers to pointers, recursively check whether this |
| 1057 | // is an Objective-C conversion. |
| 1058 | if (FromPointeeType->isPointerType() && ToPointeeType->isPointerType() && |
| 1059 | isObjCPointerConversion(FromPointeeType, ToPointeeType, ConvertedType, |
| 1060 | IncompatibleObjC)) { |
| 1061 | // We always complain about this conversion. |
| 1062 | IncompatibleObjC = true; |
| 1063 | ConvertedType = ToType; |
| 1064 | return true; |
| 1065 | } |
Douglas Gregor | 033f56d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1066 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1067 | // differences in the argument and result types are in Objective-C |
| 1068 | // pointer conversions. If so, we permit the conversion (but |
| 1069 | // complain about it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | const FunctionProtoType *FromFunctionType |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1071 | = FromPointeeType->getAsFunctionProtoType(); |
| 1072 | const FunctionProtoType *ToFunctionType |
| 1073 | = ToPointeeType->getAsFunctionProtoType(); |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1074 | if (FromFunctionType && ToFunctionType) { |
| 1075 | // If the function types are exactly the same, this isn't an |
| 1076 | // Objective-C pointer conversion. |
| 1077 | if (Context.getCanonicalType(FromPointeeType) |
| 1078 | == Context.getCanonicalType(ToPointeeType)) |
| 1079 | return false; |
| 1080 | |
| 1081 | // Perform the quick checks that will tell us whether these |
| 1082 | // function types are obviously different. |
| 1083 | if (FromFunctionType->getNumArgs() != ToFunctionType->getNumArgs() || |
| 1084 | FromFunctionType->isVariadic() != ToFunctionType->isVariadic() || |
| 1085 | FromFunctionType->getTypeQuals() != ToFunctionType->getTypeQuals()) |
| 1086 | return false; |
| 1087 | |
| 1088 | bool HasObjCConversion = false; |
| 1089 | if (Context.getCanonicalType(FromFunctionType->getResultType()) |
| 1090 | == Context.getCanonicalType(ToFunctionType->getResultType())) { |
| 1091 | // Okay, the types match exactly. Nothing to do. |
| 1092 | } else if (isObjCPointerConversion(FromFunctionType->getResultType(), |
| 1093 | ToFunctionType->getResultType(), |
| 1094 | ConvertedType, IncompatibleObjC)) { |
| 1095 | // Okay, we have an Objective-C pointer conversion. |
| 1096 | HasObjCConversion = true; |
| 1097 | } else { |
| 1098 | // Function types are too different. Abort. |
| 1099 | return false; |
| 1100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | a119f10 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1102 | // Check argument types. |
| 1103 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 1104 | ArgIdx != NumArgs; ++ArgIdx) { |
| 1105 | QualType FromArgType = FromFunctionType->getArgType(ArgIdx); |
| 1106 | QualType ToArgType = ToFunctionType->getArgType(ArgIdx); |
| 1107 | if (Context.getCanonicalType(FromArgType) |
| 1108 | == Context.getCanonicalType(ToArgType)) { |
| 1109 | // Okay, the types match exactly. Nothing to do. |
| 1110 | } else if (isObjCPointerConversion(FromArgType, ToArgType, |
| 1111 | ConvertedType, IncompatibleObjC)) { |
| 1112 | // Okay, we have an Objective-C pointer conversion. |
| 1113 | HasObjCConversion = true; |
| 1114 | } else { |
| 1115 | // Argument types are too different. Abort. |
| 1116 | return false; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | if (HasObjCConversion) { |
| 1121 | // We had an Objective-C conversion. Allow this pointer |
| 1122 | // conversion, but complain about it. |
| 1123 | ConvertedType = ToType; |
| 1124 | IncompatibleObjC = true; |
| 1125 | return true; |
| 1126 | } |
| 1127 | } |
| 1128 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1129 | return false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1132 | /// CheckPointerConversion - Check the pointer conversion from the |
| 1133 | /// expression From to the type ToType. This routine checks for |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1134 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1135 | /// conversions for which IsPointerConversion has already returned |
| 1136 | /// true. It returns true and produces a diagnostic if there was an |
| 1137 | /// error, or returns false otherwise. |
| 1138 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType) { |
| 1139 | QualType FromType = From->getType(); |
| 1140 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1141 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1142 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1143 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1144 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | 1e57a3f | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1146 | if (FromPointeeType->isRecordType() && |
| 1147 | ToPointeeType->isRecordType()) { |
| 1148 | // We must have a derived-to-base conversion. Check an |
| 1149 | // ambiguous or inaccessible conversion. |
Douglas Gregor | cea4e743 | 2008-10-24 16:17:19 +0000 | [diff] [blame] | 1150 | return CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1151 | From->getExprLoc(), |
| 1152 | From->getSourceRange()); |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | if (const ObjCObjectPointerType *FromPtrType = |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1156 | FromType->getAsObjCObjectPointerType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | if (const ObjCObjectPointerType *ToPtrType = |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1158 | ToType->getAsObjCObjectPointerType()) { |
| 1159 | // Objective-C++ conversions are always okay. |
| 1160 | // FIXME: We should have a different class of conversions for the |
| 1161 | // Objective-C++ implicit conversions. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1162 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1163 | return false; |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1164 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1165 | } |
Douglas Gregor | 39c16d4 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1166 | return false; |
| 1167 | } |
| 1168 | |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1169 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1170 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1171 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1172 | /// If so, returns true and places the converted type (that might differ from |
| 1173 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1174 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | QualType ToType, QualType &ConvertedType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1176 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1177 | if (!ToTypePtr) |
| 1178 | return false; |
| 1179 | |
| 1180 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
| 1181 | if (From->isNullPointerConstant(Context)) { |
| 1182 | ConvertedType = ToType; |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
| 1186 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1187 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1188 | if (!FromTypePtr) |
| 1189 | return false; |
| 1190 | |
| 1191 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1192 | // where D is derived from B (C++ 4.11p2). |
| 1193 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1194 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1195 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1196 | |
| 1197 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1198 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1199 | ToClass.getTypePtr()); |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1207 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1208 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1209 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1210 | /// true and produces a diagnostic if there was an error, or returns false |
| 1211 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1213 | CastExpr::CastKind &Kind) { |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1214 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1215 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1216 | if (!FromPtrType) { |
| 1217 | // This must be a null pointer to member pointer conversion |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | assert(From->isNullPointerConstant(Context) && |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1219 | "Expr must be null pointer constant!"); |
| 1220 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1221 | return false; |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1222 | } |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1223 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1224 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1225 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1226 | "that is not a member pointer."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1227 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1228 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1229 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1230 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1231 | // FIXME: What about dependent types? |
| 1232 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1233 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1234 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1235 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1236 | /*DetectVirtual=*/true); |
| 1237 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1238 | assert(DerivationOkay && |
| 1239 | "Should not have been called if derivation isn't OK."); |
| 1240 | (void)DerivationOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1241 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1242 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1243 | getUnqualifiedType())) { |
| 1244 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1245 | Paths.clear(); |
| 1246 | Paths.setRecordingPaths(true); |
| 1247 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1248 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1249 | (void)StillOkay; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1250 | |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1251 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1252 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1253 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1254 | return true; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1255 | } |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1257 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1258 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1259 | << FromClass << ToClass << QualType(VBase, 0) |
| 1260 | << From->getSourceRange(); |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
Anders Carlsson | d7923c6 | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1264 | // Must be a base to derived member conversion. |
| 1265 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 72b597d | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1266 | return false; |
| 1267 | } |
| 1268 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1269 | /// IsQualificationConversion - Determines whether the conversion from |
| 1270 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1271 | /// (C++ 4.4). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | bool |
| 1273 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1274 | FromType = Context.getCanonicalType(FromType); |
| 1275 | ToType = Context.getCanonicalType(ToType); |
| 1276 | |
| 1277 | // If FromType and ToType are the same type, this is not a |
| 1278 | // qualification conversion. |
| 1279 | if (FromType == ToType) |
| 1280 | return false; |
Sebastian Redl | ed8f200 | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1282 | // (C++ 4.4p4): |
| 1283 | // A conversion can add cv-qualifiers at levels other than the first |
| 1284 | // in multi-level pointers, subject to the following rules: [...] |
| 1285 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1286 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1287 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1288 | // Within each iteration of the loop, we check the qualifiers to |
| 1289 | // determine if this still looks like a qualification |
| 1290 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1291 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1292 | // until there are no more pointers or pointers-to-members left to |
| 1293 | // unwrap. |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1294 | UnwrappedAnyPointer = true; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1295 | |
| 1296 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1297 | // 2,j, and similarly for volatile. |
Douglas Gregor | ea2d421 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1298 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1299 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1301 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1302 | // every cv for 0 < k < j. |
| 1303 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1304 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1305 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1307 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1308 | // include const. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | PreviousToQualsIncludeConst |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1310 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1311 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1312 | |
| 1313 | // We are left with FromType and ToType being the pointee types |
| 1314 | // after unwrapping the original FromType and ToType the same number |
| 1315 | // of types. If we unwrapped any pointers, and if FromType and |
| 1316 | // ToType have the same unqualified type (since we checked |
| 1317 | // qualifiers above), then this is a qualification conversion. |
| 1318 | return UnwrappedAnyPointer && |
| 1319 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1320 | } |
| 1321 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1322 | /// \brief Given a function template or function, extract the function template |
| 1323 | /// declaration (if any) and the underlying function declaration. |
| 1324 | template<typename T> |
| 1325 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1326 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1327 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1328 | if (FunctionTemplate) |
| 1329 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1330 | else |
| 1331 | Function = cast<T>(Orig); |
| 1332 | } |
| 1333 | |
Fariborz Jahanian | c571f79 | 2009-09-10 22:26:16 +0000 | [diff] [blame] | 1334 | void |
| 1335 | Sema::AddAllConversionCandidate(CXXRecordDecl *ClassDecl, Expr *From, |
| 1336 | QualType ToType, bool AllowExplicit, |
| 1337 | OverloadCandidateSet &CandidateSet) { |
| 1338 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1339 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1340 | CXXRecordDecl *BaseClassDecl |
| 1341 | = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
| 1342 | AddAllConversionCandidate(BaseClassDecl, From, ToType, AllowExplicit, |
| 1343 | CandidateSet); |
| 1344 | } |
| 1345 | for (CXXRecordDecl::base_class_iterator Base = |
| 1346 | ClassDecl->bases_begin(), |
| 1347 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1348 | if (Base->isVirtual()) |
| 1349 | continue; |
| 1350 | CXXRecordDecl *BaseClassDecl |
| 1351 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1352 | AddAllConversionCandidate(BaseClassDecl, From, ToType, AllowExplicit, |
| 1353 | CandidateSet); |
| 1354 | } |
| 1355 | |
| 1356 | OverloadedFunctionDecl *Conversions |
| 1357 | = ClassDecl->getConversionFunctions(); |
| 1358 | |
| 1359 | for (OverloadedFunctionDecl::function_iterator Func |
| 1360 | = Conversions->function_begin(); |
| 1361 | Func != Conversions->function_end(); ++Func) { |
| 1362 | CXXConversionDecl *Conv; |
| 1363 | FunctionTemplateDecl *ConvTemplate; |
| 1364 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1365 | if (ConvTemplate) |
| 1366 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1367 | else |
| 1368 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1369 | |
| 1370 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1371 | if (ConvTemplate) |
| 1372 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1373 | CandidateSet); |
| 1374 | else |
| 1375 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1376 | } |
| 1377 | } |
| 1378 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1380 | /// Determines whether there is a user-defined conversion sequence |
| 1381 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1382 | /// ToType. If such a conversion exists, User will contain the |
| 1383 | /// user-defined conversion sequence that performs such a conversion |
| 1384 | /// and this routine will return true. Otherwise, this routine returns |
| 1385 | /// false and User is unspecified. |
| 1386 | /// |
| 1387 | /// \param AllowConversionFunctions true if the conversion should |
| 1388 | /// consider conversion functions at all. If false, only constructors |
| 1389 | /// will be considered. |
| 1390 | /// |
| 1391 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1392 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1393 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1394 | /// |
| 1395 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1396 | /// for overload resolution. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1398 | UserDefinedConversionSequence& User, |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1399 | bool AllowConversionFunctions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | bool AllowExplicit, bool ForceRValue) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1401 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1402 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | if (CXXRecordDecl *ToRecordDecl |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1404 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1405 | // C++ [over.match.ctor]p1: |
| 1406 | // When objects of class type are direct-initialized (8.5), or |
| 1407 | // copy-initialized from an expression of the same or a |
| 1408 | // derived class type (8.5), overload resolution selects the |
| 1409 | // constructor. [...] For copy-initialization, the candidate |
| 1410 | // functions are all the converting constructors (12.3.1) of |
| 1411 | // that class. The argument list is the expression-list within |
| 1412 | // the parentheses of the initializer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | DeclarationName ConstructorName |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1414 | = Context.DeclarationNames.getCXXConstructorName( |
| 1415 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1416 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1418 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1419 | Con != ConEnd; ++Con) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1420 | // Find the constructor (which may be a template). |
| 1421 | CXXConstructorDecl *Constructor = 0; |
| 1422 | FunctionTemplateDecl *ConstructorTmpl |
| 1423 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1424 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | Constructor |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1426 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1427 | else |
| 1428 | Constructor = cast<CXXConstructorDecl>(*Con); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Fariborz Jahanian | 11a8e95 | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1430 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1431 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1432 | if (ConstructorTmpl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1434 | 1, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | /*SuppressUserConversions=*/true, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1436 | ForceRValue); |
| 1437 | else |
| 1438 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 1439 | /*SuppressUserConversions=*/true, ForceRValue); |
| 1440 | } |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1441 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1445 | if (!AllowConversionFunctions) { |
| 1446 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1448 | PDiag(0) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1449 | << From->getSourceRange())) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1450 | // No conversion functions from incomplete types. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | } else if (const RecordType *FromRecordType |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1452 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | c571f79 | 2009-09-10 22:26:16 +0000 | [diff] [blame] | 1454 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) |
| 1455 | // Add all of the conversion functions as candidates. |
| 1456 | AddAllConversionCandidate(FromRecordDecl, From, ToType, AllowExplicit, |
| 1457 | CandidateSet); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1458 | } |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1459 | |
| 1460 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1461 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1462 | case OR_Success: |
| 1463 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1465 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1466 | // C++ [over.ics.user]p1: |
| 1467 | // If the user-defined conversion is specified by a |
| 1468 | // constructor (12.3.1), the initial standard conversion |
| 1469 | // sequence converts the source type to the type required by |
| 1470 | // the argument of the constructor. |
| 1471 | // |
| 1472 | // FIXME: What about ellipsis conversions? |
| 1473 | QualType ThisType = Constructor->getThisType(Context); |
| 1474 | User.Before = Best->Conversions[0].Standard; |
| 1475 | User.ConversionFunction = Constructor; |
| 1476 | User.After.setAsIdentityConversion(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | User.After.FromTypePtr |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1478 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1479 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1480 | return true; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1481 | } else if (CXXConversionDecl *Conversion |
| 1482 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1483 | // C++ [over.ics.user]p1: |
| 1484 | // |
| 1485 | // [...] If the user-defined conversion is specified by a |
| 1486 | // conversion function (12.3.2), the initial standard |
| 1487 | // conversion sequence converts the source type to the |
| 1488 | // implicit object parameter of the conversion function. |
| 1489 | User.Before = Best->Conversions[0].Standard; |
| 1490 | User.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
| 1492 | // C++ [over.ics.user]p2: |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1493 | // The second standard conversion sequence converts the |
| 1494 | // result of the user-defined conversion to the target type |
| 1495 | // for the sequence. Since an implicit conversion sequence |
| 1496 | // is an initialization, the special rules for |
| 1497 | // initialization by user-defined conversion apply when |
| 1498 | // selecting the best user-defined conversion for a |
| 1499 | // user-defined conversion sequence (see 13.3.3 and |
| 1500 | // 13.3.3.1). |
| 1501 | User.After = Best->FinalConversion; |
| 1502 | return true; |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1503 | } else { |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1504 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1505 | return false; |
| 1506 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1508 | case OR_No_Viable_Function: |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1509 | case OR_Deleted: |
Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1510 | // No conversion here! We're done. |
| 1511 | return false; |
| 1512 | |
| 1513 | case OR_Ambiguous: |
| 1514 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 1515 | // ambiguous conversion sequences. |
| 1516 | return false; |
| 1517 | } |
| 1518 | |
| 1519 | return false; |
| 1520 | } |
| 1521 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1522 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1523 | /// conversion sequences to determine whether one is better than the |
| 1524 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1526 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1527 | const ImplicitConversionSequence& ICS2) |
| 1528 | { |
| 1529 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1530 | // conversion sequences (as defined in 13.3.3.1) |
| 1531 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1532 | // conversion sequence than a user-defined conversion sequence or |
| 1533 | // an ellipsis conversion sequence, and |
| 1534 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1535 | // conversion sequence than an ellipsis conversion sequence |
| 1536 | // (13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | // |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1538 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1539 | return ImplicitConversionSequence::Better; |
| 1540 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1541 | return ImplicitConversionSequence::Worse; |
| 1542 | |
| 1543 | // Two implicit conversion sequences of the same form are |
| 1544 | // indistinguishable conversion sequences unless one of the |
| 1545 | // following rules apply: (C++ 13.3.3.2p3): |
| 1546 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1547 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | else if (ICS1.ConversionKind == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1549 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1550 | // User-defined conversion sequence U1 is a better conversion |
| 1551 | // sequence than another user-defined conversion sequence U2 if |
| 1552 | // they contain the same user-defined conversion function or |
| 1553 | // constructor and if the second standard conversion sequence of |
| 1554 | // U1 is better than the second standard conversion sequence of |
| 1555 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1557 | ICS2.UserDefined.ConversionFunction) |
| 1558 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1559 | ICS2.UserDefined.After); |
| 1560 | } |
| 1561 | |
| 1562 | return ImplicitConversionSequence::Indistinguishable; |
| 1563 | } |
| 1564 | |
| 1565 | /// CompareStandardConversionSequences - Compare two standard |
| 1566 | /// conversion sequences to determine whether one is better than the |
| 1567 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1569 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1570 | const StandardConversionSequence& SCS2) |
| 1571 | { |
| 1572 | // Standard conversion sequence S1 is a better conversion sequence |
| 1573 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1574 | |
| 1575 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1576 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1577 | // excluding any Lvalue Transformation; the identity conversion |
| 1578 | // sequence is considered to be a subsequence of any |
| 1579 | // non-identity conversion sequence) or, if not that, |
| 1580 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1581 | // Neither is a proper subsequence of the other. Do nothing. |
| 1582 | ; |
| 1583 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1584 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1586 | SCS1.Third == ICK_Identity)) |
| 1587 | // SCS1 is a proper subsequence of SCS2. |
| 1588 | return ImplicitConversionSequence::Better; |
| 1589 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1590 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1592 | SCS2.Third == ICK_Identity)) |
| 1593 | // SCS2 is a proper subsequence of SCS1. |
| 1594 | return ImplicitConversionSequence::Worse; |
| 1595 | |
| 1596 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1597 | // defined below), or, if not that, |
| 1598 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1599 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1600 | if (Rank1 < Rank2) |
| 1601 | return ImplicitConversionSequence::Better; |
| 1602 | else if (Rank2 < Rank1) |
| 1603 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1605 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1606 | // are indistinguishable unless one of the following rules |
| 1607 | // applies: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1609 | // A conversion that is not a conversion of a pointer, or |
| 1610 | // pointer to member, to bool is better than another conversion |
| 1611 | // that is such a conversion. |
| 1612 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1613 | return SCS2.isPointerConversionToBool() |
| 1614 | ? ImplicitConversionSequence::Better |
| 1615 | : ImplicitConversionSequence::Worse; |
| 1616 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1617 | // C++ [over.ics.rank]p4b2: |
| 1618 | // |
| 1619 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1620 | // conversion of B* to A* is better than conversion of B* to |
| 1621 | // void*, and conversion of A* to void* is better than conversion |
| 1622 | // of B* to void*. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | bool SCS1ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1624 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | bool SCS2ConvertsToVoid |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1626 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1627 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1628 | // Exactly one of the conversion sequences is a conversion to |
| 1629 | // a void pointer; it's the worse conversion. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1630 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1631 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1632 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1633 | // Neither conversion sequence converts to a void pointer; compare |
| 1634 | // their derived-to-base conversions. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1635 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1636 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1637 | return DerivedCK; |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1638 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1639 | // Both conversion sequences are conversions to void |
| 1640 | // pointers. Compare the source types to determine if there's an |
| 1641 | // inheritance relationship in their sources. |
| 1642 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1643 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1644 | |
| 1645 | // Adjust the types we're converting from via the array-to-pointer |
| 1646 | // conversion, if we need to. |
| 1647 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1648 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1649 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1650 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1651 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1653 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1654 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1655 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1656 | |
| 1657 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1658 | return ImplicitConversionSequence::Better; |
| 1659 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1660 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Objective-C++: If one interface is more specific than the |
| 1663 | // other, it is the better one. |
| 1664 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1665 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1666 | if (FromIface1 && FromIface1) { |
| 1667 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1668 | return ImplicitConversionSequence::Better; |
| 1669 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1670 | return ImplicitConversionSequence::Worse; |
| 1671 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1672 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1673 | |
| 1674 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1675 | // bullet 3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1677 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1678 | return QualCK; |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1679 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1680 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1681 | // C++0x [over.ics.rank]p3b4: |
| 1682 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1683 | // implicit object parameter of a non-static member function declared |
| 1684 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1685 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1686 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1687 | // or if the member function in this case has a ref qualifier. |
| 1688 | // (Of course, we don't have ref qualifiers yet.) |
| 1689 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1690 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1691 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1692 | |
| 1693 | // C++ [over.ics.rank]p3b4: |
| 1694 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1695 | // which the references refer are the same type except for |
| 1696 | // top-level cv-qualifiers, and the type to which the reference |
| 1697 | // initialized by S2 refers is more cv-qualified than the type |
| 1698 | // to which the reference initialized by S1 refers. |
Sebastian Redl | 4c0cd85 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1699 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1700 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1701 | T1 = Context.getCanonicalType(T1); |
| 1702 | T2 = Context.getCanonicalType(T2); |
| 1703 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1704 | if (T2.isMoreQualifiedThan(T1)) |
| 1705 | return ImplicitConversionSequence::Better; |
| 1706 | else if (T1.isMoreQualifiedThan(T2)) |
| 1707 | return ImplicitConversionSequence::Worse; |
| 1708 | } |
| 1709 | } |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1710 | |
| 1711 | return ImplicitConversionSequence::Indistinguishable; |
| 1712 | } |
| 1713 | |
| 1714 | /// CompareQualificationConversions - Compares two standard conversion |
| 1715 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1717 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1718 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1719 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | 4b62ec6 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1720 | // C++ 13.3.3.2p3: |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1721 | // -- S1 and S2 differ only in their qualification conversion and |
| 1722 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1723 | // cv-qualification signature of type T1 is a proper subset of |
| 1724 | // the cv-qualification signature of type T2, and S1 is not the |
| 1725 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1726 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1727 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1728 | return ImplicitConversionSequence::Indistinguishable; |
| 1729 | |
| 1730 | // FIXME: the example in the standard doesn't use a qualification |
| 1731 | // conversion (!) |
| 1732 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1733 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1734 | T1 = Context.getCanonicalType(T1); |
| 1735 | T2 = Context.getCanonicalType(T2); |
| 1736 | |
| 1737 | // If the types are the same, we won't learn anything by unwrapped |
| 1738 | // them. |
| 1739 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1740 | return ImplicitConversionSequence::Indistinguishable; |
| 1741 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1743 | = ImplicitConversionSequence::Indistinguishable; |
| 1744 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1745 | // Within each iteration of the loop, we check the qualifiers to |
| 1746 | // determine if this still looks like a qualification |
| 1747 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1748 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1749 | // until there are no more pointers or pointers-to-members left |
| 1750 | // to unwrap. This essentially mimics what |
| 1751 | // IsQualificationConversion does, but here we're checking for a |
| 1752 | // strict subset of qualifiers. |
| 1753 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1754 | // The qualifiers are the same, so this doesn't tell us anything |
| 1755 | // about how the sequences rank. |
| 1756 | ; |
| 1757 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1758 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1759 | if (Result == ImplicitConversionSequence::Worse) |
| 1760 | // Neither has qualifiers that are a subset of the other's |
| 1761 | // qualifiers. |
| 1762 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1764 | Result = ImplicitConversionSequence::Better; |
| 1765 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1766 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1767 | if (Result == ImplicitConversionSequence::Better) |
| 1768 | // Neither has qualifiers that are a subset of the other's |
| 1769 | // qualifiers. |
| 1770 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1772 | Result = ImplicitConversionSequence::Worse; |
| 1773 | } else { |
| 1774 | // Qualifiers are disjoint. |
| 1775 | return ImplicitConversionSequence::Indistinguishable; |
| 1776 | } |
| 1777 | |
| 1778 | // If the types after this point are equivalent, we're done. |
| 1779 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1780 | break; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Douglas Gregor | e1eb9d8 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1783 | // Check that the winning standard conversion sequence isn't using |
| 1784 | // the deprecated string literal array to pointer conversion. |
| 1785 | switch (Result) { |
| 1786 | case ImplicitConversionSequence::Better: |
| 1787 | if (SCS1.Deprecated) |
| 1788 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1789 | break; |
| 1790 | |
| 1791 | case ImplicitConversionSequence::Indistinguishable: |
| 1792 | break; |
| 1793 | |
| 1794 | case ImplicitConversionSequence::Worse: |
| 1795 | if (SCS2.Deprecated) |
| 1796 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1797 | break; |
| 1798 | } |
| 1799 | |
| 1800 | return Result; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1803 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1804 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1805 | /// various kinds of derived-to-base conversions (C++ |
| 1806 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1807 | /// conversions between Objective-C interface types. |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1808 | ImplicitConversionSequence::CompareKind |
| 1809 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1810 | const StandardConversionSequence& SCS2) { |
| 1811 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1812 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1813 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1814 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1815 | |
| 1816 | // Adjust the types we're converting from via the array-to-pointer |
| 1817 | // conversion, if we need to. |
| 1818 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1819 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1820 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1821 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1822 | |
| 1823 | // Canonicalize all of the types. |
| 1824 | FromType1 = Context.getCanonicalType(FromType1); |
| 1825 | ToType1 = Context.getCanonicalType(ToType1); |
| 1826 | FromType2 = Context.getCanonicalType(FromType2); |
| 1827 | ToType2 = Context.getCanonicalType(ToType2); |
| 1828 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1829 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1830 | // |
| 1831 | // If class B is derived directly or indirectly from class A and |
| 1832 | // class C is derived directly or indirectly from B, |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1833 | // |
| 1834 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1835 | // interfaces. |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1836 | |
| 1837 | // Compare based on pointer conversions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | a29dc05 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1839 | SCS2.Second == ICK_Pointer_Conversion && |
| 1840 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1841 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1842 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | QualType FromPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1844 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | QualType ToPointee1 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1846 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1847 | QualType FromPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1848 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1849 | QualType ToPointee2 |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1850 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1851 | |
| 1852 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1853 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1854 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType(); |
| 1855 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType(); |
| 1856 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1857 | // -- 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] | 1858 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1859 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1860 | return ImplicitConversionSequence::Better; |
| 1861 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1862 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1863 | |
| 1864 | if (ToIface1 && ToIface2) { |
| 1865 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1866 | return ImplicitConversionSequence::Better; |
| 1867 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1868 | return ImplicitConversionSequence::Worse; |
| 1869 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1870 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1871 | |
| 1872 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1873 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1874 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1875 | return ImplicitConversionSequence::Better; |
| 1876 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1877 | return ImplicitConversionSequence::Worse; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Douglas Gregor | 237f96c | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1879 | if (FromIface1 && FromIface2) { |
| 1880 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1881 | return ImplicitConversionSequence::Better; |
| 1882 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1883 | return ImplicitConversionSequence::Worse; |
| 1884 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1885 | } |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1888 | // Compare based on reference bindings. |
| 1889 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1890 | SCS1.Second == ICK_Derived_To_Base) { |
| 1891 | // -- binding of an expression of type C to a reference of type |
| 1892 | // B& is better than binding an expression of type C to a |
| 1893 | // reference of type A&, |
| 1894 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1895 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1896 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1897 | return ImplicitConversionSequence::Better; |
| 1898 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1899 | return ImplicitConversionSequence::Worse; |
| 1900 | } |
| 1901 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1902 | // -- binding of an expression of type B to a reference of type |
| 1903 | // A& is better than binding an expression of type C to a |
| 1904 | // reference of type A&, |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1905 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1906 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1907 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1908 | return ImplicitConversionSequence::Better; |
| 1909 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1910 | return ImplicitConversionSequence::Worse; |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | |
| 1915 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1916 | // A::* to C::*, |
| 1917 | |
| 1918 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1919 | // A::* to C::*, and |
| 1920 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1921 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1922 | SCS1.Second == ICK_Derived_To_Base) { |
| 1923 | // -- conversion of C to B is better than conversion of C to A, |
| 1924 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1925 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1926 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1927 | return ImplicitConversionSequence::Better; |
| 1928 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1929 | return ImplicitConversionSequence::Worse; |
| 1930 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1932 | // -- conversion of B to A is better than conversion of C to A. |
| 1933 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1934 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1935 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1936 | return ImplicitConversionSequence::Better; |
| 1937 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1938 | return ImplicitConversionSequence::Worse; |
| 1939 | } |
| 1940 | } |
Douglas Gregor | ef30a5f | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 5c407d9 | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1942 | return ImplicitConversionSequence::Indistinguishable; |
| 1943 | } |
| 1944 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1945 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1946 | /// ToType from the expression From. Return the implicit conversion |
| 1947 | /// sequence required to pass this argument, which may be a bad |
| 1948 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1949 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1950 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 1951 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | ImplicitConversionSequence |
| 1953 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1954 | bool SuppressUserConversions, bool ForceRValue, |
| 1955 | bool InOverloadResolution) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1956 | if (ToType->isReferenceType()) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1957 | ImplicitConversionSequence ICS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | CheckReferenceInit(From, ToType, |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1959 | SuppressUserConversions, |
| 1960 | /*AllowExplicit=*/false, |
| 1961 | ForceRValue, |
| 1962 | &ICS); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1963 | return ICS; |
| 1964 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1966 | SuppressUserConversions, |
| 1967 | /*AllowExplicit=*/false, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1968 | ForceRValue, |
| 1969 | InOverloadResolution); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1973 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 1974 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 1975 | /// an error, returns false if the initialization succeeded. Elidable should |
| 1976 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 1977 | /// differently in C++0x for this case. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1979 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1980 | if (!getLangOptions().CPlusPlus) { |
| 1981 | // In C, argument passing is the same as performing an assignment. |
| 1982 | QualType FromType = From->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1984 | AssignConvertType ConvTy = |
| 1985 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1986 | if (ConvTy != Compatible && |
| 1987 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 1988 | ConvTy = Compatible; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1990 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1991 | FromType, From, Flavor); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1992 | } |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1994 | if (ToType->isReferenceType()) |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1995 | return CheckReferenceInit(From, ToType, |
| 1996 | /*SuppressUserConversions=*/false, |
| 1997 | /*AllowExplicit=*/false, |
| 1998 | /*ForceRValue=*/false); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1999 | |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2000 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 2001 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2002 | return false; |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2004 | return Diag(From->getSourceRange().getBegin(), |
| 2005 | diag::err_typecheck_convert_incompatible) |
| 2006 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2009 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 2010 | /// parameter of the given member function (@c Method) from the |
| 2011 | /// expression @p From. |
| 2012 | ImplicitConversionSequence |
| 2013 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 2014 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
| 2015 | unsigned MethodQuals = Method->getTypeQualifiers(); |
| 2016 | QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals); |
| 2017 | |
| 2018 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 2019 | // to exit early. |
| 2020 | ImplicitConversionSequence ICS; |
| 2021 | ICS.Standard.setAsIdentityConversion(); |
| 2022 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2023 | |
| 2024 | // We need to have an object of class type. |
| 2025 | QualType FromType = From->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2026 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2027 | FromType = PT->getPointeeType(); |
| 2028 | |
| 2029 | assert(FromType->isRecordType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2030 | |
| 2031 | // The implicit object parmeter is has the type "reference to cv X", |
| 2032 | // where X is the class of which the function is a member |
| 2033 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2034 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2036 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2037 | // reference binding here, that allows class rvalues to bind to |
| 2038 | // non-constant references. |
| 2039 | |
| 2040 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2041 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2042 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 2043 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 2044 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 2045 | return ICS; |
| 2046 | |
| 2047 | // Check that we have either the same type or a derived type. It |
| 2048 | // affects the conversion rank. |
| 2049 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2050 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 2051 | ICS.Standard.Second = ICK_Identity; |
| 2052 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2053 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2054 | else |
| 2055 | return ICS; |
| 2056 | |
| 2057 | // Success. Mark this as a reference binding. |
| 2058 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2059 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2060 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2061 | ICS.Standard.ReferenceBinding = true; |
| 2062 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | f69a94a | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2063 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2064 | return ICS; |
| 2065 | } |
| 2066 | |
| 2067 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2068 | /// the implicit object parameter for the given Method with the given |
| 2069 | /// expression. |
| 2070 | bool |
| 2071 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2072 | QualType FromRecordType, DestType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | QualType ImplicitParamRecordType = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2074 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2076 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2077 | FromRecordType = PT->getPointeeType(); |
| 2078 | DestType = Method->getThisType(Context); |
| 2079 | } else { |
| 2080 | FromRecordType = From->getType(); |
| 2081 | DestType = ImplicitParamRecordType; |
| 2082 | } |
| 2083 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | ImplicitConversionSequence ICS |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2085 | = TryObjectArgumentInitialization(From, Method); |
| 2086 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2087 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2088 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2089 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2091 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2092 | CheckDerivedToBaseConversion(FromRecordType, |
| 2093 | ImplicitParamRecordType, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2094 | From->getSourceRange().getBegin(), |
| 2095 | From->getSourceRange())) |
| 2096 | return true; |
| 2097 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 4f4aab2 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2099 | /*isLvalue=*/true); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2100 | return false; |
| 2101 | } |
| 2102 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2103 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2104 | /// expression From to bool (C++0x [conv]p3). |
| 2105 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | ef4c721 | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2107 | // FIXME: Are these flags correct? |
| 2108 | /*SuppressUserConversions=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | /*AllowExplicit=*/true, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2110 | /*ForceRValue=*/false, |
| 2111 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2115 | /// of the expression From to bool (C++0x [conv]p3). |
| 2116 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2117 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2118 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2119 | return false; |
| 2120 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | return Diag(From->getSourceRange().getBegin(), |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2122 | diag::err_typecheck_bool_condition) |
| 2123 | << From->getType() << From->getSourceRange(); |
| 2124 | } |
| 2125 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2126 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2127 | /// candidate functions, using the given function call arguments. If |
| 2128 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2129 | /// conversions via constructors or conversion operators. |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2130 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2131 | /// hacky way to implement the overloading rules for elidable copy |
| 2132 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | void |
| 2134 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2135 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 2fe9883 | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2136 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2137 | bool SuppressUserConversions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | bool ForceRValue) { |
| 2139 | const FunctionProtoType* Proto |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2140 | = dyn_cast<FunctionProtoType>(Function->getType()->getAsFunctionType()); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2141 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2143 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2145 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2147 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2148 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2149 | // If we get here, it's because we're calling a member function |
| 2150 | // that is named without a member access expression (e.g., |
| 2151 | // "this->f") that was either written explicitly or created |
| 2152 | // implicitly. This can happen with a qualified call to a member |
| 2153 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2154 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2156 | SuppressUserConversions, ForceRValue); |
| 2157 | return; |
| 2158 | } |
| 2159 | // We treat a constructor like a non-member function, since its object |
| 2160 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2164 | // Add this candidate |
| 2165 | CandidateSet.push_back(OverloadCandidate()); |
| 2166 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2167 | Candidate.Function = Function; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2168 | Candidate.Viable = true; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2169 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2170 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2171 | |
| 2172 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2173 | |
| 2174 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2175 | // parameters is viable only if it has an ellipsis in its parameter |
| 2176 | // list (8.3.5). |
| 2177 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2178 | Candidate.Viable = false; |
| 2179 | return; |
| 2180 | } |
| 2181 | |
| 2182 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2183 | // is viable only if the (m+1)st parameter has a default argument |
| 2184 | // (8.3.6). For the purposes of overload resolution, the |
| 2185 | // parameter list is truncated on the right, so that there are |
| 2186 | // exactly m parameters. |
| 2187 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 2188 | if (NumArgs < MinRequiredArgs) { |
| 2189 | // Not enough arguments. |
| 2190 | Candidate.Viable = false; |
| 2191 | return; |
| 2192 | } |
| 2193 | |
| 2194 | // Determine the implicit conversion sequences for each of the |
| 2195 | // arguments. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2196 | Candidate.Conversions.resize(NumArgs); |
| 2197 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2198 | if (ArgIdx < NumArgsInProto) { |
| 2199 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2200 | // exist for each argument an implicit conversion sequence |
| 2201 | // (13.3.3.1) that converts that argument to the corresponding |
| 2202 | // parameter of F. |
| 2203 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | Candidate.Conversions[ArgIdx] |
| 2205 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2206 | SuppressUserConversions, ForceRValue, |
| 2207 | /*InOverloadResolution=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2209 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2210 | Candidate.Viable = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2213 | } else { |
| 2214 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2215 | // argument for which there is no corresponding parameter is |
| 2216 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2218 | = ImplicitConversionSequence::EllipsisConversion; |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2223 | /// \brief Add all of the function declarations in the given function set to |
| 2224 | /// the overload canddiate set. |
| 2225 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2226 | Expr **Args, unsigned NumArgs, |
| 2227 | OverloadCandidateSet& CandidateSet, |
| 2228 | bool SuppressUserConversions) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2230 | FEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2231 | F != FEnd; ++F) { |
| 2232 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2234 | SuppressUserConversions); |
| 2235 | else |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2236 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*F), |
| 2237 | /*FIXME: explicit args */false, 0, 0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2239 | SuppressUserConversions); |
| 2240 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2243 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2244 | /// of candidate functions, using the given function call arguments |
| 2245 | /// and the object argument (@c Object). For example, in a call |
| 2246 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2247 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2248 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2249 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2250 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2251 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | void |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2253 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2254 | Expr **Args, unsigned NumArgs, |
| 2255 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | bool SuppressUserConversions, bool ForceRValue) { |
| 2257 | const FunctionProtoType* Proto |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2258 | = dyn_cast<FunctionProtoType>(Method->getType()->getAsFunctionType()); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2259 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2260 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2261 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2262 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2263 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Add this candidate |
| 2266 | CandidateSet.push_back(OverloadCandidate()); |
| 2267 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2268 | Candidate.Function = Method; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2269 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2270 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2271 | |
| 2272 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2273 | |
| 2274 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2275 | // parameters is viable only if it has an ellipsis in its parameter |
| 2276 | // list (8.3.5). |
| 2277 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2278 | Candidate.Viable = false; |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2283 | // is viable only if the (m+1)st parameter has a default argument |
| 2284 | // (8.3.6). For the purposes of overload resolution, the |
| 2285 | // parameter list is truncated on the right, so that there are |
| 2286 | // exactly m parameters. |
| 2287 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2288 | if (NumArgs < MinRequiredArgs) { |
| 2289 | // Not enough arguments. |
| 2290 | Candidate.Viable = false; |
| 2291 | return; |
| 2292 | } |
| 2293 | |
| 2294 | Candidate.Viable = true; |
| 2295 | Candidate.Conversions.resize(NumArgs + 1); |
| 2296 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2297 | if (Method->isStatic() || !Object) |
| 2298 | // The implicit object argument is ignored. |
| 2299 | Candidate.IgnoreObjectArgument = true; |
| 2300 | else { |
| 2301 | // Determine the implicit conversion sequence for the object |
| 2302 | // parameter. |
| 2303 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2305 | == ImplicitConversionSequence::BadConversion) { |
| 2306 | Candidate.Viable = false; |
| 2307 | return; |
| 2308 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | // Determine the implicit conversion sequences for each of the |
| 2312 | // arguments. |
| 2313 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2314 | if (ArgIdx < NumArgsInProto) { |
| 2315 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2316 | // exist for each argument an implicit conversion sequence |
| 2317 | // (13.3.3.1) that converts that argument to the corresponding |
| 2318 | // parameter of F. |
| 2319 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | Candidate.Conversions[ArgIdx + 1] |
| 2321 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2322 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 228eea3 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2323 | /*InOverloadResolution=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2325 | == ImplicitConversionSequence::BadConversion) { |
| 2326 | Candidate.Viable = false; |
| 2327 | break; |
| 2328 | } |
| 2329 | } else { |
| 2330 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2331 | // argument for which there is no corresponding parameter is |
| 2332 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2334 | = ImplicitConversionSequence::EllipsisConversion; |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2339 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2340 | /// set, using template argument deduction to produce an appropriate member |
| 2341 | /// function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 | void |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2343 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2344 | bool HasExplicitTemplateArgs, |
| 2345 | const TemplateArgument *ExplicitTemplateArgs, |
| 2346 | unsigned NumExplicitTemplateArgs, |
| 2347 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2348 | OverloadCandidateSet& CandidateSet, |
| 2349 | bool SuppressUserConversions, |
| 2350 | bool ForceRValue) { |
| 2351 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2353 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | // 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] | 2355 | // candidate functions in the usual way.113) A given name can refer to one |
| 2356 | // or more function templates and also to a set of overloaded non-template |
| 2357 | // functions. In such a case, the candidate functions generated from each |
| 2358 | // function template are combined with the set of non-template candidate |
| 2359 | // functions. |
| 2360 | TemplateDeductionInfo Info(Context); |
| 2361 | FunctionDecl *Specialization = 0; |
| 2362 | if (TemplateDeductionResult Result |
| 2363 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2364 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2365 | Args, NumArgs, Specialization, Info)) { |
| 2366 | // FIXME: Record what happened with template argument deduction, so |
| 2367 | // that we can give the user a beautiful diagnostic. |
| 2368 | (void)Result; |
| 2369 | return; |
| 2370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2372 | // Add the function template specialization produced by template argument |
| 2373 | // deduction as a candidate. |
| 2374 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2375 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2376 | "Specialization is not a member function?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2378 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2379 | } |
| 2380 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2381 | /// \brief Add a C++ function template specialization as a candidate |
| 2382 | /// in the candidate set, using template argument deduction to produce |
| 2383 | /// an appropriate function template specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | void |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2385 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2386 | bool HasExplicitTemplateArgs, |
| 2387 | const TemplateArgument *ExplicitTemplateArgs, |
| 2388 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2389 | Expr **Args, unsigned NumArgs, |
| 2390 | OverloadCandidateSet& CandidateSet, |
| 2391 | bool SuppressUserConversions, |
| 2392 | bool ForceRValue) { |
| 2393 | // C++ [over.match.funcs]p7: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2395 | // function template specializations are generated using template argument |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | // 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] | 2397 | // candidate functions in the usual way.113) A given name can refer to one |
| 2398 | // or more function templates and also to a set of overloaded non-template |
| 2399 | // functions. In such a case, the candidate functions generated from each |
| 2400 | // function template are combined with the set of non-template candidate |
| 2401 | // functions. |
| 2402 | TemplateDeductionInfo Info(Context); |
| 2403 | FunctionDecl *Specialization = 0; |
| 2404 | if (TemplateDeductionResult Result |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2405 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2406 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2407 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2408 | // FIXME: Record what happened with template argument deduction, so |
| 2409 | // that we can give the user a beautiful diagnostic. |
| 2410 | (void)Result; |
| 2411 | return; |
| 2412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2414 | // Add the function template specialization produced by template argument |
| 2415 | // deduction as a candidate. |
| 2416 | assert(Specialization && "Missing function template specialization?"); |
| 2417 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2418 | SuppressUserConversions, ForceRValue); |
| 2419 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2421 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2422 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2423 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | /// 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] | 2425 | /// (which may or may not be the same type as the type that the |
| 2426 | /// conversion function produces). |
| 2427 | void |
| 2428 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2429 | Expr *From, QualType ToType, |
| 2430 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2431 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2432 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2433 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2434 | // Add this candidate |
| 2435 | CandidateSet.push_back(OverloadCandidate()); |
| 2436 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2437 | Candidate.Function = Conversion; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2438 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2439 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2440 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2442 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2443 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2444 | |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2445 | // Determine the implicit conversion sequence for the implicit |
| 2446 | // object parameter. |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2447 | Candidate.Viable = true; |
| 2448 | Candidate.Conversions.resize(1); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2449 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2450 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2451 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2452 | == ImplicitConversionSequence::BadConversion) { |
| 2453 | Candidate.Viable = false; |
| 2454 | return; |
| 2455 | } |
| 2456 | |
| 2457 | // To determine what the conversion from the result of calling the |
| 2458 | // conversion function to the type we're eventually trying to |
| 2459 | // convert to (ToType), we need to synthesize a call to the |
| 2460 | // conversion function and attempt copy initialization from it. This |
| 2461 | // makes sure that we get the right semantics with respect to |
| 2462 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2463 | // call on the stack and we don't need its arguments to be |
| 2464 | // well-formed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2466 | SourceLocation()); |
| 2467 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Anders Carlsson | a261592 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2468 | CastExpr::CK_Unknown, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2469 | &ConversionRef, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
| 2471 | // 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] | 2472 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2473 | // allocator). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2475 | Conversion->getConversionType().getNonReferenceType(), |
| 2476 | SourceLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | ImplicitConversionSequence ICS = |
| 2478 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2479 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2480 | /*ForceRValue=*/false, |
| 2481 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2483 | switch (ICS.ConversionKind) { |
| 2484 | case ImplicitConversionSequence::StandardConversion: |
| 2485 | Candidate.FinalConversion = ICS.Standard; |
| 2486 | break; |
| 2487 | |
| 2488 | case ImplicitConversionSequence::BadConversion: |
| 2489 | Candidate.Viable = false; |
| 2490 | break; |
| 2491 | |
| 2492 | default: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | assert(false && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2494 | "Can only end up with a standard conversion sequence or failure"); |
| 2495 | } |
| 2496 | } |
| 2497 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2498 | /// \brief Adds a conversion function template specialization |
| 2499 | /// candidate to the overload set, using template argument deduction |
| 2500 | /// to deduce the template arguments of the conversion function |
| 2501 | /// template from the type that we are converting to (C++ |
| 2502 | /// [temp.deduct.conv]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | void |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2504 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2505 | Expr *From, QualType ToType, |
| 2506 | OverloadCandidateSet &CandidateSet) { |
| 2507 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2508 | "Only conversion function templates permitted here"); |
| 2509 | |
| 2510 | TemplateDeductionInfo Info(Context); |
| 2511 | CXXConversionDecl *Specialization = 0; |
| 2512 | if (TemplateDeductionResult Result |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2514 | Specialization, Info)) { |
| 2515 | // FIXME: Record what happened with template argument deduction, so |
| 2516 | // that we can give the user a beautiful diagnostic. |
| 2517 | (void)Result; |
| 2518 | return; |
| 2519 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2520 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2521 | // Add the conversion function template specialization produced by |
| 2522 | // template argument deduction as a candidate. |
| 2523 | assert(Specialization && "Missing function template specialization?"); |
| 2524 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2525 | } |
| 2526 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2527 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2528 | /// converts the given @c Object to a function pointer via the |
| 2529 | /// conversion function @c Conversion, and then attempts to call it |
| 2530 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2531 | /// the type of function that we'll eventually be calling. |
| 2532 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2533 | const FunctionProtoType *Proto, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2534 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2535 | OverloadCandidateSet& CandidateSet) { |
| 2536 | CandidateSet.push_back(OverloadCandidate()); |
| 2537 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2538 | Candidate.Function = 0; |
| 2539 | Candidate.Surrogate = Conversion; |
| 2540 | Candidate.Viable = true; |
| 2541 | Candidate.IsSurrogate = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2542 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2543 | Candidate.Conversions.resize(NumArgs + 1); |
| 2544 | |
| 2545 | // Determine the implicit conversion sequence for the implicit |
| 2546 | // object parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2548 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2549 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2550 | Candidate.Viable = false; |
| 2551 | return; |
| 2552 | } |
| 2553 | |
| 2554 | // The first conversion is actually a user-defined conversion whose |
| 2555 | // first conversion is ObjectInit's standard conversion (which is |
| 2556 | // effectively a reference binding). Record it as such. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2558 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2559 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2560 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2562 | = Candidate.Conversions[0].UserDefined.Before; |
| 2563 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2564 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | // Find the |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2566 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2567 | |
| 2568 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2569 | // parameters is viable only if it has an ellipsis in its parameter |
| 2570 | // list (8.3.5). |
| 2571 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2572 | Candidate.Viable = false; |
| 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | // Function types don't have any default arguments, so just check if |
| 2577 | // we have enough arguments. |
| 2578 | if (NumArgs < NumArgsInProto) { |
| 2579 | // Not enough arguments. |
| 2580 | Candidate.Viable = false; |
| 2581 | return; |
| 2582 | } |
| 2583 | |
| 2584 | // Determine the implicit conversion sequences for each of the |
| 2585 | // arguments. |
| 2586 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2587 | if (ArgIdx < NumArgsInProto) { |
| 2588 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2589 | // exist for each argument an implicit conversion sequence |
| 2590 | // (13.3.3.1) that converts that argument to the corresponding |
| 2591 | // parameter of F. |
| 2592 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | Candidate.Conversions[ArgIdx + 1] |
| 2594 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2595 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2596 | /*ForceRValue=*/false, |
| 2597 | /*InOverloadResolution=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2599 | == ImplicitConversionSequence::BadConversion) { |
| 2600 | Candidate.Viable = false; |
| 2601 | break; |
| 2602 | } |
| 2603 | } else { |
| 2604 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2605 | // argument for which there is no corresponding parameter is |
| 2606 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2608 | = ImplicitConversionSequence::EllipsisConversion; |
| 2609 | } |
| 2610 | } |
| 2611 | } |
| 2612 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2613 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2614 | // operator overloading logic over to the scheme used by binary operators, which |
| 2615 | // works for template instantiation. |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2616 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2617 | SourceLocation OpLoc, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2618 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 94eabf3 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2619 | OverloadCandidateSet& CandidateSet, |
| 2620 | SourceRange OpRange) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2621 | |
| 2622 | FunctionSet Functions; |
| 2623 | |
| 2624 | QualType T1 = Args[0]->getType(); |
| 2625 | QualType T2; |
| 2626 | if (NumArgs > 1) |
| 2627 | T2 = Args[1]->getType(); |
| 2628 | |
| 2629 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2630 | if (S) |
| 2631 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2632 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2633 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2634 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2635 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2636 | } |
| 2637 | |
| 2638 | /// \brief Add overload candidates for overloaded operators that are |
| 2639 | /// member functions. |
| 2640 | /// |
| 2641 | /// Add the overloaded operator candidates that are member functions |
| 2642 | /// for the operator Op that was used in an operator expression such |
| 2643 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2644 | /// CandidateSet will store the added overload candidates. (C++ |
| 2645 | /// [over.match.oper]). |
| 2646 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2647 | SourceLocation OpLoc, |
| 2648 | Expr **Args, unsigned NumArgs, |
| 2649 | OverloadCandidateSet& CandidateSet, |
| 2650 | SourceRange OpRange) { |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2651 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2652 | |
| 2653 | // C++ [over.match.oper]p3: |
| 2654 | // For a unary operator @ with an operand of a type whose |
| 2655 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2656 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2657 | // a right operand of a type whose cv-unqualified version is T2, |
| 2658 | // three sets of candidate functions, designated member |
| 2659 | // candidates, non-member candidates and built-in candidates, are |
| 2660 | // constructed as follows: |
| 2661 | QualType T1 = Args[0]->getType(); |
| 2662 | QualType T2; |
| 2663 | if (NumArgs > 1) |
| 2664 | T2 = Args[1]->getType(); |
| 2665 | |
| 2666 | // -- If T1 is a class type, the set of member candidates is the |
| 2667 | // result of the qualified lookup of T1::operator@ |
| 2668 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2669 | // empty. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2670 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2671 | // Complete the type if it can be completed. Otherwise, we're done. |
| 2672 | if (RequireCompleteType(OpLoc, T1, PartialDiagnostic(0))) |
| 2673 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | |
| 2675 | LookupResult Operators = LookupQualifiedName(T1Rec->getDecl(), OpName, |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2676 | LookupOrdinaryName, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 6a1f965 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2678 | OperEnd = Operators.end(); |
| 2679 | Oper != OperEnd; |
| 2680 | ++Oper) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2682 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2683 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2684 | } |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2687 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2688 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2689 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2690 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2691 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2692 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2693 | /// (at the beginning of the argument list) that will be contextually |
| 2694 | /// converted to bool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2696 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2697 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2698 | bool IsAssignmentOperator, |
| 2699 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2700 | // Add this candidate |
| 2701 | CandidateSet.push_back(OverloadCandidate()); |
| 2702 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2703 | Candidate.Function = 0; |
Douglas Gregor | 1d248c5 | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2704 | Candidate.IsSurrogate = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2705 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2706 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2707 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2708 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2709 | |
| 2710 | // Determine the implicit conversion sequences for each of the |
| 2711 | // arguments. |
| 2712 | Candidate.Viable = true; |
| 2713 | Candidate.Conversions.resize(NumArgs); |
| 2714 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2715 | // C++ [over.match.oper]p4: |
| 2716 | // For the built-in assignment operators, conversions of the |
| 2717 | // left operand are restricted as follows: |
| 2718 | // -- no temporaries are introduced to hold the left operand, and |
| 2719 | // -- no user-defined conversions are applied to the left |
| 2720 | // operand to achieve a type match with the left-most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | // parameter of a built-in candidate. |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2722 | // |
| 2723 | // We block these conversions by turning off user-defined |
| 2724 | // conversions, since that is the only way that initialization of |
| 2725 | // a reference to a non-class type can occur from something that |
| 2726 | // is not of the same type. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2727 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2729 | "Contextual conversion to bool requires bool type"); |
| 2730 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2731 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | Candidate.Conversions[ArgIdx] |
| 2733 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2734 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2735 | /*ForceRValue=*/false, |
| 2736 | /*InOverloadResolution=*/false); |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2737 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2739 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2740 | Candidate.Viable = false; |
Douglas Gregor | 436424c | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2741 | break; |
| 2742 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2747 | /// candidate operator functions for built-in operators (C++ |
| 2748 | /// [over.built]). The types are separated into pointer types and |
| 2749 | /// enumeration types. |
| 2750 | class BuiltinCandidateTypeSet { |
| 2751 | /// TypeSet - A set of types. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2752 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2753 | |
| 2754 | /// PointerTypes - The set of pointer types that will be used in the |
| 2755 | /// built-in candidates. |
| 2756 | TypeSet PointerTypes; |
| 2757 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2758 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2759 | /// used in the built-in candidates. |
| 2760 | TypeSet MemberPointerTypes; |
| 2761 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2762 | /// EnumerationTypes - The set of enumeration types that will be |
| 2763 | /// used in the built-in candidates. |
| 2764 | TypeSet EnumerationTypes; |
| 2765 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2766 | /// Sema - The semantic analysis instance where we are building the |
| 2767 | /// candidate type set. |
| 2768 | Sema &SemaRef; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2770 | /// Context - The AST context in which we will build the type sets. |
| 2771 | ASTContext &Context; |
| 2772 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2773 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 2774 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2775 | |
| 2776 | public: |
| 2777 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2778 | typedef TypeSet::iterator iterator; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2779 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2781 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2782 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2783 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2784 | bool AllowExplicitConversions); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2785 | |
| 2786 | /// pointer_begin - First pointer type found; |
| 2787 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2788 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2789 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2790 | iterator pointer_end() { return PointerTypes.end(); } |
| 2791 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2792 | /// member_pointer_begin - First member pointer type found; |
| 2793 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2794 | |
| 2795 | /// member_pointer_end - Past the last member pointer type found; |
| 2796 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2797 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2798 | /// enumeration_begin - First enumeration type found; |
| 2799 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2800 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2801 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2802 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2803 | }; |
| 2804 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2805 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2806 | /// the set of pointer types along with any more-qualified variants of |
| 2807 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2808 | /// will add "int const *", "int const volatile *", "int const |
| 2809 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2810 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2811 | /// false otherwise. |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2812 | bool |
| 2813 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2814 | // Insert this type. |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2815 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2816 | return false; |
| 2817 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2818 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2819 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2820 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2821 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2822 | // FIXME: Do we have to add CVR qualifiers at *all* levels to deal with all |
| 2823 | // pointer conversions that don't cast away constness? |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2824 | if (!PointeeTy.isConstQualified()) |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2825 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2826 | (Context.getPointerType(PointeeTy.withConst())); |
| 2827 | if (!PointeeTy.isVolatileQualified()) |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2828 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2829 | (Context.getPointerType(PointeeTy.withVolatile())); |
| 2830 | if (!PointeeTy.isRestrictQualified()) |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2831 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2832 | (Context.getPointerType(PointeeTy.withRestrict())); |
| 2833 | } |
| 2834 | |
| 2835 | return true; |
| 2836 | } |
| 2837 | |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2838 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 2839 | /// to the set of pointer types along with any more-qualified variants of |
| 2840 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2841 | /// will add "int const *", "int const volatile *", "int const |
| 2842 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2843 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2844 | /// false otherwise. |
| 2845 | bool |
| 2846 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 2847 | QualType Ty) { |
| 2848 | // Insert this type. |
| 2849 | if (!MemberPointerTypes.insert(Ty)) |
| 2850 | return false; |
| 2851 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2852 | if (const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>()) { |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2853 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2854 | const Type *ClassTy = PointerTy->getClass(); |
| 2855 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2856 | |
| 2857 | if (!PointeeTy.isConstQualified()) |
| 2858 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2859 | (Context.getMemberPointerType(PointeeTy.withConst(), ClassTy)); |
| 2860 | if (!PointeeTy.isVolatileQualified()) |
| 2861 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2862 | (Context.getMemberPointerType(PointeeTy.withVolatile(), ClassTy)); |
| 2863 | if (!PointeeTy.isRestrictQualified()) |
| 2864 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2865 | (Context.getMemberPointerType(PointeeTy.withRestrict(), ClassTy)); |
| 2866 | } |
| 2867 | |
| 2868 | return true; |
| 2869 | } |
| 2870 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2871 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2872 | /// 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] | 2873 | /// primarily interested in pointer types and enumeration types. We also |
| 2874 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2875 | /// AllowUserConversions is true if we should look at the conversion |
| 2876 | /// functions of a class type, and AllowExplicitConversions if we |
| 2877 | /// should also include the explicit conversion functions of a class |
| 2878 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2879 | void |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2880 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2881 | bool AllowUserConversions, |
| 2882 | bool AllowExplicitConversions) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2883 | // Only deal with canonical types. |
| 2884 | Ty = Context.getCanonicalType(Ty); |
| 2885 | |
| 2886 | // Look through reference types; they aren't part of the type of an |
| 2887 | // expression for the purposes of conversions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2888 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2889 | Ty = RefTy->getPointeeType(); |
| 2890 | |
| 2891 | // We don't care about qualifiers on the type. |
| 2892 | Ty = Ty.getUnqualifiedType(); |
| 2893 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2894 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2895 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2896 | |
| 2897 | // Insert our type, and its more-qualified variants, into the set |
| 2898 | // of types. |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2899 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty)) |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2900 | return; |
| 2901 | |
| 2902 | // Add 'cv void*' to our set of types. |
| 2903 | if (!Ty->isVoidType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | QualType QualVoid |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2905 | = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2906 | AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | // If this is a pointer to a class type, add pointers to its bases |
| 2910 | // (with the same level of cv-qualification as the original |
| 2911 | // derived class, of course). |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2912 | if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2913 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2914 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2915 | Base != ClassDecl->bases_end(); ++Base) { |
| 2916 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
| 2917 | BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2918 | |
| 2919 | // Add the pointer type, recursively, so that we get all of |
| 2920 | // the indirect base classes, too. |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2921 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2922 | } |
| 2923 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2924 | } else if (Ty->isMemberPointerType()) { |
| 2925 | // Member pointers are far easier, since the pointee can't be converted. |
| 2926 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 2927 | return; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2928 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | a59a3e2 | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2929 | EnumerationTypes.insert(Ty); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2930 | } else if (AllowUserConversions) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2931 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2932 | if (SemaRef.RequireCompleteType(SourceLocation(), Ty, 0)) { |
| 2933 | // No conversion functions in incomplete types. |
| 2934 | return; |
| 2935 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2937 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 2938 | // FIXME: Visit conversion functions in the base classes, too. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2940 | = ClassDecl->getConversionFunctions(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2942 | = Conversions->function_begin(); |
| 2943 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2944 | CXXConversionDecl *Conv; |
| 2945 | FunctionTemplateDecl *ConvTemplate; |
| 2946 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 2947 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2949 | // about which builtin types we can convert to. |
| 2950 | if (ConvTemplate) |
| 2951 | continue; |
| 2952 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2953 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 2954 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2955 | } |
| 2956 | } |
| 2957 | } |
| 2958 | } |
| 2959 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2960 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 2961 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 2962 | /// given type to the candidate set. |
| 2963 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 2964 | QualType T, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | Expr **Args, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2966 | unsigned NumArgs, |
| 2967 | OverloadCandidateSet &CandidateSet) { |
| 2968 | QualType ParamTypes[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2970 | // T& operator=(T&, T) |
| 2971 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 2972 | ParamTypes[1] = T; |
| 2973 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 2974 | /*IsAssignmentOperator=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2976 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 2977 | // volatile T& operator=(volatile T&, T) |
| 2978 | ParamTypes[0] = S.Context.getLValueReferenceType(T.withVolatile()); |
| 2979 | ParamTypes[1] = T; |
| 2980 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2982 | } |
| 2983 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2985 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 2986 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 2987 | /// on the operator @p Op and the arguments given. For example, if the |
| 2988 | /// operator is a binary '+', this routine might add "int |
| 2989 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2990 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2992 | Expr **Args, unsigned NumArgs, |
| 2993 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2994 | // The set of "promoted arithmetic types", which are the arithmetic |
| 2995 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 2996 | // that the first few of these types are the promoted integral |
| 2997 | // types; these types need to be first. |
| 2998 | // FIXME: What about complex? |
| 2999 | const unsigned FirstIntegralType = 0; |
| 3000 | const unsigned LastIntegralType = 13; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3002 | LastPromotedIntegralType = 13; |
| 3003 | const unsigned FirstPromotedArithmeticType = 7, |
| 3004 | LastPromotedArithmeticType = 16; |
| 3005 | const unsigned NumArithmeticTypes = 16; |
| 3006 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 3008 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3009 | Context.SignedCharTy, Context.ShortTy, |
| 3010 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 3011 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 3012 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 3013 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 3014 | }; |
| 3015 | |
| 3016 | // Find all of the types that the arguments can convert to, but only |
| 3017 | // if the operator we're looking at has built-in operator candidates |
| 3018 | // that make use of these types. |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3019 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3020 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3021 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3022 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3023 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3024 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3025 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3026 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3027 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 3028 | true, |
| 3029 | (Op == OO_Exclaim || |
| 3030 | Op == OO_AmpAmp || |
| 3031 | Op == OO_PipePipe)); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | bool isComparison = false; |
| 3035 | switch (Op) { |
| 3036 | case OO_None: |
| 3037 | case NUM_OVERLOADED_OPERATORS: |
| 3038 | assert(false && "Expected an overloaded operator"); |
| 3039 | break; |
| 3040 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3041 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | if (NumArgs == 1) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3043 | goto UnaryStar; |
| 3044 | else |
| 3045 | goto BinaryStar; |
| 3046 | break; |
| 3047 | |
| 3048 | case OO_Plus: // '+' is either unary or binary |
| 3049 | if (NumArgs == 1) |
| 3050 | goto UnaryPlus; |
| 3051 | else |
| 3052 | goto BinaryPlus; |
| 3053 | break; |
| 3054 | |
| 3055 | case OO_Minus: // '-' is either unary or binary |
| 3056 | if (NumArgs == 1) |
| 3057 | goto UnaryMinus; |
| 3058 | else |
| 3059 | goto BinaryMinus; |
| 3060 | break; |
| 3061 | |
| 3062 | case OO_Amp: // '&' is either unary or binary |
| 3063 | if (NumArgs == 1) |
| 3064 | goto UnaryAmp; |
| 3065 | else |
| 3066 | goto BinaryAmp; |
| 3067 | |
| 3068 | case OO_PlusPlus: |
| 3069 | case OO_MinusMinus: |
| 3070 | // C++ [over.built]p3: |
| 3071 | // |
| 3072 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3073 | // is either volatile or empty, there exist candidate operator |
| 3074 | // functions of the form |
| 3075 | // |
| 3076 | // VQ T& operator++(VQ T&); |
| 3077 | // T operator++(VQ T&, int); |
| 3078 | // |
| 3079 | // C++ [over.built]p4: |
| 3080 | // |
| 3081 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3082 | // than bool, and VQ is either volatile or empty, there exist |
| 3083 | // candidate operator functions of the form |
| 3084 | // |
| 3085 | // VQ T& operator--(VQ T&); |
| 3086 | // T operator--(VQ T&, int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3088 | Arith < NumArithmeticTypes; ++Arith) { |
| 3089 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | QualType ParamTypes[2] |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3091 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3092 | |
| 3093 | // Non-volatile version. |
| 3094 | if (NumArgs == 1) |
| 3095 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3096 | else |
| 3097 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3098 | |
| 3099 | // Volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3100 | ParamTypes[0] = Context.getLValueReferenceType(ArithTy.withVolatile()); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3101 | if (NumArgs == 1) |
| 3102 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3103 | else |
| 3104 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3105 | } |
| 3106 | |
| 3107 | // C++ [over.built]p5: |
| 3108 | // |
| 3109 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3110 | // cv-unqualified object type, and VQ is either volatile or |
| 3111 | // empty, there exist candidate operator functions of the form |
| 3112 | // |
| 3113 | // T*VQ& operator++(T*VQ&); |
| 3114 | // T*VQ& operator--(T*VQ&); |
| 3115 | // T* operator++(T*VQ&, int); |
| 3116 | // T* operator--(T*VQ&, int); |
| 3117 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3118 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3119 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3120 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3121 | continue; |
| 3122 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | QualType ParamTypes[2] = { |
| 3124 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3125 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3127 | // Without volatile |
| 3128 | if (NumArgs == 1) |
| 3129 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3130 | else |
| 3131 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3132 | |
| 3133 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3134 | // With volatile |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3135 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3136 | if (NumArgs == 1) |
| 3137 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3138 | else |
| 3139 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3140 | } |
| 3141 | } |
| 3142 | break; |
| 3143 | |
| 3144 | UnaryStar: |
| 3145 | // C++ [over.built]p6: |
| 3146 | // For every cv-qualified or cv-unqualified object type T, there |
| 3147 | // exist candidate operator functions of the form |
| 3148 | // |
| 3149 | // T& operator*(T*); |
| 3150 | // |
| 3151 | // C++ [over.built]p7: |
| 3152 | // For every function type T, there exist candidate operator |
| 3153 | // functions of the form |
| 3154 | // T& operator*(T*); |
| 3155 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3156 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3157 | QualType ParamTy = *Ptr; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3158 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3160 | &ParamTy, Args, 1, CandidateSet); |
| 3161 | } |
| 3162 | break; |
| 3163 | |
| 3164 | UnaryPlus: |
| 3165 | // C++ [over.built]p8: |
| 3166 | // For every type T, there exist candidate operator functions of |
| 3167 | // the form |
| 3168 | // |
| 3169 | // T* operator+(T*); |
| 3170 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3171 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3172 | QualType ParamTy = *Ptr; |
| 3173 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3174 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3176 | // Fall through |
| 3177 | |
| 3178 | UnaryMinus: |
| 3179 | // C++ [over.built]p9: |
| 3180 | // For every promoted arithmetic type T, there exist candidate |
| 3181 | // operator functions of the form |
| 3182 | // |
| 3183 | // T operator+(T); |
| 3184 | // T operator-(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3186 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3187 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3188 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3189 | } |
| 3190 | break; |
| 3191 | |
| 3192 | case OO_Tilde: |
| 3193 | // C++ [over.built]p10: |
| 3194 | // For every promoted integral type T, there exist candidate |
| 3195 | // operator functions of the form |
| 3196 | // |
| 3197 | // T operator~(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3199 | Int < LastPromotedIntegralType; ++Int) { |
| 3200 | QualType IntTy = ArithmeticTypes[Int]; |
| 3201 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3202 | } |
| 3203 | break; |
| 3204 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3205 | case OO_New: |
| 3206 | case OO_Delete: |
| 3207 | case OO_Array_New: |
| 3208 | case OO_Array_Delete: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3209 | case OO_Call: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3210 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3211 | break; |
| 3212 | |
| 3213 | case OO_Comma: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3214 | UnaryAmp: |
| 3215 | case OO_Arrow: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3216 | // C++ [over.match.oper]p3: |
| 3217 | // -- For the operator ',', the unary operator '&', or the |
| 3218 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3219 | break; |
| 3220 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3221 | case OO_EqualEqual: |
| 3222 | case OO_ExclaimEqual: |
| 3223 | // C++ [over.match.oper]p16: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | // For every pointer to member type T, there exist candidate operator |
| 3225 | // functions of the form |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3226 | // |
| 3227 | // bool operator==(T,T); |
| 3228 | // bool operator!=(T,T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3230 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3231 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3232 | MemPtr != MemPtrEnd; |
| 3233 | ++MemPtr) { |
| 3234 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3235 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3236 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3238 | // Fall through |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3240 | case OO_Less: |
| 3241 | case OO_Greater: |
| 3242 | case OO_LessEqual: |
| 3243 | case OO_GreaterEqual: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3244 | // C++ [over.built]p15: |
| 3245 | // |
| 3246 | // For every pointer or enumeration type T, there exist |
| 3247 | // candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3249 | // bool operator<(T, T); |
| 3250 | // bool operator>(T, T); |
| 3251 | // bool operator<=(T, T); |
| 3252 | // bool operator>=(T, T); |
| 3253 | // bool operator==(T, T); |
| 3254 | // bool operator!=(T, T); |
| 3255 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3256 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3257 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3258 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3259 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3261 | = CandidateTypes.enumeration_begin(); |
| 3262 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3263 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3264 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3265 | } |
| 3266 | |
| 3267 | // Fall through. |
| 3268 | isComparison = true; |
| 3269 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3270 | BinaryPlus: |
| 3271 | BinaryMinus: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3272 | if (!isComparison) { |
| 3273 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3274 | |
| 3275 | // C++ [over.built]p13: |
| 3276 | // |
| 3277 | // For every cv-qualified or cv-unqualified object type T |
| 3278 | // there exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3280 | // T* operator+(T*, ptrdiff_t); |
| 3281 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3282 | // T* operator-(T*, ptrdiff_t); |
| 3283 | // T* operator+(ptrdiff_t, T*); |
| 3284 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3285 | // |
| 3286 | // C++ [over.built]p14: |
| 3287 | // |
| 3288 | // For every T, where T is a pointer to object type, there |
| 3289 | // exist candidate operator functions of the form |
| 3290 | // |
| 3291 | // ptrdiff_t operator-(T, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3293 | = CandidateTypes.pointer_begin(); |
| 3294 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3295 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3296 | |
| 3297 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3298 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3299 | |
| 3300 | if (Op == OO_Plus) { |
| 3301 | // T* operator+(ptrdiff_t, T*); |
| 3302 | ParamTypes[0] = ParamTypes[1]; |
| 3303 | ParamTypes[1] = *Ptr; |
| 3304 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3305 | } else { |
| 3306 | // ptrdiff_t operator-(T, T); |
| 3307 | ParamTypes[1] = *Ptr; |
| 3308 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3309 | Args, 2, CandidateSet); |
| 3310 | } |
| 3311 | } |
| 3312 | } |
| 3313 | // Fall through |
| 3314 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3315 | case OO_Slash: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3316 | BinaryStar: |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3317 | Conditional: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3318 | // C++ [over.built]p12: |
| 3319 | // |
| 3320 | // For every pair of promoted arithmetic types L and R, there |
| 3321 | // exist candidate operator functions of the form |
| 3322 | // |
| 3323 | // LR operator*(L, R); |
| 3324 | // LR operator/(L, R); |
| 3325 | // LR operator+(L, R); |
| 3326 | // LR operator-(L, R); |
| 3327 | // bool operator<(L, R); |
| 3328 | // bool operator>(L, R); |
| 3329 | // bool operator<=(L, R); |
| 3330 | // bool operator>=(L, R); |
| 3331 | // bool operator==(L, R); |
| 3332 | // bool operator!=(L, R); |
| 3333 | // |
| 3334 | // where LR is the result of the usual arithmetic conversions |
| 3335 | // between types L and R. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3336 | // |
| 3337 | // C++ [over.built]p24: |
| 3338 | // |
| 3339 | // For every pair of promoted arithmetic types L and R, there exist |
| 3340 | // candidate operator functions of the form |
| 3341 | // |
| 3342 | // LR operator?(bool, L, R); |
| 3343 | // |
| 3344 | // where LR is the result of the usual arithmetic conversions |
| 3345 | // between types L and R. |
| 3346 | // Our candidates ignore the first parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3348 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3350 | Right < LastPromotedArithmeticType; ++Right) { |
| 3351 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3352 | QualType Result |
| 3353 | = isComparison |
| 3354 | ? Context.BoolTy |
| 3355 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3356 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3357 | } |
| 3358 | } |
| 3359 | break; |
| 3360 | |
| 3361 | case OO_Percent: |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3362 | BinaryAmp: |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3363 | case OO_Caret: |
| 3364 | case OO_Pipe: |
| 3365 | case OO_LessLess: |
| 3366 | case OO_GreaterGreater: |
| 3367 | // C++ [over.built]p17: |
| 3368 | // |
| 3369 | // For every pair of promoted integral types L and R, there |
| 3370 | // exist candidate operator functions of the form |
| 3371 | // |
| 3372 | // LR operator%(L, R); |
| 3373 | // LR operator&(L, R); |
| 3374 | // LR operator^(L, R); |
| 3375 | // LR operator|(L, R); |
| 3376 | // L operator<<(L, R); |
| 3377 | // L operator>>(L, R); |
| 3378 | // |
| 3379 | // where LR is the result of the usual arithmetic conversions |
| 3380 | // between types L and R. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3382 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3383 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3384 | Right < LastPromotedIntegralType; ++Right) { |
| 3385 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3386 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3387 | ? LandR[0] |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3388 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3389 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3390 | } |
| 3391 | } |
| 3392 | break; |
| 3393 | |
| 3394 | case OO_Equal: |
| 3395 | // C++ [over.built]p20: |
| 3396 | // |
| 3397 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3398 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3399 | // empty, there exist candidate operator functions of the form |
| 3400 | // |
| 3401 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3402 | for (BuiltinCandidateTypeSet::iterator |
| 3403 | Enum = CandidateTypes.enumeration_begin(), |
| 3404 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3405 | Enum != EnumEnd; ++Enum) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3407 | CandidateSet); |
| 3408 | for (BuiltinCandidateTypeSet::iterator |
| 3409 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3410 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3411 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 84605ae | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3413 | CandidateSet); |
| 3414 | // Fall through. |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3415 | |
| 3416 | case OO_PlusEqual: |
| 3417 | case OO_MinusEqual: |
| 3418 | // C++ [over.built]p19: |
| 3419 | // |
| 3420 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3421 | // volatile or empty, there exist candidate operator functions |
| 3422 | // of the form |
| 3423 | // |
| 3424 | // T*VQ& operator=(T*VQ&, T*); |
| 3425 | // |
| 3426 | // C++ [over.built]p21: |
| 3427 | // |
| 3428 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3429 | // cv-unqualified object type and VQ is either volatile or |
| 3430 | // empty, there exist candidate operator functions of the form |
| 3431 | // |
| 3432 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3433 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3434 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3435 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3436 | QualType ParamTypes[2]; |
| 3437 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3438 | |
| 3439 | // non-volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3440 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3441 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3442 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3443 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3444 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3445 | // volatile version |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3446 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3447 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3448 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3449 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3450 | } |
| 3451 | // Fall through. |
| 3452 | |
| 3453 | case OO_StarEqual: |
| 3454 | case OO_SlashEqual: |
| 3455 | // C++ [over.built]p18: |
| 3456 | // |
| 3457 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3458 | // VQ is either volatile or empty, and R is a promoted |
| 3459 | // arithmetic type, there exist candidate operator functions of |
| 3460 | // the form |
| 3461 | // |
| 3462 | // VQ L& operator=(VQ L&, R); |
| 3463 | // VQ L& operator*=(VQ L&, R); |
| 3464 | // VQ L& operator/=(VQ L&, R); |
| 3465 | // VQ L& operator+=(VQ L&, R); |
| 3466 | // VQ L& operator-=(VQ L&, R); |
| 3467 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3469 | Right < LastPromotedArithmeticType; ++Right) { |
| 3470 | QualType ParamTypes[2]; |
| 3471 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3472 | |
| 3473 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3474 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3475 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3476 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3477 | |
| 3478 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3479 | ParamTypes[0] = ArithmeticTypes[Left].withVolatile(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3480 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | c5e6107 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3481 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3482 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3483 | } |
| 3484 | } |
| 3485 | break; |
| 3486 | |
| 3487 | case OO_PercentEqual: |
| 3488 | case OO_LessLessEqual: |
| 3489 | case OO_GreaterGreaterEqual: |
| 3490 | case OO_AmpEqual: |
| 3491 | case OO_CaretEqual: |
| 3492 | case OO_PipeEqual: |
| 3493 | // C++ [over.built]p22: |
| 3494 | // |
| 3495 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3496 | // is either volatile or empty, and R is a promoted integral |
| 3497 | // type, there exist candidate operator functions of the form |
| 3498 | // |
| 3499 | // VQ L& operator%=(VQ L&, R); |
| 3500 | // VQ L& operator<<=(VQ L&, R); |
| 3501 | // VQ L& operator>>=(VQ L&, R); |
| 3502 | // VQ L& operator&=(VQ L&, R); |
| 3503 | // VQ L& operator^=(VQ L&, R); |
| 3504 | // VQ L& operator|=(VQ L&, R); |
| 3505 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3506 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3507 | Right < LastPromotedIntegralType; ++Right) { |
| 3508 | QualType ParamTypes[2]; |
| 3509 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3510 | |
| 3511 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3512 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3513 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3514 | |
| 3515 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3516 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3517 | ParamTypes[0].addVolatile(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3518 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3519 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3520 | } |
| 3521 | } |
| 3522 | break; |
| 3523 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3524 | case OO_Exclaim: { |
| 3525 | // C++ [over.operator]p23: |
| 3526 | // |
| 3527 | // There also exist candidate operator functions of the form |
| 3528 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | // bool operator!(bool); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3530 | // bool operator&&(bool, bool); [BELOW] |
| 3531 | // bool operator||(bool, bool); [BELOW] |
| 3532 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3533 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3534 | /*IsAssignmentOperator=*/false, |
| 3535 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3536 | break; |
| 3537 | } |
| 3538 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3539 | case OO_AmpAmp: |
| 3540 | case OO_PipePipe: { |
| 3541 | // C++ [over.operator]p23: |
| 3542 | // |
| 3543 | // There also exist candidate operator functions of the form |
| 3544 | // |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3545 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3546 | // bool operator&&(bool, bool); |
| 3547 | // bool operator||(bool, bool); |
| 3548 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3549 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3550 | /*IsAssignmentOperator=*/false, |
| 3551 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3552 | break; |
| 3553 | } |
| 3554 | |
| 3555 | case OO_Subscript: |
| 3556 | // C++ [over.built]p13: |
| 3557 | // |
| 3558 | // For every cv-qualified or cv-unqualified object type T there |
| 3559 | // exist candidate operator functions of the form |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | // |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3561 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3562 | // T& operator[](T*, ptrdiff_t); |
| 3563 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3564 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3565 | // T& operator[](ptrdiff_t, T*); |
| 3566 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3567 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3568 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3569 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3570 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3571 | |
| 3572 | // T& operator[](T*, ptrdiff_t) |
| 3573 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3574 | |
| 3575 | // T& operator[](ptrdiff_t, T*); |
| 3576 | ParamTypes[0] = ParamTypes[1]; |
| 3577 | ParamTypes[1] = *Ptr; |
| 3578 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3579 | } |
| 3580 | break; |
| 3581 | |
| 3582 | case OO_ArrowStar: |
| 3583 | // FIXME: No support for pointer-to-members yet. |
| 3584 | break; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3585 | |
| 3586 | case OO_Conditional: |
| 3587 | // Note that we don't consider the first argument, since it has been |
| 3588 | // contextually converted to bool long ago. The candidates below are |
| 3589 | // therefore added as binary. |
| 3590 | // |
| 3591 | // C++ [over.built]p24: |
| 3592 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3593 | // there exist candidate operator functions of the form |
| 3594 | // |
| 3595 | // T operator?(bool, T, T); |
| 3596 | // |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3597 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3598 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3599 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3600 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3601 | } |
Sebastian Redl | 8ce189f | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3602 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3603 | CandidateTypes.member_pointer_begin(), |
| 3604 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3605 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3606 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3607 | } |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3608 | goto Conditional; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3609 | } |
| 3610 | } |
| 3611 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3612 | /// \brief Add function candidates found via argument-dependent lookup |
| 3613 | /// to the set of overloading candidates. |
| 3614 | /// |
| 3615 | /// This routine performs argument-dependent name lookup based on the |
| 3616 | /// given function name (which may also be an operator name) and adds |
| 3617 | /// all of the overload candidates found by ADL to the overload |
| 3618 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | void |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3620 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3621 | Expr **Args, unsigned NumArgs, |
| 3622 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3623 | FunctionSet Functions; |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3624 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3625 | // Record all of the function candidates that we've already |
| 3626 | // added to the overload set, so that we don't add those same |
| 3627 | // candidates a second time. |
| 3628 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3629 | CandEnd = CandidateSet.end(); |
| 3630 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3631 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3632 | Functions.insert(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3633 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3634 | Functions.insert(FunTmpl); |
| 3635 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3636 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3637 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3638 | |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3639 | // Erase all of the candidates we already knew about. |
| 3640 | // FIXME: This is suboptimal. Is there a better way? |
| 3641 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3642 | CandEnd = CandidateSet.end(); |
| 3643 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3644 | if (Cand->Function) { |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3645 | Functions.erase(Cand->Function); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3646 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3647 | Functions.erase(FunTmpl); |
| 3648 | } |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3649 | |
| 3650 | // For each of the ADL candidates we found, add it to the overload |
| 3651 | // set. |
| 3652 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3653 | FuncEnd = Functions.end(); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3654 | Func != FuncEnd; ++Func) { |
| 3655 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) |
| 3656 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet); |
| 3657 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3659 | /*FIXME: explicit args */false, 0, 0, |
| 3660 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 15448f8 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3661 | } |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3664 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3665 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3666 | bool |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3667 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3669 | // Define viable functions to be better candidates than non-viable |
| 3670 | // functions. |
| 3671 | if (!Cand2.Viable) |
| 3672 | return Cand1.Viable; |
| 3673 | else if (!Cand1.Viable) |
| 3674 | return false; |
| 3675 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3676 | // C++ [over.match.best]p1: |
| 3677 | // |
| 3678 | // -- if F is a static member function, ICS1(F) is defined such |
| 3679 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3680 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3681 | // better nor worse than ICS1(F). |
| 3682 | unsigned StartArg = 0; |
| 3683 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3684 | StartArg = 1; |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3686 | // C++ [over.match.best]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | // A viable function F1 is defined to be a better function than another |
| 3688 | // 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] | 3689 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3690 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3691 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3692 | bool HasBetterConversion = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3693 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3694 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3695 | Cand2.Conversions[ArgIdx])) { |
| 3696 | case ImplicitConversionSequence::Better: |
| 3697 | // Cand1 has a better conversion sequence. |
| 3698 | HasBetterConversion = true; |
| 3699 | break; |
| 3700 | |
| 3701 | case ImplicitConversionSequence::Worse: |
| 3702 | // Cand1 can't be better than Cand2. |
| 3703 | return false; |
| 3704 | |
| 3705 | case ImplicitConversionSequence::Indistinguishable: |
| 3706 | // Do nothing. |
| 3707 | break; |
| 3708 | } |
| 3709 | } |
| 3710 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3712 | // ICSj(F2), or, if not that, |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3713 | if (HasBetterConversion) |
| 3714 | return true; |
| 3715 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | d3cb356 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3717 | // specialization, or, if not that, |
| 3718 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 3719 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 3720 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
| 3722 | // -- F1 and F2 are function template specializations, and the function |
| 3723 | // template for F1 is more specialized than the template for F2 |
| 3724 | // 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] | 3725 | // if not that, |
Douglas Gregor | 55137cb | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 3726 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 3727 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3728 | if (FunctionTemplateDecl *BetterTemplate |
| 3729 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 3730 | Cand2.Function->getPrimaryTemplate(), |
| 3731 | true)) |
| 3732 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3733 | |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3734 | // -- the context is an initialization by user-defined conversion |
| 3735 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3736 | // from the return type of F1 to the destination type (i.e., |
| 3737 | // the type of the entity being initialized) is a better |
| 3738 | // conversion sequence than the standard conversion sequence |
| 3739 | // from the return type of F2 to the destination type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | if (Cand1.Function && Cand2.Function && |
| 3741 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | a1f013e | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3742 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3743 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3744 | Cand2.FinalConversion)) { |
| 3745 | case ImplicitConversionSequence::Better: |
| 3746 | // Cand1 has a better conversion sequence. |
| 3747 | return true; |
| 3748 | |
| 3749 | case ImplicitConversionSequence::Worse: |
| 3750 | // Cand1 can't be better than Cand2. |
| 3751 | return false; |
| 3752 | |
| 3753 | case ImplicitConversionSequence::Indistinguishable: |
| 3754 | // Do nothing |
| 3755 | break; |
| 3756 | } |
| 3757 | } |
| 3758 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3759 | return false; |
| 3760 | } |
| 3761 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3763 | /// within an overload candidate set. |
| 3764 | /// |
| 3765 | /// \param CandidateSet the set of candidate functions. |
| 3766 | /// |
| 3767 | /// \param Loc the location of the function name (or operator symbol) for |
| 3768 | /// which overload resolution occurs. |
| 3769 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3771 | /// function, Best points to the candidate function found. |
| 3772 | /// |
| 3773 | /// \returns The result of overload resolution. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | Sema::OverloadingResult |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3775 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3776 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3778 | // Find the best viable function. |
| 3779 | Best = CandidateSet.end(); |
| 3780 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3781 | Cand != CandidateSet.end(); ++Cand) { |
| 3782 | if (Cand->Viable) { |
| 3783 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3784 | Best = Cand; |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | // If we didn't find any viable functions, abort. |
| 3789 | if (Best == CandidateSet.end()) |
| 3790 | return OR_No_Viable_Function; |
| 3791 | |
| 3792 | // Make sure that this function is better than every other viable |
| 3793 | // function. If not, we have an ambiguity. |
| 3794 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3795 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | if (Cand->Viable && |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3797 | Cand != Best && |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3798 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3799 | Best = CandidateSet.end(); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3800 | return OR_Ambiguous; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3801 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3802 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3804 | // Best is the best viable function. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3805 | if (Best->Function && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3807 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3808 | return OR_Deleted; |
| 3809 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3810 | // C++ [basic.def.odr]p2: |
| 3811 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3812 | // when referred to from a potentially-evaluated expression. [Note: this |
| 3813 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3814 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 3815 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 3816 | if (Best->Function) |
| 3817 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3818 | return OR_Success; |
| 3819 | } |
| 3820 | |
| 3821 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3822 | /// diagnostic messages containing the candidates in the candidate |
| 3823 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3824 | void |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3825 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | bool OnlyViable) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3827 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3828 | LastCand = CandidateSet.end(); |
| 3829 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3830 | if (Cand->Viable || !OnlyViable) { |
| 3831 | if (Cand->Function) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3832 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3833 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3834 | // Deleted or "unavailable" function. |
| 3835 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3836 | << Cand->Function->isDeleted(); |
| 3837 | } else { |
| 3838 | // Normal function |
| 3839 | // FIXME: Give a better reason! |
| 3840 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
| 3841 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3842 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3843 | // Desugar the type of the surrogate down to a function type, |
| 3844 | // retaining as many typedefs as possible while still showing |
| 3845 | // the function type (and, therefore, its parameter types). |
| 3846 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3847 | bool isLValueReference = false; |
| 3848 | bool isRValueReference = false; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3849 | bool isPointer = false; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3850 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3851 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3852 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3853 | isLValueReference = true; |
| 3854 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3855 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3856 | FnType = FnTypeRef->getPointeeType(); |
| 3857 | isRValueReference = true; |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3858 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3859 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3860 | FnType = FnTypePtr->getPointeeType(); |
| 3861 | isPointer = true; |
| 3862 | } |
| 3863 | // Desugar down to a function type. |
| 3864 | FnType = QualType(FnType->getAsFunctionType(), 0); |
| 3865 | // Reconstruct the pointer/reference as appropriate. |
| 3866 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3867 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 3868 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 4fc308b | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3870 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3871 | << FnType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3872 | } else { |
| 3873 | // FIXME: We need to get the identifier in here |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3874 | // FIXME: Do we want the error message to point at the operator? |
| 3875 | // (built-ins won't have a location) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | QualType FnType |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3877 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 3878 | Cand->BuiltinTypes.ParamTypes, |
| 3879 | Cand->Conversions.size(), |
| 3880 | false, 0); |
| 3881 | |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3882 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3885 | } |
| 3886 | } |
| 3887 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3888 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 3889 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 3890 | /// expression with overloaded function type and @p ToType is the type |
| 3891 | /// we're trying to resolve to. For example: |
| 3892 | /// |
| 3893 | /// @code |
| 3894 | /// int f(double); |
| 3895 | /// int f(int); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | /// |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3897 | /// int (*pfd)(double) = f; // selects f(double) |
| 3898 | /// @endcode |
| 3899 | /// |
| 3900 | /// This routine returns the resulting FunctionDecl if it could be |
| 3901 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 3902 | /// routine will emit diagnostics if there is an error. |
| 3903 | FunctionDecl * |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3904 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3905 | bool Complain) { |
| 3906 | QualType FunctionType = ToType; |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3907 | bool IsMember = false; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3908 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3909 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3910 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | b566c6c | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 3911 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3912 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3913 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3914 | FunctionType = MemTypePtr->getPointeeType(); |
| 3915 | IsMember = true; |
| 3916 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3917 | |
| 3918 | // We only look at pointers or references to functions. |
Douglas Gregor | 6b6ba8b | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 3919 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3920 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3921 | return 0; |
| 3922 | |
| 3923 | // Find the actual overloaded function declaration. |
| 3924 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3926 | // C++ [over.over]p1: |
| 3927 | // [...] [Note: any redundant set of parentheses surrounding the |
| 3928 | // overloaded function name is ignored (5.1). ] |
| 3929 | Expr *OvlExpr = From->IgnoreParens(); |
| 3930 | |
| 3931 | // C++ [over.over]p1: |
| 3932 | // [...] The overloaded function name can be preceded by the & |
| 3933 | // operator. |
| 3934 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 3935 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 3936 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 3937 | } |
| 3938 | |
| 3939 | // Try to dig out the overloaded function. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3940 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 3941 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3942 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3943 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
| 3944 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3945 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3947 | // we're done. |
| 3948 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3949 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3951 | OverloadIterator Fun; |
| 3952 | if (Ovl) |
| 3953 | Fun = Ovl; |
| 3954 | else |
| 3955 | Fun = FunctionTemplate; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3957 | // Look through all of the overloaded functions, searching for one |
| 3958 | // whose type matches exactly. |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3959 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3961 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3962 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3963 | // C++ [over.over]p3: |
| 3964 | // Non-member functions and static member functions match |
Sebastian Redl | 16d307d | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 3965 | // targets of type "pointer-to-function" or "reference-to-function." |
| 3966 | // Nonstatic member functions match targets of |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3967 | // type "pointer-to-member-function." |
| 3968 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3969 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3971 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3972 | if (CXXMethodDecl *Method |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3973 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3975 | // static when converting to member pointer. |
| 3976 | if (Method->isStatic() == IsMember) |
| 3977 | continue; |
| 3978 | } else if (IsMember) |
| 3979 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3981 | // C++ [over.over]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3982 | // If the name is a function template, template argument deduction is |
| 3983 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 3984 | // resulting template argument list is used to generate a single |
| 3985 | // function template specialization, which is added to the set of |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3986 | // overloaded functions considered. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3987 | FunctionDecl *Specialization = 0; |
| 3988 | TemplateDeductionInfo Info(Context); |
| 3989 | if (TemplateDeductionResult Result |
| 3990 | = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false, |
| 3991 | /*FIXME:*/0, /*FIXME:*/0, |
| 3992 | FunctionType, Specialization, Info)) { |
| 3993 | // FIXME: make a note of the failed deduction for diagnostics. |
| 3994 | (void)Result; |
| 3995 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 | assert(FunctionType |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3997 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3998 | Matches.insert( |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3999 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4000 | } |
| 4001 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4002 | |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4003 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 4004 | // Skip non-static functions when converting to pointer, and static |
| 4005 | // when converting to member pointer. |
| 4006 | if (Method->isStatic() == IsMember) |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4007 | continue; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4008 | } else if (IsMember) |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4009 | continue; |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4011 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4012 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4013 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4014 | FoundNonTemplateFunction = true; |
| 4015 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4019 | // If there were 0 or 1 matches, we're done. |
| 4020 | if (Matches.empty()) |
| 4021 | return 0; |
| 4022 | else if (Matches.size() == 1) |
| 4023 | return *Matches.begin(); |
| 4024 | |
| 4025 | // C++ [over.over]p4: |
| 4026 | // If more than one function is selected, [...] |
| 4027 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4028 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4029 | if (FoundNonTemplateFunction) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4030 | // [...] any function template specializations in the set are |
| 4031 | // eliminated if the set also contains a non-template function, [...] |
| 4032 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4033 | if ((*M)->getPrimaryTemplate() == 0) |
| 4034 | RemainingMatches.push_back(*M); |
| 4035 | } else { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4036 | // [...] and any given function template specialization F1 is |
| 4037 | // eliminated if the set contains a second function template |
| 4038 | // specialization whose function template is more specialized |
| 4039 | // than the function template of F1 according to the partial |
| 4040 | // ordering rules of 14.5.5.2. |
| 4041 | |
| 4042 | // The algorithm specified above is quadratic. We instead use a |
| 4043 | // two-pass algorithm (similar to the one used to identify the |
| 4044 | // best viable function in an overload set) that identifies the |
| 4045 | // best function template (if it exists). |
| 4046 | MatchIter Best = Matches.begin(); |
| 4047 | MatchIter M = Best, MEnd = Matches.end(); |
| 4048 | // Find the most specialized function. |
| 4049 | for (++M; M != MEnd; ++M) |
| 4050 | if (getMoreSpecializedTemplate((*M)->getPrimaryTemplate(), |
| 4051 | (*Best)->getPrimaryTemplate(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4052 | false) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4053 | == (*M)->getPrimaryTemplate()) |
| 4054 | Best = M; |
| 4055 | |
| 4056 | // Determine whether this function template is more specialized |
| 4057 | // that all of the others. |
| 4058 | bool Ambiguous = false; |
| 4059 | for (M = Matches.begin(); M != MEnd; ++M) { |
| 4060 | if (M != Best && |
| 4061 | getMoreSpecializedTemplate((*M)->getPrimaryTemplate(), |
| 4062 | (*Best)->getPrimaryTemplate(), |
| 4063 | false) |
| 4064 | != (*Best)->getPrimaryTemplate()) { |
| 4065 | Ambiguous = true; |
| 4066 | break; |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | // If one function template was more specialized than all of the |
| 4071 | // others, return it. |
| 4072 | if (!Ambiguous) |
| 4073 | return *Best; |
| 4074 | |
| 4075 | // We could not find a most-specialized function template, which |
| 4076 | // is equivalent to having a set of function templates with more |
| 4077 | // than one such template. So, we place all of the function |
| 4078 | // templates into the set of remaining matches and produce a |
| 4079 | // diagnostic below. FIXME: we could perform the quadratic |
| 4080 | // algorithm here, pruning the result set to limit the number of |
| 4081 | // candidates output later. |
| 4082 | RemainingMatches.append(Matches.begin(), Matches.end()); |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4083 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
| 4085 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4086 | // selected function. |
| 4087 | if (RemainingMatches.size() == 1) |
| 4088 | return RemainingMatches.front(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4089 | |
Douglas Gregor | b257e4f | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4090 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4091 | // returns (even if we issue the diagnostics here). |
| 4092 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4093 | << RemainingMatches[0]->getDeclName(); |
| 4094 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4095 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4096 | return 0; |
| 4097 | } |
| 4098 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4099 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4100 | /// (which eventually refers to the declaration Func) and the call |
| 4101 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4102 | /// to a specific function. If overload resolution succeeds, returns |
| 4103 | /// the function declaration produced by overload |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4104 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4105 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4106 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4107 | DeclarationName UnqualifiedName, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4108 | bool HasExplicitTemplateArgs, |
| 4109 | const TemplateArgument *ExplicitTemplateArgs, |
| 4110 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4111 | SourceLocation LParenLoc, |
| 4112 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | SourceLocation *CommaLocs, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4114 | SourceLocation RParenLoc, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4115 | bool &ArgumentDependentLookup) { |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4116 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4117 | |
| 4118 | // Add the functions denoted by Callee to the set of candidate |
| 4119 | // functions. While we're doing so, track whether argument-dependent |
| 4120 | // lookup still applies, per: |
| 4121 | // |
| 4122 | // C++0x [basic.lookup.argdep]p3: |
| 4123 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4124 | // and let Y be the lookup set produced by argument dependent |
| 4125 | // lookup (defined as follows). If X contains |
| 4126 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | // -- a declaration of a class member, or |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4128 | // |
| 4129 | // -- a block-scope function declaration that is not a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | // using-declaration, or |
| 4131 | // |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4132 | // -- a declaration that is neither a function or a function |
| 4133 | // template |
| 4134 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | // then Y is empty. |
| 4136 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4137 | = dyn_cast_or_null<OverloadedFunctionDecl>(Callee)) { |
| 4138 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4139 | FuncEnd = Ovl->function_end(); |
| 4140 | Func != FuncEnd; ++Func) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4141 | DeclContext *Ctx = 0; |
| 4142 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Func)) { |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4143 | if (HasExplicitTemplateArgs) |
| 4144 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4145 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4146 | AddOverloadCandidate(FunDecl, Args, NumArgs, CandidateSet); |
| 4147 | Ctx = FunDecl->getDeclContext(); |
| 4148 | } else { |
| 4149 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*Func); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4150 | AddTemplateOverloadCandidate(FunTmpl, HasExplicitTemplateArgs, |
| 4151 | ExplicitTemplateArgs, |
| 4152 | NumExplicitTemplateArgs, |
| 4153 | Args, NumArgs, CandidateSet); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4154 | Ctx = FunTmpl->getDeclContext(); |
| 4155 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4156 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (Ctx->isRecord() || Ctx->isFunctionOrMethod()) |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4159 | ArgumentDependentLookup = false; |
| 4160 | } |
| 4161 | } else if (FunctionDecl *Func = dyn_cast_or_null<FunctionDecl>(Callee)) { |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4162 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4163 | AddOverloadCandidate(Func, Args, NumArgs, CandidateSet); |
| 4164 | |
| 4165 | if (Func->getDeclContext()->isRecord() || |
| 4166 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4167 | ArgumentDependentLookup = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | } else if (FunctionTemplateDecl *FuncTemplate |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4169 | = dyn_cast_or_null<FunctionTemplateDecl>(Callee)) { |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4170 | AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4171 | ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | NumExplicitTemplateArgs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4173 | Args, NumArgs, CandidateSet); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4174 | |
| 4175 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4176 | ArgumentDependentLookup = false; |
| 4177 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4178 | |
| 4179 | if (Callee) |
| 4180 | UnqualifiedName = Callee->getDeclName(); |
| 4181 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4182 | // FIXME: Pass explicit template arguments through for ADL |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4183 | if (ArgumentDependentLookup) |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4184 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4185 | CandidateSet); |
| 4186 | |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4187 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4188 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | a60a691 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4189 | case OR_Success: |
| 4190 | return Best->Function; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4191 | |
| 4192 | case OR_No_Viable_Function: |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4193 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4194 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4195 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4196 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4197 | break; |
| 4198 | |
| 4199 | case OR_Ambiguous: |
| 4200 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4201 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4202 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4203 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4204 | |
| 4205 | case OR_Deleted: |
| 4206 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4207 | << Best->Function->isDeleted() |
| 4208 | << UnqualifiedName |
| 4209 | << Fn->getSourceRange(); |
| 4210 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4211 | break; |
Douglas Gregor | 99dcbff | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4215 | // return NULL. |
| 4216 | Fn->Destroy(Context); |
| 4217 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4218 | Args[Arg]->Destroy(Context); |
| 4219 | return 0; |
| 4220 | } |
| 4221 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4222 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4223 | /// operator. |
| 4224 | /// |
| 4225 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4226 | /// |
| 4227 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4228 | /// operator. |
| 4229 | /// |
| 4230 | /// \param Functions The set of non-member functions that will be |
| 4231 | /// considered by overload resolution. The caller needs to build this |
| 4232 | /// set based on the context using, e.g., |
| 4233 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4234 | /// set should not contain any member functions; those will be added |
| 4235 | /// by CreateOverloadedUnaryOp(). |
| 4236 | /// |
| 4237 | /// \param input The input argument. |
| 4238 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4239 | unsigned OpcIn, |
| 4240 | FunctionSet &Functions, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4242 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4243 | Expr *Input = (Expr *)input.get(); |
| 4244 | |
| 4245 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4246 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4247 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4248 | |
| 4249 | Expr *Args[2] = { Input, 0 }; |
| 4250 | unsigned NumArgs = 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4252 | // For post-increment and post-decrement, add the implicit '0' as |
| 4253 | // the second argument, so that we know this is a post-increment or |
| 4254 | // post-decrement. |
| 4255 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4256 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4258 | SourceLocation()); |
| 4259 | NumArgs = 2; |
| 4260 | } |
| 4261 | |
| 4262 | if (Input->isTypeDependent()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4263 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4264 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4266 | FuncEnd = Functions.end(); |
| 4267 | Func != FuncEnd; ++Func) |
| 4268 | Overloads->addOverload(*Func); |
| 4269 | |
| 4270 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4271 | OpLoc, false, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4273 | input.release(); |
| 4274 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4275 | &Args[0], NumArgs, |
| 4276 | Context.DependentTy, |
| 4277 | OpLoc)); |
| 4278 | } |
| 4279 | |
| 4280 | // Build an empty overload set. |
| 4281 | OverloadCandidateSet CandidateSet; |
| 4282 | |
| 4283 | // Add the candidates from the given function set. |
| 4284 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4285 | |
| 4286 | // Add operator candidates that are member functions. |
| 4287 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4288 | |
| 4289 | // Add builtin operator candidates. |
| 4290 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 4291 | |
| 4292 | // Perform overload resolution. |
| 4293 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4294 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4295 | case OR_Success: { |
| 4296 | // We found a built-in operator or an overloaded operator. |
| 4297 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4298 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4299 | if (FnDecl) { |
| 4300 | // We matched an overloaded operator. Build a call to that |
| 4301 | // operator. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4303 | // Convert the arguments. |
| 4304 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4305 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4306 | return ExprError(); |
| 4307 | } else { |
| 4308 | // Convert the arguments. |
| 4309 | if (PerformCopyInitialization(Input, |
| 4310 | FnDecl->getParamDecl(0)->getType(), |
| 4311 | "passing")) |
| 4312 | return ExprError(); |
| 4313 | } |
| 4314 | |
| 4315 | // Determine the result type |
| 4316 | QualType ResultTy |
| 4317 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4318 | ResultTy = ResultTy.getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4320 | // Build the actual expression node. |
| 4321 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4322 | SourceLocation()); |
| 4323 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4325 | input.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4326 | |
| 4327 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | e80ccac | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4328 | &Input, 1, ResultTy, OpLoc); |
| 4329 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4330 | } else { |
| 4331 | // We matched a built-in operator. Convert the arguments, then |
| 4332 | // break out so that we will build the appropriate built-in |
| 4333 | // operator node. |
| 4334 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4335 | Best->Conversions[0], "passing")) |
| 4336 | return ExprError(); |
| 4337 | |
| 4338 | break; |
| 4339 | } |
| 4340 | } |
| 4341 | |
| 4342 | case OR_No_Viable_Function: |
| 4343 | // No viable function; fall through to handling this as a |
| 4344 | // built-in operator, which will produce an error message for us. |
| 4345 | break; |
| 4346 | |
| 4347 | case OR_Ambiguous: |
| 4348 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4349 | << UnaryOperator::getOpcodeStr(Opc) |
| 4350 | << Input->getSourceRange(); |
| 4351 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4352 | return ExprError(); |
| 4353 | |
| 4354 | case OR_Deleted: |
| 4355 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4356 | << Best->Function->isDeleted() |
| 4357 | << UnaryOperator::getOpcodeStr(Opc) |
| 4358 | << Input->getSourceRange(); |
| 4359 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4360 | return ExprError(); |
| 4361 | } |
| 4362 | |
| 4363 | // Either we found no viable overloaded operator or we matched a |
| 4364 | // built-in operator. In either case, fall through to trying to |
| 4365 | // build a built-in operation. |
| 4366 | input.release(); |
| 4367 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4368 | } |
| 4369 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4370 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4371 | /// operator. |
| 4372 | /// |
| 4373 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4374 | /// |
| 4375 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4376 | /// operator. |
| 4377 | /// |
| 4378 | /// \param Functions The set of non-member functions that will be |
| 4379 | /// considered by overload resolution. The caller needs to build this |
| 4380 | /// set based on the context using, e.g., |
| 4381 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4382 | /// set should not contain any member functions; those will be added |
| 4383 | /// by CreateOverloadedBinOp(). |
| 4384 | /// |
| 4385 | /// \param LHS Left-hand argument. |
| 4386 | /// \param RHS Right-hand argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | Sema::OwningExprResult |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4388 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | unsigned OpcIn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4390 | FunctionSet &Functions, |
| 4391 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4392 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4393 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4394 | |
| 4395 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4396 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4397 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4398 | |
| 4399 | // If either side is type-dependent, create an appropriate dependent |
| 4400 | // expression. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4401 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4402 | // .* cannot be overloaded. |
| 4403 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4404 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4405 | Context.DependentTy, OpLoc)); |
| 4406 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4408 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4410 | FuncEnd = Functions.end(); |
| 4411 | Func != FuncEnd; ++Func) |
| 4412 | Overloads->addOverload(*Func); |
| 4413 | |
| 4414 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4415 | OpLoc, false, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4417 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4418 | Args, 2, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4419 | Context.DependentTy, |
| 4420 | OpLoc)); |
| 4421 | } |
| 4422 | |
| 4423 | // If this is the .* operator, which is not overloadable, just |
| 4424 | // create a built-in binary operator. |
| 4425 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4426 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4427 | |
| 4428 | // If this is one of the assignment operators, we only perform |
| 4429 | // overload resolution if the left-hand side is a class or |
| 4430 | // enumeration type (C++ [expr.ass]p3). |
| 4431 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4432 | !Args[0]->getType()->isOverloadableType()) |
| 4433 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4434 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4435 | // Build an empty overload set. |
| 4436 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4437 | |
| 4438 | // Add the candidates from the given function set. |
| 4439 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4440 | |
| 4441 | // Add operator candidates that are member functions. |
| 4442 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4443 | |
| 4444 | // Add builtin operator candidates. |
| 4445 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 4446 | |
| 4447 | // Perform overload resolution. |
| 4448 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4449 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4450 | case OR_Success: { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4451 | // We found a built-in operator or an overloaded operator. |
| 4452 | FunctionDecl *FnDecl = Best->Function; |
| 4453 | |
| 4454 | if (FnDecl) { |
| 4455 | // We matched an overloaded operator. Build a call to that |
| 4456 | // operator. |
| 4457 | |
| 4458 | // Convert the arguments. |
| 4459 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4460 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4461 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4462 | "passing")) |
| 4463 | return ExprError(); |
| 4464 | } else { |
| 4465 | // Convert the arguments. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4466 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4467 | "passing") || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4468 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4469 | "passing")) |
| 4470 | return ExprError(); |
| 4471 | } |
| 4472 | |
| 4473 | // Determine the result type |
| 4474 | QualType ResultTy |
| 4475 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4476 | ResultTy = ResultTy.getNonReferenceType(); |
| 4477 | |
| 4478 | // Build the actual expression node. |
| 4479 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | ef1c1e5 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4480 | OpLoc); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4481 | UsualUnaryConversions(FnExpr); |
| 4482 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4483 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | e80ccac | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4484 | Args, 2, ResultTy, OpLoc); |
| 4485 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4486 | } else { |
| 4487 | // We matched a built-in operator. Convert the arguments, then |
| 4488 | // break out so that we will build the appropriate built-in |
| 4489 | // operator node. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4490 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4491 | Best->Conversions[0], "passing") || |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4492 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4493 | Best->Conversions[1], "passing")) |
| 4494 | return ExprError(); |
| 4495 | |
| 4496 | break; |
| 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | case OR_No_Viable_Function: |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4501 | // For class as left operand for assignment or compound assigment operator |
| 4502 | // do not fall through to handling in built-in, but report that no overloaded |
| 4503 | // assignment operator found |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4504 | if (Args[0]->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4505 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4506 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4507 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Sebastian Redl | 027de2a | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4508 | return ExprError(); |
| 4509 | } |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4510 | // No viable function; fall through to handling this as a |
| 4511 | // built-in operator, which will produce an error message for us. |
| 4512 | break; |
| 4513 | |
| 4514 | case OR_Ambiguous: |
| 4515 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4516 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4517 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4518 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4519 | return ExprError(); |
| 4520 | |
| 4521 | case OR_Deleted: |
| 4522 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4523 | << Best->Function->isDeleted() |
| 4524 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4525 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4526 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4527 | return ExprError(); |
| 4528 | } |
| 4529 | |
| 4530 | // Either we found no viable overloaded operator or we matched a |
| 4531 | // built-in operator. In either case, try to build a built-in |
| 4532 | // operation. |
Douglas Gregor | e9899d9 | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4533 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4536 | /// BuildCallToMemberFunction - Build a call to a member |
| 4537 | /// function. MemExpr is the expression that refers to the member |
| 4538 | /// function (and includes the object parameter), Args/NumArgs are the |
| 4539 | /// arguments to the function call (not including the object |
| 4540 | /// parameter). The caller needs to validate that the member |
| 4541 | /// expression refers to a member function or an overloaded member |
| 4542 | /// function. |
| 4543 | Sema::ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4544 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 4545 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4546 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 4547 | SourceLocation RParenLoc) { |
| 4548 | // Dig out the member expression. This holds both the object |
| 4549 | // argument and the member function we're referring to. |
| 4550 | MemberExpr *MemExpr = 0; |
| 4551 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 4552 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 4553 | else |
| 4554 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 4555 | assert(MemExpr && "Building member call without member expression"); |
| 4556 | |
| 4557 | // Extract the object argument. |
| 4558 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | bfdea0f | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4559 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4560 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4561 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 4562 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4563 | // Add overload candidates |
| 4564 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4565 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4566 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4567 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 4568 | Func != FuncEnd; ++Func) { |
| 4569 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4570 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4571 | /*SuppressUserConversions=*/false); |
| 4572 | else |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 4573 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 4574 | MemExpr->hasExplicitTemplateArgumentList(), |
| 4575 | MemExpr->getTemplateArgs(), |
| 4576 | MemExpr->getNumTemplateArgs(), |
| 4577 | ObjectArg, Args, NumArgs, |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4578 | CandidateSet, |
| 4579 | /*SuppressUsedConversions=*/false); |
| 4580 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4582 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4583 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4584 | case OR_Success: |
| 4585 | Method = cast<CXXMethodDecl>(Best->Function); |
| 4586 | break; |
| 4587 | |
| 4588 | case OR_No_Viable_Function: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4590 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4591 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4592 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4593 | // FIXME: Leaking incoming expressions! |
| 4594 | return true; |
| 4595 | |
| 4596 | case OR_Ambiguous: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4598 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4599 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4600 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4601 | // FIXME: Leaking incoming expressions! |
| 4602 | return true; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4603 | |
| 4604 | case OR_Deleted: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4606 | diag::err_ovl_deleted_member_call) |
| 4607 | << Best->Function->isDeleted() |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4608 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4609 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4610 | // FIXME: Leaking incoming expressions! |
| 4611 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | FixOverloadedFunctionReference(MemExpr, Method); |
| 4615 | } else { |
| 4616 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 4617 | } |
| 4618 | |
| 4619 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4620 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4621 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4622 | NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4623 | Method->getResultType().getNonReferenceType(), |
| 4624 | RParenLoc)); |
| 4625 | |
| 4626 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4627 | if (!Method->isStatic() && |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4628 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4629 | return true; |
| 4630 | MemExpr->setBase(ObjectArg); |
| 4631 | |
| 4632 | // Convert the rest of the arguments |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4633 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4635 | RParenLoc)) |
| 4636 | return true; |
| 4637 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4638 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4639 | return true; |
Anders Carlsson | 8c84c20 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 4640 | |
| 4641 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4644 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4645 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4646 | /// overloaded function call operator (@c operator()) or performing a |
| 4647 | /// user-defined conversion on the object argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | Sema::ExprResult |
| 4649 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4650 | SourceLocation LParenLoc, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4651 | Expr **Args, unsigned NumArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | SourceLocation *CommaLocs, |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4653 | SourceLocation RParenLoc) { |
| 4654 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4655 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4656 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4657 | // C++ [over.call.object]p1: |
| 4658 | // If the primary-expression E in the function call syntax |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4659 | // 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] | 4660 | // candidate functions includes at least the function call |
| 4661 | // operators of T. The function call operators of T are obtained by |
| 4662 | // ordinary lookup of the name operator() in the context of |
| 4663 | // (E).operator(). |
| 4664 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4665 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4666 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4667 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4668 | Oper != OperEnd; ++Oper) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4670 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4671 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4672 | // C++ [over.call.object]p2: |
| 4673 | // In addition, for each conversion function declared in T of the |
| 4674 | // form |
| 4675 | // |
| 4676 | // operator conversion-type-id () cv-qualifier; |
| 4677 | // |
| 4678 | // where cv-qualifier is the same cv-qualification as, or a |
| 4679 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | f49fdf8 | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4680 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4681 | // R", or the type "reference to pointer to function of |
| 4682 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4683 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4684 | // is also considered as a candidate function. Similarly, |
| 4685 | // surrogate call functions are added to the set of candidate |
| 4686 | // functions for each conversion function declared in an |
| 4687 | // accessible base class provided the function is not hidden |
| 4688 | // within T by another intervening declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4690 | if (!RequireCompleteType(SourceLocation(), Object->getType(), 0)) { |
| 4691 | // FIXME: Look in base classes for more conversion operators! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4693 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4694 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4695 | Func = Conversions->function_begin(), |
| 4696 | FuncEnd = Conversions->function_end(); |
| 4697 | Func != FuncEnd; ++Func) { |
| 4698 | CXXConversionDecl *Conv; |
| 4699 | FunctionTemplateDecl *ConvTemplate; |
| 4700 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4701 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4702 | // Skip over templated conversion functions; they aren't |
| 4703 | // surrogates. |
| 4704 | if (ConvTemplate) |
| 4705 | continue; |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4706 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4707 | // Strip the reference type (if any) and then the pointer type (if |
| 4708 | // any) to get down to what might be a function type. |
| 4709 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 4710 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4711 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4712 | |
Douglas Gregor | 8a2e601 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4713 | if (const FunctionProtoType *Proto = ConvType->getAsFunctionProtoType()) |
| 4714 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4715 | } |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4716 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4718 | // Perform overload resolution. |
| 4719 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4720 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4721 | case OR_Success: |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4722 | // Overload resolution succeeded; we'll build the appropriate call |
| 4723 | // below. |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4724 | break; |
| 4725 | |
| 4726 | case OR_No_Viable_Function: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | 15b02d2 | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4728 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 45d9d60 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4729 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | 15b02d2 | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4730 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4731 | break; |
| 4732 | |
| 4733 | case OR_Ambiguous: |
| 4734 | Diag(Object->getSourceRange().getBegin(), |
| 4735 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4736 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4737 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4738 | break; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4739 | |
| 4740 | case OR_Deleted: |
| 4741 | Diag(Object->getSourceRange().getBegin(), |
| 4742 | diag::err_ovl_deleted_object_call) |
| 4743 | << Best->Function->isDeleted() |
| 4744 | << Object->getType() << Object->getSourceRange(); |
| 4745 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4746 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4748 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4749 | if (Best == CandidateSet.end()) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4750 | // We had an error; delete all of the subexpressions and return |
| 4751 | // the error. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4752 | Object->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4753 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4754 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4755 | return true; |
| 4756 | } |
| 4757 | |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4758 | if (Best->Function == 0) { |
| 4759 | // Since there is no function declaration, this is one of the |
| 4760 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | CXXConversionDecl *Conv |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4762 | = cast<CXXConversionDecl>( |
| 4763 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4764 | |
| 4765 | // We selected one of the surrogate functions that converts the |
| 4766 | // object parameter to a function pointer. Perform the conversion |
| 4767 | // on the object argument, then let ActOnCallExpr finish the job. |
| 4768 | // FIXME: Represent the user-defined conversion in the AST! |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4769 | ImpCastExprToType(Object, |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4770 | Conv->getConversionType().getNonReferenceType(), |
Anders Carlsson | a076d14 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 4771 | CastExpr::CK_Unknown, |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4772 | Conv->getConversionType()->isLValueReferenceType()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4773 | return ActOnCallExpr(S, ExprArg(*this, Object), LParenLoc, |
| 4774 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4775 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | ab7897a | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4779 | // that calls this method, using Object for the implicit object |
| 4780 | // parameter and passing along the remaining arguments. |
| 4781 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4782 | const FunctionProtoType *Proto = Method->getType()->getAsFunctionProtoType(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4783 | |
| 4784 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4785 | unsigned NumArgsToCheck = NumArgs; |
| 4786 | |
| 4787 | // Build the full argument list for the method call (the |
| 4788 | // implicit object parameter is placed at the beginning of the |
| 4789 | // list). |
| 4790 | Expr **MethodArgs; |
| 4791 | if (NumArgs < NumArgsInProto) { |
| 4792 | NumArgsToCheck = NumArgsInProto; |
| 4793 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4794 | } else { |
| 4795 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4796 | } |
| 4797 | MethodArgs[0] = Object; |
| 4798 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4799 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4800 | |
| 4801 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4802 | SourceLocation()); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4803 | UsualUnaryConversions(NewFn); |
| 4804 | |
| 4805 | // Once we've built TheCall, all of the expressions are properly |
| 4806 | // owned. |
| 4807 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | ExprOwningPtr<CXXOperatorCallExpr> |
| 4809 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4810 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4811 | ResultTy, RParenLoc)); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4812 | delete [] MethodArgs; |
| 4813 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4814 | // We may have default arguments. If so, we need to allocate more |
| 4815 | // slots in the call for them. |
| 4816 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4817 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4818 | else if (NumArgs > NumArgsInProto) |
| 4819 | NumArgsToCheck = NumArgsInProto; |
| 4820 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4821 | bool IsError = false; |
| 4822 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4823 | // Initialize the implicit object parameter. |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4824 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4825 | TheCall->setArg(0, Object); |
| 4826 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4827 | |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4828 | // Check the argument types. |
| 4829 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4830 | Expr *Arg; |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4831 | if (i < NumArgs) { |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4832 | Arg = Args[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4833 | |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4834 | // Pass the argument. |
| 4835 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4836 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4837 | } else { |
Anders Carlsson | e827123 | 2009-08-14 18:30:22 +0000 | [diff] [blame] | 4838 | Arg = CXXDefaultArgExpr::Create(Context, Method->getParamDecl(i)); |
Douglas Gregor | 02a0acd | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4839 | } |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4840 | |
| 4841 | TheCall->setArg(i + 1, Arg); |
| 4842 | } |
| 4843 | |
| 4844 | // If this is a variadic call, handle args passed through "...". |
| 4845 | if (Proto->isVariadic()) { |
| 4846 | // Promote the arguments (C99 6.5.2.2p7). |
| 4847 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 4848 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4849 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4850 | TheCall->setArg(i + 1, Arg); |
| 4851 | } |
| 4852 | } |
| 4853 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4854 | if (IsError) return true; |
| 4855 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4856 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4857 | return true; |
| 4858 | |
Anders Carlsson | 1c83deb | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 4859 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 91cea0a | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4862 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | /// (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] | 4864 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4865 | Sema::OwningExprResult |
| 4866 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 4867 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4868 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4869 | |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4870 | // C++ [over.ref]p1: |
| 4871 | // |
| 4872 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 4873 | // for a class object x of type T if T::operator->() exists and if |
| 4874 | // the operator is selected as the best match function by the |
| 4875 | // overload resolution mechanism (13.3). |
| 4876 | // FIXME: look in base classes. |
| 4877 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 4878 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4879 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4880 | |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame^] | 4881 | LookupResult R = LookupQualifiedName(BaseRecord->getDecl(), OpName, |
| 4882 | LookupOrdinaryName); |
| 4883 | |
| 4884 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 4885 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4886 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4887 | /*SuppressUserConversions=*/false); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4888 | |
| 4889 | // Perform overload resolution. |
| 4890 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4891 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4892 | case OR_Success: |
| 4893 | // Overload resolution succeeded; we'll build the call below. |
| 4894 | break; |
| 4895 | |
| 4896 | case OR_No_Viable_Function: |
| 4897 | if (CandidateSet.empty()) |
| 4898 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4899 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4900 | else |
| 4901 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4902 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4903 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4904 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4905 | |
| 4906 | case OR_Ambiguous: |
| 4907 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame^] | 4908 | << "->" << Base->getSourceRange(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4909 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4910 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4911 | |
| 4912 | case OR_Deleted: |
| 4913 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4914 | << Best->Function->isDeleted() |
Anders Carlsson | 78b5493 | 2009-09-10 23:18:36 +0000 | [diff] [blame^] | 4915 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4916 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4917 | return ExprError(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | // Convert the object parameter. |
| 4921 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4922 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4923 | return ExprError(); |
Douglas Gregor | 9ecea26 | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4924 | |
| 4925 | // No concerns about early exits now. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4926 | BaseIn.release(); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4927 | |
| 4928 | // Build the operator call. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4929 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 4930 | SourceLocation()); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4931 | UsualUnaryConversions(FnExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4932 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4933 | Method->getResultType().getNonReferenceType(), |
| 4934 | OpLoc); |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4935 | return Owned(Base); |
Douglas Gregor | e0e79bd | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4936 | } |
| 4937 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4938 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 4939 | /// a C++ overloaded function (possibly with some parentheses and |
| 4940 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 4941 | /// to the function declaration Fn, so patch up the expression E to |
| 4942 | /// refer (possibly indirectly) to Fn. |
| 4943 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 4944 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 4945 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 4946 | E->setType(PE->getSubExpr()->getType()); |
| 4947 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4949 | "Can only take the address of an overloaded function"); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4950 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4951 | if (Method->isStatic()) { |
| 4952 | // Do nothing: static member functions aren't any different |
| 4953 | // from non-member functions. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | } else if (QualifiedDeclRefExpr *DRE |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4955 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 4956 | // We have taken the address of a pointer to member |
| 4957 | // function. Perform the computation here so that we get the |
| 4958 | // appropriate pointer to member type. |
| 4959 | DRE->setDecl(Fn); |
| 4960 | DRE->setType(Fn->getType()); |
| 4961 | QualType ClassType |
| 4962 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | E->setType(Context.getMemberPointerType(Fn->getType(), |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4964 | ClassType.getTypePtr())); |
| 4965 | return; |
| 4966 | } |
| 4967 | } |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4968 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4969 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4970 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4971 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | isa<FunctionTemplateDecl>(DR->getDecl())) && |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4973 | "Expected overloaded function or function template"); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4974 | DR->setDecl(Fn); |
| 4975 | E->setType(Fn->getType()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4976 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 4977 | MemExpr->setMemberDecl(Fn); |
| 4978 | E->setType(Fn->getType()); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4979 | } else { |
| 4980 | assert(false && "Invalid reference to overloaded function"); |
| 4981 | } |
| 4982 | } |
| 4983 | |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4984 | } // end namespace clang |